Skip to content

Commit be86110

Browse files
committed
refined sorting algo
1 parent 73a8c9a commit be86110

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/_.general/sorting.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@ function checkArray(arr) {
1414
}
1515

1616
/**
17-
* Bubble sort, sorted by swapping item pairs(if needed) from the start postion to the end of the
17+
* Selection sort, sorted by selecting min to max items for the start to the end position of the
1818
* array.
19-
* First round will have a largest item swapped to the array end, and second round will have the
20-
* second largest item swapped to array end - 1 position, and so on until all items are sorted.
2119
*
22-
* Time complexity: O(N, N^2)
20+
* Time complexity: O(N^2)
2321
* Space complexity: O(1)
2422
*
2523
* @param {Array} array
2624
* @return {Array}
2725
*
2826
*/
29-
export function bubbleSort(array) {
27+
export function selectionSort(array) {
3028
const arr = array.slice();
3129
checkArray(arr);
3230
if (arr.length < 2) return arr;
3331

34-
for (let i = arr.length - 1; i > 0; i--) {
35-
let swapped = false;
36-
for (let j = 0; j < i; j++) {
37-
if (arr[j] > arr[j + 1]) {
38-
swap(arr, j, j + 1);
39-
swapped = true;
32+
for (let i = 0; i < arr.length - 1; i++) {
33+
let min = i;
34+
for (let j = i + 1; j < arr.length; j++) {
35+
if (arr[min] > arr[j]) {
36+
min = j;
4037
}
4138
}
42-
if (!swapped) return arr;
39+
swap(arr, i, min);
4340
}
4441
return arr;
4542
}
4643

4744
/**
48-
* Selection sort, sorted by selecting min to max items for the start to the end position of the
45+
* Bubble sort, sorted by swapping item pairs(if needed) from the start postion to the end of the
4946
* array.
47+
* First round will have a largest item swapped to the array end, and second round will have the
48+
* second largest item swapped to array end - 1 position, and so on until all items are sorted.
5049
*
5150
* Time complexity: O(N^2)
5251
* Space complexity: O(1)
@@ -55,19 +54,20 @@ export function bubbleSort(array) {
5554
* @return {Array}
5655
*
5756
*/
58-
export function selectionSort(array) {
57+
export function bubbleSort(array) {
5958
const arr = array.slice();
6059
checkArray(arr);
6160
if (arr.length < 2) return arr;
6261

63-
for (let i = 0; i < arr.length - 1; i++) {
64-
let min = i;
65-
for (let j = i + 1; j < arr.length; j++) {
66-
if (arr[min] > arr[j]) {
67-
min = j;
62+
for (let i = arr.length - 1; i > 0; i--) {
63+
let swapped = false;
64+
for (let j = 0; j < i; j++) {
65+
if (arr[j] > arr[j + 1]) {
66+
swap(arr, j, j + 1);
67+
swapped = true;
6868
}
6969
}
70-
swap(arr, i, min);
70+
if (!swapped) return arr;
7171
}
7272
return arr;
7373
}
@@ -77,9 +77,9 @@ export function selectionSort(array) {
7777
* array before it.
7878
* The second item is the first one to insert, this carries on until the last item is inserted into
7979
* the sorted part of the array before it.
80-
* Insertion sort is extra fast when the array to sort is small, or is partially sorted.
80+
* Insertion sort is extra fast when the array to sort is `small`, or is `partially sorted`.
8181
*
82-
* Time complexity: O(N, N^2)
82+
* Time complexity: O(N) ~ O(N^2)
8383
* Space complexity: O(1)
8484
*
8585
* @param {Array} array
@@ -110,7 +110,7 @@ export function insertionSort(array) {
110110
* The gap number is determined by a base number, for example, if the base number is 2, then for an
111111
* array of 16 items, its gap range will be 7, 3, 1.
112112
*
113-
* Time complexity: O(NlogN, N(logN)^2)
113+
* Time complexity: O(NlogN)
114114
* Space complexity: O(1)
115115
*
116116
* @param {Array} array
@@ -176,7 +176,7 @@ export function quickSortWithArray(array) {
176176
* The partition process will swap pairs of items until the left part are all smaller than pivot,
177177
* and right part are all larger.
178178
*
179-
* Time complexity: O(NlogN, N^2)
179+
* Time complexity: O(NlogN)
180180
* Space complexity: O(logN)
181181
*
182182
* @param {Array} array

0 commit comments

Comments
 (0)