Skip to content

Commit 46249bc

Browse files
add quick sort
1 parent b498873 commit 46249bc

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
2. [Сортировка выбором / Selection Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/selection-sort/selection-sort.js) | [Инфо](https://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D0%B2%D1%8B%D0%B1%D0%BE%D1%80%D0%BE%D0%BC)
88
3. [Сортировка подсчётом / Counting Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/counting-sort/counting-sort.js) | [Инфо](https://www.youtube.com/watch?v=6dk_csyWif0)
99
4. [Сортировка чёт-нечет / OddEven Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/odd-even-sort/odd-even-sort.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0_%D1%87%D1%91%D1%82-%D0%BD%D0%B5%D1%87%D0%B5%D1%82)
10+
5. [Быстрая сортировка / Quick Sort](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/sorts/quick-sort/quick-sort.js) | [Инфо](https://habrahabr.ru/sandbox/29775/)
1011

1112
## Структуры данных / Data Structures
1213
1. [Стек / Stack](https://github.com/dmitrymorozoff/algorithms-in-javascript/blob/master/source/structures/stack.js) | [Инфо](https://ru.wikipedia.org/wiki/%D0%A1%D1%82%D0%B5%D0%BA)

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<script src="./source/sorts/counting-sort/counting-sort.js" type="modul≠e"></script>
1414
<script src="./source/sorts/selection-sort/selection-sort.js" type="module"></script>
1515
<script src="./source/sorts/odd-even-sort/odd-even-sort.js" type="module"></script>
16+
<script src="./source/sorts/quick-sort/quick-sort.js" type="module"></script>
1617
<script src="./source/structures/stack.js" type="module"></script>
1718
<script src="./source/structures/queue.js" type="module"></script>
1819
<script src="./source/structures/singly-list.js" type="module"></script>
@@ -23,7 +24,6 @@
2324
<script src="./source/cryptographic/caesar-cipher/caesar-cipher.js" type="module"></script>
2425
<script src="./source/hash-table/linear-hashing/linear-hashing.js" type="module"></script>
2526
<script src="./source/other/sieve-of-eratosthenes.js" type="module"></script>
26-
<script src="./source/main.js" type="module"></script>
2727
</body>
2828

2929
</html>

source/sorts/quick-sort/quick-sort.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { swap } from "../../utils/helpers.js";
2+
3+
/**
4+
* Функция разделения массива
5+
* @param {any} array Входящий массив
6+
* @param {any} begin Левая граница
7+
* @param {any} end Правая граница
8+
* @returns
9+
*/
10+
function division(array, begin, end) {
11+
let left = begin;
12+
let right = end;
13+
let pivot = array[Math.floor((left + right) / 2)];
14+
while (left <= right) {
15+
// Находим первое число слева больше опорного элемента
16+
while (array[left] < pivot) {
17+
left++;
18+
}
19+
// Находим первое число справа меньше опорного элемента
20+
while (array[right] > pivot) {
21+
right--;
22+
}
23+
if (left <= right) {
24+
// Меняем местами найденные элементы массива
25+
swap(array, left, right);
26+
// Продвигаем левый указатель на единицу вправо
27+
left++;
28+
// Продвигаем правый указатель на единицу влево
29+
right--;
30+
}
31+
// Повторяем алгоритм
32+
}
33+
return left;
34+
}
35+
36+
/**
37+
* Функция быстрой сортировки
38+
* @param {any} array Массив который требуется отсортировать
39+
* @param {any} left Индекс первого элемента массива
40+
* @param {any} right Индекс последнего элемента массива
41+
* @returns Отсортированный массив
42+
*/
43+
export default function quickSort(array, left, right) {
44+
var index;
45+
if (array.length > 1) {
46+
index = division(array, left, right);
47+
if (left < index - 1) {
48+
quickSort(array, left, index - 1);
49+
}
50+
if (index < right) {
51+
quickSort(array, index, right);
52+
}
53+
}
54+
return array;
55+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import quickSort from "./quick-sort";
2+
3+
describe("quick-sort", () => {
4+
test("should return [1,2,3,4,5,6] if sort", () => {
5+
const testArray = [3, 1, 2, 4, 6, 5];
6+
expect(quickSort(testArray, 0, testArray.length - 1)).toEqual([
7+
1,
8+
2,
9+
3,
10+
4,
11+
5,
12+
6
13+
]);
14+
});
15+
16+
test("should return [12,22,44,45,56,76] if sort", () => {
17+
const testArray = [22, 12, 45, 44, 76, 56];
18+
expect(quickSort(testArray, 0, testArray.length - 1)).toEqual([
19+
12,
20+
22,
21+
44,
22+
45,
23+
56,
24+
76
25+
]);
26+
});
27+
});

0 commit comments

Comments
 (0)