Array Selection Sort Code in JavaScript and its Big-O

Array Selection Sort Code in JavaScript



function selectionSort(arr) {
    for (i =0; i<arr.length; i++) {
        indexSelection = i;
        for (j = i+1; j< arr.length; j++) {
             if (arr[j] < arr[indexSelection]) {
                   indexSelection = j;
             }
         } //end for j
        if (indexSelection !== i) {
           let temp = arr[indexSelection];
           arr[indexSelection] = arr[i];
           arr[i] = temp;
        }
        arr[i] = arr[indexSelection];
    }//end for i
    return arr;
}

Selection Sort complexity or BigO is O(n^2) in best, average and worst case scenarios.

Leave a Reply

Your email address will not be published. Required fields are marked *