Skip to content

Commit a810eb9

Browse files
Hui ChenHui Chen
authored andcommitted
Added Comb Sort
1 parent 5d329b5 commit a810eb9

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"quick": "Quicksort",
4343
"heap" : "Heapsort",
4444
"shell": "Shellsort",
45-
"cycle": "Cycle Sort"
45+
"cycle": "Cycle Sort",
46+
"comb": "Comb Sort"
4647
}
4748
},
4849
"string": {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
logger._print('original array = [' + D.join(', ') + ']');
2+
var N = D.length;
3+
var swapped;
4+
var gap = N; // initialize gap size
5+
var shrink = 1.3; // set the gap shrink factor
6+
7+
do{
8+
// update the gap value for the next comb.
9+
gap = Math.floor( gap/shrink );
10+
if( gap < 1 ){
11+
// minimum gap is 1
12+
gap = 1;
13+
}
14+
15+
swapped = false; // initialize swapped
16+
// a single comb over the input list
17+
for( var i=0; i+gap < N; i++ ){
18+
if( D[i] > D[i+gap] ){
19+
logger._print('swap ' + D[i] + ' and ' + D[i+gap]); // log swap event
20+
21+
var temp = D[i];
22+
D[i] = D[i+gap];
23+
D[i+gap] = temp;
24+
25+
tracer._notify(i, D[i])._notify(i+gap, D[i+gap])._wait();
26+
tracer._denotify(i)._denotify(i+gap);
27+
28+
swapped = true; // Flag swapped has happened and list is not guaranteed sorted
29+
}
30+
} // End of combing
31+
} while( gap!=1 || swapped )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var tracer = new Array1DTracer();
2+
var logger = new LogTracer();
3+
var D = Array1D.random(15);
4+
tracer._setData(D);

algorithm/sorting/comb/desc.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Comb Sort": "Comb sort is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz in 1980. Later it was rediscovered by Stephen Lacey and Richard Box in 1991. Comb sort improves on bubble sort. <br /><br />The basic idea is to eliminate turtles, or small values near the end of the list, since in a bubble sort these slow the sorting down tremendously. Rabbits, large values around the beginning of the list, do not pose a problem in bubble sort.",
3+
"Complexity": {
4+
"time": "worst O(n<sup>2</sup>), best O(n log n), average O(n<sup>2</sup>/2<sup>p</sup>, where p is the number of increment)",
5+
"space": "worst O(1) auxiliary"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Comb_sort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Bubble sort"
12+
}
13+
}

0 commit comments

Comments
 (0)