-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorting.js
154 lines (105 loc) · 2.74 KB
/
sorting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let arr = [0, 2, 213, 4, 21, 5, 3]
////////////////////////////////////////////////////////////merge sort//////////////////////////////////////
function mergeArr(arr) {
if (arr.length <= 1) {
return arr
}
let middle = Math.floor(arr.length / 2)
let left = arr.slice(0, middle)
let right = arr.slice(middle)
console.log(left, right);
return sortMergeArray(mergeArr(left), mergeArr(right))
}
function sortMergeArray(left, right) {
let sortedArr = []
let i = 0
let j = 0
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
sortedArr.push(left[i])
i++
} else {
sortedArr.push(right[j])
j++
}
}
while (i < left.length) {
sortedArr.push(left[i])
i++
}
while (j < right.length) {
sortedArr.push(right[j])
j++
}
console.log(sortedArr, 'sorted');
return sortedArr
}
console.log(mergeArr(arr));
//////////////////////////////////////////////////////////////quick sort/////////////////////////////////////
function quickSort(arr) {
if (arr.length <= 1) {
return arr
}
let pivot = arr[arr.length - 1]
let left = []
let right = []
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > pivot) {
right.push(arr[i])
} else {
left.push(arr[i])
}
}
return [...quickSort(left), pivot, ...quickSort(right)]
}
console.log(quickSort(arr));
//////////////////////////////////////////////////////////////selection sort/////////////////////////////
//selection sort
for (let i = 0; i < arr.length; i++) {
let min = i
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j
}
}
let temp = arr[min]
arr[min] = arr[i]
arr[i] = temp
}
console.log(arr);
//////////////////////////////////////////////////////////////insertion sort//////////////////////////////
// insertion sort
for (let i = 1; i < arr.length; i++) {
let numToInsert = arr[i]
let j = i - 1
while (j >= 0 && arr[j] > numToInsert) {
arr[j + 1] = arr[j]
j--
}
arr[j + 1] = numToInsert
}
console.log(arr);
//////////////////////////////////////////////////////////////bubble sort/////////////////////////////////////
// bubble sort
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
console.log(arr);
//////////////////////////////////////////LEET CODE:344. Reverse String////////////////////////////////////////
var reverseString = function(s) {
let start = 0;
let end = s.length - 1;
while (start <= end) {
[s[start], s[end]] = [s[end], s[start]];
start++;
end--;
}
console.log(s)
};
reverseString(['h','e','l','l','o']);