Implementing comb sorting based on C#

Why is it named comb? Maybe each comb has its own gap. Big combs have a larger gap, and small combs have a smaller gap. In the previous article, we saw that cocktail sorting made some optimizations on bubble sorting, turning one-way comparison into two-way. Similarly, comb sorting here also made some optimizations on bubble sorting.
In bubble sorting, our choice is to compare two adjacent numbers, that is, their gap is 1. In fact, comb sorting puts forward a different point of view. If the gap here is set to a certain size, the efficiency must be gap=1. Much more efficient.
Let's take a look at the specific idea. Comb sorting has a ratio value of 1.3. After each comparison, this 1.3 will be used to decrement the gap until gap=1 and it becomes bubble sorting. This algorithm is better than bubble sorting. The efficiency is much higher, and the time complexity is O(N2/2p). The p here is an increment. Is it a bit similar to Hill sorting?
For example, there is a set of data below: initialized gap = list.count/1.3, and then use this gap as an array subscript to compare the size across numbers. If the former is greater than the latter, it will be exchanged. After each sorting is completed, it will be divided by 1.3, and finally Keep dividing until gap=1.
image.png
Finally, our array is sorted. Let’s look at the code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Xml.Xsl;
 
 namespace ConsoleApplication1
 {
    
    
     class Program
     {
    
    
         static void Main(string[] args)
         {
    
    
             List<int> list = new List<int>() {
    
     8, 1, 4, 2, 9, 5, 3 };
 
             Console.WriteLine("\n排序前 => {0}\n", string.Join(",", list));
 
             list = CombSort(list);
 
             Console.WriteLine("\n排序后 => {0}\n", string.Join(",", list));
 
             Console.Read();
         }
 
         /// <summary>
         /// 梳排序
         /// </summary>
         /// <param name="list"></param>
         /// <returns></returns>
         static List<int> CombSort(List<int> list)
         {
    
    
             //获取最佳排序尺寸: 比率为 1.3
             var step = (int)Math.Floor(list.Count / 1.3);
 
             while (step >= 1)
             {
    
    
                 for (int i = 0; i < list.Count; i++)
                 {
    
    
                     //如果前者大于后者,则进行交换
                     if (i + step < list.Count && list[i] > list[i + step])
                     {
    
    
                         var temp = list[i];
 
                         list[i] = list[i + step];
 
                         list[i + step] = temp;
                     }
 
                     //如果越界,直接跳出
                     if (i + step > list.Count)
                         break;
                 }
 
                 //在当前的step在除1.3
                 step = (int)Math.Floor(step / 1.3);
             }
 
             return list;
         }
     }
 }

image.png

Guess you like

Origin blog.csdn.net/s1t16/article/details/134640675