본문 바로가기

algorithm

selection algorithm

반응형
//  main.cpp
//  SelectSort
//
//  Created by  drake on 2020/05/24.
//  Copyright © 2020 drake. All rights reserved.
//

#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    int temp, i, j, index, min;
    int arr[10] = {5, 10, 1, 8, 7, 6, 4, 3, 2, 9};
    index = 0;

    for (i=0; i<10; i++) {
        min = 9999;
        for (j=i; j<10; j++) {
            if (min > arr[j]) {
                min = arr[j];
                index = j;
            }
        }
        
        if (i != index) {
            temp = arr[i];
            arr[i] = arr[index];
            arr[index] = temp;
        }
    }
    
    for (i=0; i<10; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");
    return 0;
}



time complecttime complexity = n * (n-1) /2
O(n^2)
반응형

'algorithm' 카테고리의 다른 글

using c++ in Mac OS  (0) 2020.05.24

]