• C#实现所有经典排序算法(选择排序,冒泡排序,快速排序,插入排序,希尔排序)


    C#实现所有经典排序算法
    1、选择排序

    class SelectionSorter    
    {    
        
    private int min;    
        
    public void Sort(int[] arr)    
        {    
            
    for (int i = 0; i < arr.Length - 1++i)    
            {    
                min 
    = i;    
                
    for (int j = i + 1; j < arr.Length; ++j)    
                {    
                    
    if (arr[j] < arr[min])    
                        min 
    = j;    
                }    
                
    int t = arr[min];    
                arr[min] 
    = arr[i];    
                arr[i] 
    = t;    
            }    
        }    
        
    static void Main(string[] args)    
        {    
            
    int[] array = new int[] { 1536105592871234753347 };    
            SelectionSorter s 
    = new SelectionSorter();    
            s.Sort(array);    
            
    foreach (int m in array)    
                Console.WriteLine(
    "{0}", m);    
        }    

    2、冒泡排序
    class EbullitionSorter    
    {    
        
    public void Sort(int[] arr)    
        {    
            
    int i, j, temp;    
            
    bool done = false;    
            j 
    = 1;    
            
    while ((j < arr.Length) && (!done))//判断长度    
            {    
                done 
    = true;    
                
    for (i = 0; i < arr.Length - j; i++)    
                {    
                    
    if (arr[i] > arr[i + 1])    
                    {    
                        done 
    = false;    
                        temp 
    = arr[i];    
                        arr[i] 
    = arr[i + 1];//交换数据    
                        arr[i + 1= temp;    
                    }    
                }    
                j
    ++;    
            }    
        }    
       
        
    static void Main(string[] args)    
        {    
            
    int[] array = new int[] { 1536105592871234753347 };    
            EbullitionSorter e 
    = new EbullitionSorter ();    
            e.Sort(array);    
            
    foreach (int m in array)    
                Console.WriteLine(
    "{0}", m);    
       
        }    

    3、快速排序

    class QuickSorter    
    {    
        
    private void swap(ref int l, ref int r)    
        {    
            
    int temp;    
            temp 
    = l;    
            l 
    = r;    
            r 
    = temp;    
        }    
        
    public void Sort(int[] list, int low, int high)    
        {    
            
    int pivot;//存储分支点    
            int l, r;    
            
    int mid;    
            
    if (high <= low)    
                
    return;    
            
    else if (high == low + 1)    
            {    
                
    if (list[low] > list[high])    
                    swap(
    ref list[low], ref list[high]);    
                
    return;    
            }    
            mid 
    = (low + high) >> 1;    
            pivot 
    = list[mid];    
            swap(
    ref list[low], ref list[mid]);    
            l 
    = low + 1;    
            r 
    = high;    
            
    do   
            {    
            
    while (l <= r && list[l] < pivot)    
                l
    ++;    
            
    while (list[r] >= pivot)    
                r
    --;    
                
    if (l < r)    
                    swap(
    ref list[l], ref list[r]);    
            } 
    while (l < r);    
            list[low] 
    = list[r];    
            list[r] 
    = pivot;    
            
    if (low + 1 < r)    
                Sort(list, low, r 
    - 1);    
            
    if (r + 1 < high)    
                Sort(list, r 
    + 1, high);    
        }    
       
        
    static void Main(string[] args)    
        {    
            
    int[] iArrary = new int[] { 1536105592871234753347 };    
            QuickSorter q 
    = new QuickSorter();    
            q.Sort(iArrary, 
    013);    
            
    for (int m = 0; m <= 13; m++)    
                Console.WriteLine(
    "{0}", iArrary[m]);    
        }    
    }    

    4、插入排序

    public class InsertionSorter    
    {    
        
    public void Sort(int[] arr)    
        {    
            
    for (int i = 1; i < arr.Length; i++)    
            {    
                
    int t = arr[i];    
                
    int j = i;    
                
    while ((j > 0&& (arr[j - 1> t))    
                {    
                    arr[j] 
    = arr[j - 1];//交换顺序    
                    --j;    
                }    
                arr[j] 
    = t;    
            }    
        }    
        
    static void Main(string[] args)    
        {    
            
    int[] array = new int[] { 1536105592871234753347 };    
            InsertionSorter i 
    = new InsertionSorter();    
            i.Sort(array);    
            
    foreach (int m in array)    
                Console.WriteLine(
    "{0}", m);     
        }    
    }    

    5、希尔排序

    public class ShellSorter    
    {    
        
    public void Sort(int[] arr)    
        {    
            
    int inc;    
            
    for (inc = 1; inc <= arr.Length / 9; inc = 3 * inc + 1) ;    
            
    for (; inc > 0; inc /= 3)    
            {    
                
    for (int i = inc + 1; i <= arr.Length; i += inc)    
                {    
                    
    int t = arr[i - 1];    
                    
    int j = i;    
                    
    while ((j > inc) && (arr[j - inc - 1> t))    
                    {    
                        arr[j 
    - 1= arr[j - inc - 1];//交换数据    
                        j -= inc;    
                    }    
                    arr[j 
    - 1= t;    
                }    
            }    
        }    
       
        
    static void Main(string[] args)    
        {    
            
    int[] array = new int[] { 1536105592871234753347 };    
            ShellSorter s 
    = new ShellSorter();    
            s.Sort(array);    
            
    foreach (int m in array)    
                Console.WriteLine(
    "{0}", m);     
        }    
    }  

  • 相关阅读:
    centOS7 完整克隆后网络配置
    索引角度理解innodb/myisam原理
    JUC 7大并发容器原理详解、及使用场景
    MySQL索引列没有走索引?
    Java 各种并发锁 从 synchronized 到 CAS 和 AQS
    JDK1.8 HashMap两种扩容的情况和转红黑树
    开发自己的网上支付案例代码(易宝支付php)
    redis学习基础(二)
    redis使用基础(一)
    直角三角形打印
  • 原文地址:https://www.cnblogs.com/soso_ak/p/2022072.html
Copyright © 2020-2023  润新知