From 755add1ea0ae4cbac5756666e505706fc1dfc670 Mon Sep 17 00:00:00 2001 From: ascpixel Date: Fri, 2 Oct 2020 22:34:38 +0200 Subject: [PATCH 1/3] Comb sort implementation in JavaScript This commit implements comb sort in JavaScript, alongside comments. Resources used: - https://en.wikipedia.org/wiki/Comb_sort --- js/ascpixel_combSort.js | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 js/ascpixel_combSort.js diff --git a/js/ascpixel_combSort.js b/js/ascpixel_combSort.js new file mode 100644 index 00000000..d582bcb6 --- /dev/null +++ b/js/ascpixel_combSort.js @@ -0,0 +1,59 @@ +// Comb sort is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz and Artur Borowy in 1980. +// Comb sort improves on bubble sort. +// https://en.wikipedia.org/wiki/Comb_sort + +/** + * Sorts an array of numbers using comb sort. + * @param {Array} data + */ +function combSort(data){ + const length = data.length; + + const shrink = 1.3; // Set the gap shrink factor + let _gap = length; // Initialize gap size + const out = [...data]; // Copy data to the output variable + let sorted = false; + + while(!sorted){ + // Update the gap value for a next comb + _gap /= shrink; + let gap = Math.round(_gap); + if(gap <= 1){ + sorted = true; // If there are no swaps this pass, we are done + gap = 1; + } + + // A single "comb" over the input list + for(let i = 0; i < length - gap; i++){ + const sm = gap + i; + + if(out[i] > out[sm]){ + // Swap out[i] and out[sm] + // out[i] out[sm] + // | | + // a b + + const a = out[i]; + const b = out[sm]; + + // a b + // | | + // out[sm] out[i] + + out[sm] = a; + out[i] = b; + sorted = false; + + // If this assignment never happens within the loop, + // then there have been no swaps and the list is sorted. + } + } + } + + return out; +} + +// Test code. +console.log(combSort([3, 5, 8, 1, 2, 123, 43, 86])); +console.log(combSort([1, 2, 3])); +console.log(combSort([3, 6, 3, 2, 7])); \ No newline at end of file From cb1ebc8e17fd150bc1e7a27ebb2e72dfa8da8a57 Mon Sep 17 00:00:00 2001 From: ascpixel Date: Sat, 3 Oct 2020 14:43:47 +0200 Subject: [PATCH 2/3] Add class to determine if a year is a leap year or not in C# C# program to determine if a year is a leap year or not. Optimized to have the least IL instructions. --- csharp/ascpixel_leapYear.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 csharp/ascpixel_leapYear.cs diff --git a/csharp/ascpixel_leapYear.cs b/csharp/ascpixel_leapYear.cs new file mode 100644 index 00000000..8d0088c1 --- /dev/null +++ b/csharp/ascpixel_leapYear.cs @@ -0,0 +1,33 @@ +// C# program to determine if a year is a leap year or not. +// Optimized to have the least IL instructions. + +using System; + +static class Program +{ + /// + /// Main entry-point method. + /// + public static void Main() + { + int year; + + Console.Write("Enter the year you want to check: "); + year = int.Parse(Console.ReadLine()); + + Console.Write("Year " + year + " is "); + + if (!((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) + Console.Write("not "); + + Console.WriteLine("a leap year."); + } + + /* + * Input: 2020 + * Output: Year 2020 is not a leap year. + * + * Input: 1996 + * Output: Year 1996 is a leap year. + */ +} \ No newline at end of file From e65a798ed3d4b244baaa0ee0a891a31b42a86081 Mon Sep 17 00:00:00 2001 From: ascpixel Date: Sat, 3 Oct 2020 14:44:49 +0200 Subject: [PATCH 3/3] Revert "Add class to determine if a year is a leap year or not in C#" This reverts commit cb1ebc8e17fd150bc1e7a27ebb2e72dfa8da8a57. --- csharp/ascpixel_leapYear.cs | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 csharp/ascpixel_leapYear.cs diff --git a/csharp/ascpixel_leapYear.cs b/csharp/ascpixel_leapYear.cs deleted file mode 100644 index 8d0088c1..00000000 --- a/csharp/ascpixel_leapYear.cs +++ /dev/null @@ -1,33 +0,0 @@ -// C# program to determine if a year is a leap year or not. -// Optimized to have the least IL instructions. - -using System; - -static class Program -{ - /// - /// Main entry-point method. - /// - public static void Main() - { - int year; - - Console.Write("Enter the year you want to check: "); - year = int.Parse(Console.ReadLine()); - - Console.Write("Year " + year + " is "); - - if (!((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) - Console.Write("not "); - - Console.WriteLine("a leap year."); - } - - /* - * Input: 2020 - * Output: Year 2020 is not a leap year. - * - * Input: 1996 - * Output: Year 1996 is a leap year. - */ -} \ No newline at end of file