@@ -14,39 +14,38 @@ function checkArray(arr) {
14
14
}
15
15
16
16
/**
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
18
18
* 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.
21
19
*
22
- * Time complexity: O(N, N ^2)
20
+ * Time complexity: O(N^2)
23
21
* Space complexity: O(1)
24
22
*
25
23
* @param {Array } array
26
24
* @return {Array }
27
25
*
28
26
*/
29
- export function bubbleSort ( array ) {
27
+ export function selectionSort ( array ) {
30
28
const arr = array . slice ( ) ;
31
29
checkArray ( arr ) ;
32
30
if ( arr . length < 2 ) return arr ;
33
31
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 ;
40
37
}
41
38
}
42
- if ( ! swapped ) return arr ;
39
+ swap ( arr , i , min ) ;
43
40
}
44
41
return arr ;
45
42
}
46
43
47
44
/**
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
49
46
* 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.
50
49
*
51
50
* Time complexity: O(N^2)
52
51
* Space complexity: O(1)
@@ -55,19 +54,20 @@ export function bubbleSort(array) {
55
54
* @return {Array }
56
55
*
57
56
*/
58
- export function selectionSort ( array ) {
57
+ export function bubbleSort ( array ) {
59
58
const arr = array . slice ( ) ;
60
59
checkArray ( arr ) ;
61
60
if ( arr . length < 2 ) return arr ;
62
61
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 ;
68
68
}
69
69
}
70
- swap ( arr , i , min ) ;
70
+ if ( ! swapped ) return arr ;
71
71
}
72
72
return arr ;
73
73
}
@@ -77,9 +77,9 @@ export function selectionSort(array) {
77
77
* array before it.
78
78
* The second item is the first one to insert, this carries on until the last item is inserted into
79
79
* 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` .
81
81
*
82
- * Time complexity: O(N, N^2)
82
+ * Time complexity: O(N) ~ O( N^2)
83
83
* Space complexity: O(1)
84
84
*
85
85
* @param {Array } array
@@ -110,7 +110,7 @@ export function insertionSort(array) {
110
110
* The gap number is determined by a base number, for example, if the base number is 2, then for an
111
111
* array of 16 items, its gap range will be 7, 3, 1.
112
112
*
113
- * Time complexity: O(NlogN, N(logN)^2 )
113
+ * Time complexity: O(NlogN)
114
114
* Space complexity: O(1)
115
115
*
116
116
* @param {Array } array
@@ -176,7 +176,7 @@ export function quickSortWithArray(array) {
176
176
* The partition process will swap pairs of items until the left part are all smaller than pivot,
177
177
* and right part are all larger.
178
178
*
179
- * Time complexity: O(NlogN, N^2 )
179
+ * Time complexity: O(NlogN)
180
180
* Space complexity: O(logN)
181
181
*
182
182
* @param {Array } array
0 commit comments