Skip to content

Commit 8aecf0b

Browse files
committed
merge sort
1 parent cf4a5d7 commit 8aecf0b

2 files changed

Lines changed: 40 additions & 58 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// The Merge sort algorithm
2+
3+
// Definition
4+
5+
// Merge sort is a divide and conquer algorithm.
6+
// Conceptually, a Merge sort works as follows:
7+
// 1) Divide the unsorted list into n sublists, each containing 1 element
8+
// (a list of 1 element is considered sorted),
9+
// 2) Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining.
10+
// This will be the sorted list. From Wikipedia
11+
12+
// array to sort
13+
var array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
14+
15+
// top-down implementation
16+
function mergeSortTopDown(array) {
17+
if(array.length < 2) {
18+
return array;
19+
}
20+
21+
var middle = Math.floor(array.length / 2);
22+
var left = array.slice(0, middle);
23+
var right = array.slice(middle);
24+
25+
return mergeTopDown(mergeSortTopDown(left), mergeSortTopDown(right));
26+
}
27+
function mergeTopDown(left, right) {
28+
var array = [];
29+
30+
while(left.length && right.length) {
31+
if(left[0] < right[0]) {
32+
array.push(left.shift());
33+
} else {
34+
array.push(right.shift());
35+
}
36+
}
37+
return array.concat(left.slice()).concat(right.slice());
38+
}
39+
40+
console.log(mergeSortTopDown(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

algorithms/sorting_algo/quicksort.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,3 @@ function quicksortBasic(array) {
3333

3434
console.log(quicksortBasic(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
3535

36-
// swap function helper
37-
function swap(array, i, j) {
38-
var temp = array[i];
39-
array[i] = array[j];
40-
array[j] = temp;
41-
}
42-
43-
// classic implementation (with Hoare or Lomuto partition scheme, you can comment either one method or the other to see the difference)
44-
function quicksort(array, left, right) {
45-
left = left || 0;
46-
right = right || array.length - 1;
47-
48-
// var pivot = partitionLomuto(array, left, right); // you can play with both partition
49-
var pivot = partitionHoare(array, left, right); // you can play with both partition
50-
51-
if(left < pivot - 1) {
52-
quicksort(array, left, pivot - 1);
53-
}
54-
if(right > pivot) {
55-
quicksort(array, pivot, right);
56-
}
57-
return array;
58-
}
59-
// Lomuto partition scheme, it is less efficient than the Hoare partition scheme
60-
function partitionLomuto(array, left, right) {
61-
var pivot = right;
62-
var i = left;
63-
64-
for(var j = left; j < right; j++) {
65-
if(array[j] <= array[pivot]) {
66-
swap(array, i , j);
67-
i = i + 1;
68-
}
69-
}
70-
swap(array, i, j);
71-
return i;
72-
}
73-
// Hoare partition scheme, it is more efficient than the Lomuto partition scheme because it does three times fewer swaps on average
74-
function partitionHoare(array, left, right) {
75-
var pivot = Math.floor((left + right) / 2 );
76-
77-
while(left <= right) {
78-
while(array[left] < array[pivot]) {
79-
left++;
80-
}
81-
while(array[right] > array[pivot]) {
82-
right--;
83-
}
84-
if(left <= right) {
85-
swap(array, left, right);
86-
left++;
87-
right--;
88-
}
89-
}
90-
return left;
91-
}
92-
93-
console.log(quicksort(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

0 commit comments

Comments
 (0)