2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,47 @@
|
|||
// Node 5.4.1 tested implementation (ES6)
|
||||
function is_array_sorted(arr) {
|
||||
var sorted = true;
|
||||
for (var i = 0; i < arr.length - 1; i++) {
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
sorted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
// Array to sort
|
||||
var arr = [4, 9, 0, 3, 1, 5];
|
||||
|
||||
var iteration_count = 0;
|
||||
var gap = arr.length - 2;
|
||||
var decrease_factor = 1.25;
|
||||
|
||||
// Until array is not sorted, repeat iterations
|
||||
while (!is_array_sorted(arr)) {
|
||||
// If not first gap
|
||||
if (iteration_count > 0)
|
||||
// Calculate gap
|
||||
gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor);
|
||||
|
||||
// Set front and back elements and increment to a gap
|
||||
var front = 0;
|
||||
var back = gap;
|
||||
while (back <= arr.length - 1) {
|
||||
// If elements are not ordered swap them
|
||||
if (arr[front] > arr[back]) {
|
||||
var temp = arr[front];
|
||||
arr[front] = arr[back];
|
||||
arr[back] = temp;
|
||||
}
|
||||
|
||||
// Increment and re-run swapping
|
||||
front += 1;
|
||||
back += 1;
|
||||
}
|
||||
iteration_count += 1;
|
||||
}
|
||||
|
||||
// Print the sorted array
|
||||
console.log(arr);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue