UNPKG

@onesy/algorithms

Version:
23 lines (22 loc) 803 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // For every i, an entire i + 1 slice of array is looped to find the min value(smaller than i) within it, // and if that value is not the same as i, i and min are swapped. // Complexity: O(n ^ 2) function selectionSort(value, ascending = true) { const length = value.length; for (let i = 0; i < length; i++) { let minOrMax = i; for (let j = i + 1; j < length; j++) { if (ascending ? value[j] < value[minOrMax] : value[j] > value[minOrMax]) minOrMax = j; } if (minOrMax !== i) { const item = value[i]; value[i] = value[minOrMax]; value[minOrMax] = item; } } return value; } exports.default = selectionSort;