diff --git a/problem-1/problem-1.test.js b/problem-1/problem-1.test.js index f54f840..6149459 100644 --- a/problem-1/problem-1.test.js +++ b/problem-1/problem-1.test.js @@ -1,4 +1,21 @@ const bubbleSort = (array) => { + const len = array.length; + + for (let i = 0; i < len; i++) { + let isChanged = false; + for (let j = 0; j < len - i; j++) { + if (array[j] > array[j + 1]) { + isChanged = true; + [array[j], array[j + 1]] = [array[j + 1], array[j]]; + } + } + + if (!isChanged) { + return array; + } + } + + return array; }; test.each([ diff --git a/problem-2/problem-2.test.js b/problem-2/problem-2.test.js index 1e1fe4f..7d17ece 100644 --- a/problem-2/problem-2.test.js +++ b/problem-2/problem-2.test.js @@ -1,4 +1,17 @@ const selectionSort = (array) => { + for (let i = 0; i < array.length; i++) { + let min = i; + + for (let j = i + 1; j < array.length; j++) { + if (array[j] < array[min]) { + min = j; + } + } + + [array[i], array[min]] = [array[min], array[i]]; + } + + return array; }; test.each([ diff --git a/problem-3/problem-3.test.js b/problem-3/problem-3.test.js index e4450b5..e96ea76 100644 --- a/problem-3/problem-3.test.js +++ b/problem-3/problem-3.test.js @@ -1,4 +1,12 @@ const insertionSort = (array) => { + for (let i = 1; i < array.length; i++) { + for (let j = i; j > 0; j--) { + if (array[j] < array[j - 1]) { + [array[j], array[j - 1]] = [array[j - 1], array[j]]; + } + } + } + return array; }; test.each([ diff --git a/problem-4/problem-4.test.js b/problem-4/problem-4.test.js index 069ccdf..786938b 100644 --- a/problem-4/problem-4.test.js +++ b/problem-4/problem-4.test.js @@ -1,4 +1,19 @@ const shellSort = (array) => { + let h = Math.floor(array.length / 2); + + while (h >= 1) { + for (let i = h; i < array.length; i++) { + for (let j = i; j >= 0; j = j - h) { + if (array[j] < array[j - h]) { + [array[j], array[j - h]] = [array[j - h], array[j]]; + } + } + } + + h = Math.floor(h / 2); + } + + return array; }; test.each([ diff --git a/problem-5/problem-5.test.js b/problem-5/problem-5.test.js index 124ce9e..2542ed4 100644 --- a/problem-5/problem-5.test.js +++ b/problem-5/problem-5.test.js @@ -1,4 +1,32 @@ -const mergeSort = (array) => { +const merge = (array, start, mid, end) => { + let left = start; + let right = mid + 1; + + const temp = [...array]; + + for (let i = start; i <= end; i++) { + if (left > mid) { + array[i] = temp[right++]; + } else if (right > end) { + array[i] = temp[left++]; + } else if (temp[left] < temp[right]) { + array[i] = temp[left++]; + } else { + array[i] = temp[right++]; + } + } +}; + +const mergeSort = (array, start = 0, end = array.length - 1) => { + if (start >= end) { + return; + } + + const mid = Math.floor((start + end) / 2); + + mergeSort(array, start, mid); + mergeSort(array, mid + 1, end); + merge(array, start, mid, end); }; test.each([ diff --git a/problem-6/problem-6.test.js b/problem-6/problem-6.test.js index f335a1f..d1306af 100644 --- a/problem-6/problem-6.test.js +++ b/problem-6/problem-6.test.js @@ -1,4 +1,45 @@ -const quickSort = (array) => { +const partition = (array, start, end) => { + let left = start + 1; + let right = end; + + const pivot = array[start]; + + while (true) { + while (array[left] < pivot) { + if (left === end) { + break; + } + + left++; + } + + while (pivot < array[right]) { + if (right === start) { + break; + } + + right--; + } + + if (left >= right) { + break; + } + + [array[left], array[right]] = [array[right], array[left]]; + } + + [array[start], array[right]] = [array[right], array[start]]; + return right; +}; + +const quickSort = (array, start = 0, end = array.length - 1) => { + if (start >= end) { + return; + } + + const j = partition(array, start, end); + quickSort(array, start, j - 1); + quickSort(array, j + 1, end); }; test.each([ diff --git a/problem-7/problem-7.test.js b/problem-7/problem-7.test.js index 8d36fec..900eacc 100644 --- a/problem-7/problem-7.test.js +++ b/problem-7/problem-7.test.js @@ -1,4 +1,32 @@ +const sink = (array, i, len) => { + while (2 * i <= len) { + let j = 2 * i; + + if (j < len && array[j] < array[j + 1]) { + j++; + } + + if (!(array[i] < array[j])) { + break; + } + + [array[i], array[j]] = [array[j], array[i]]; + i = j; + } +}; + const heapSort = (array) => { + let len = array.length - 1; + + for (let i = Math.floor(len / 2); i >= 1; i--) { + sink(array, i, len); + } + + while (len > 1) { + [array[1], array[len]] = [array[len], array[1]]; + len--; + sink(array, 1, len); + } }; test.each([