Sorting

Implement Selection Sort

Problem

Given a list of numbers, sort it using the Selection Sort algorithm.

Example

{
"arr": [5, 8, 3, 9, 4, 1, 7]
}

Output:

[1, 3, 4, 5, 7, 8, 9]

Solution

function swap(arr, i, j) {
    var tempArr = arr[i];
    arr[i] = arr[j];
    arr[j] = tempArr;
}

function selection_sort(arr) {
    // Write your code here.
    for(var i=0; i arr[j]) {
                swap(arr, j-1, j);
                }
            }
        }
        return arr;
    }

Leave a comment