Selection Sort

6 阅读1分钟

Code :

template <typename T>
void selectionSort(T arr[], int n) {
    for(int i=0; i<n; i++) {
        int minIndex = i;
        for(int j = i; j<n; j++) {
            if(arr[j]>arr[minIndex]){
                minIndex = j;
            }
        }
        swap(arr[i], arr[minIndex]);
    }
}

Analysis of Algorithm Complexity:

Time Complexity of the Algorithm: O(n^2)
Space Complexity of the Algorithm: O(1)