From a854254bc8370f16316b6da5970711c580797d3c Mon Sep 17 00:00:00 2001 From: Shreyash Date: Sun, 16 Oct 2022 20:27:47 +0530 Subject: [PATCH 1/2] Comb sort implementation in Javascript added --- Sorting/Comb Sort/combSort.js | 77 +++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Sorting/Comb Sort/combSort.js diff --git a/Sorting/Comb Sort/combSort.js b/Sorting/Comb Sort/combSort.js new file mode 100644 index 0000000..a79d5dd --- /dev/null +++ b/Sorting/Comb Sort/combSort.js @@ -0,0 +1,77 @@ + +// Algorithm + +// Comb Sort is an advancement of Bubble Sort. Bubble sort removes inversions one by one by comparing adjacent values. + +// Comb Sort uses 'gap size'>1 to improve Bubble sort. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. Thus Comb Sort removes more than one inversion count with one swap and performs better than Bubble Sort. + +// The shrink factor has been empirically found to be 1.3 (by testing Combsort on over 200,000 random lists) [Source: Wiki] + +// Although it works better than Bubble Sort on average, worst-case remains O(n2).Comb Sort is mainly an improvement over Bubble Sort. Bubble sort always compares adjacent values. So all inversions are removed one by one. Comb Sort improves on Bubble Sort by using a gap of the size of more than 1. The gap starts with a large value and shrinks by a factor of 1.3 in every iteration until it reaches the value 1. + +// Thus Comb Sort removes more than one inversion count with one swap and performs better than Bubble Sort. +// The shrink factor has been empirically found to be 1.3 (by testing Combsort on over 200,000 random lists) [Source: Wiki] +// Although it works better than Bubble Sort on average, worst-case remains O(n2). + + + + + + + +// Code for Comb Sort + +const combSort=(array)=> +{ + const is_sorted=(array)=>{ + let sorted = true; + for (let i = 0; i < array.length - 1; i++) { + if (array[i] > array[i + 1]) { + sorted = false; + break; + } + } + return sorted; + } + + var iteration_count = 0; + var gap = array.length - 2; + var decrease_factor = 1.25; + + // Repeat iterations Until array is not sorted + + while (!is_sorted(array)) + { + // If not first gap Calculate gap + if (iteration_count > 0) + 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 <= array.length - 1) + { + // Swap the elements if they are not ordered + + if (array[front] > array[back]) + { + var temp = array[front]; + array[front] = array[back]; + array[back] = temp; + } + + // Increment and re-run swapping + + front += 1; + back += 1; + } + iteration_count += 1; + } + return array; +} + + var arra = [3, 0, 2, 5, -1, 4, 1]; +console.log("Original Array Elements"); +console.log(arra); +console.log("Sorted Array Elements"); +console.log(combsort(arra)); From 9132d71a8f728e820b00e1a8c2f93933953fde17 Mon Sep 17 00:00:00 2001 From: Shreyash Date: Wed, 19 Oct 2022 20:04:45 +0530 Subject: [PATCH 2/2] File name changed. combsort.js -> in_javascript.js --- Sorting/Comb Sort/{combSort.js => in_javascript.js.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Sorting/Comb Sort/{combSort.js => in_javascript.js.js} (100%) diff --git a/Sorting/Comb Sort/combSort.js b/Sorting/Comb Sort/in_javascript.js.js similarity index 100% rename from Sorting/Comb Sort/combSort.js rename to Sorting/Comb Sort/in_javascript.js.js