五中常见的排序排序算法

根据大话数据结构,使用C#实现

冒泡排序、简单选择排序、直接插入排序、堆排序、快速排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numArray = new int[] {6,5,4,3,2,1};

            //MaoPaoSort(numArray);//最简单排序算法
            //MaoPaoSortUP(numArray);//冒泡排序算法
            //MaoPaoSortUUP(numArray);//添加标记的冒泡排序算法

            //SeleceSort(numArray);//简单选择排序

            //InsertSort(numArray);//直接插入排序

            //HeapSort(numArray);//堆排序

            QuickSort(numArray);//快速排序

            for (int i = 0; i < numArray.Length;i++ )
            {
                Console.Write(numArray[i]+" ");
            }




            Console.ReadKey();
        }
        //最简单排序算法
        //
        public static void MaoPaoSort(int[]array) {
            int tempMin;
            for (int i = 0; i < array.Length; i++)
            {
                for (int j = i; j < array.Length; j++)
                {
                    if (array[i] > array[j]) 
                    {
                        tempMin = array[i];
                        array[i]=array[j];
                        array[j] = tempMin;
                    }
                }
            }
        }
        //冒泡排序算法
        //从最下面开始,如果相邻两数,下面的小于上面的数,则交换两数位置,每次结束,较小的数均向上移动
        public static void MaoPaoSortUP(int[] array) {
            int tempMin;
            for (int i = 0; i <array.Length ; i++)
            {
                for (int j = array.Length-2; j >=i ; j--)
                {
                    if (array[j] > array[j+1]) 
                    {
                        tempMin=array[j+1];
                        array[j+1]=array[j];
                        array[j] = tempMin;
                    }
                }
            }
        }
        //增加标记的冒泡排序算法
        //若在某次排序中,未发生交换,则数据已经是有序的了,终止排序
        public static void MaoPaoSortUUP(int[]array) {
            int i, j,tempMin;
            bool isChange=true;
            for (i = 0; i < array.Length&&isChange; i++)
            {
                for ( j = array.Length-2; j >=i; j--)
                {
                    if(array[j] > array[j+1])
                    {
                        tempMin = array[j+1];
                        array[j+1]=array[j];
                        array[j] = tempMin;
                        isChange = true;
                    }
                }
            }
            
        }

        //简单选择排序
        //循环中,更新较小值的下标,循环结束后,只进行一次数据交换
        public static void SeleceSort(int[] array) {
            int minIndex,tempMin;
            for (int i = 0; i < array.Length-1; i++)
            {
                minIndex = i;
                for (int j =i+1; j <= array.Length-1; j++)
                {
                    if(array[i]>array[j]){
                        minIndex = j; 
                    }
                }
                if (minIndex != i)
                {
                    tempMin = array[i];
                    array[i] = array[minIndex];
                    array[minIndex] = tempMin;
                }

            }
        }

        //直接插入排序
        //将记录插入到已经排好序的表中
        public static void InsertSort(int[]array) {
            int tempIndex=1;
  
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] < array[i - 1])
                {
                    int tempNum = array[i];
                    for (int j = i - 1; j >= 0 && tempNum < array[j]; j--)
                    {
                        array[j+1] = array[j];
                        tempIndex = j;
                    }
                    array[tempIndex] = tempNum;
                }
            }
        }

        //希尔排序

        //堆排序***************非常重要***************
       static void HeapSort(int[]array)
        {
            for (int i = (array.Length - 1) / 2; i >= 0; i--)//遍历这个数的所有非叶节点,挨个把所有的子树,变成大顶堆
            {
                HeapAdjust(array,i,array.Length-1);
            }
            for (int i = array.Length-1; i >0; i--)
            {
                Swap(array, 0, i);//将堆顶记录和当前未经排序的最后一个记录交换
                HeapAdjust(array, 0, i - 1);//将0到i-1重新调整为大顶堆
            }

        }

        static void HeapAdjust(int[]array,int s,int m ) {
            int temp;
            temp=array[s];    //存储array[s]的值到temp
            for (int j = 2*s; j <m; j*=2)
            {
                if(j<m&&array[j]<array[j+1]){
                    j++;//取得子节点s中较大的下标
                }
                if(temp>=array[j])  //若子节点中较大值小于temp则不需要交换
                {
                    break;
                }
                array[s]=array[j];//将较大子节点赋值给其父节点
                s = j;      //上一步代码中array[s]的值被覆盖,而array[s]原本的值应放在应放在下标j的地方,此处,没有立即更新值,而是更新了下标,,最终循环结束才更新值  类似简单选择排序
            }
            array[s] = temp;//更新数据
        }

        //归并排序


        //快速排序     ************非常重要***************
        //递归(分治算法)
        //选取一个关键字(选取中间大小的数效率高,,一般选取数组0号位置,所以不稳定)
        //然后通过一趟排序将待排记录分割成独立的两部分(比关键字大的,交换到右边,比关键字小的交换到左边)
        //分别对这两部分记录继续进行排序
        public static void QuickSort(int[]array) {
            QSort(array,0,array.Length-1);
        }
        public static void QSort(int[]array,int low,int high) {
            int pivot;
            if (low < high)//递归终止条件low==high
            {
                pivot = Partition(array,low,high);

                QSort(array,low,pivot-1);
                QSort(array,pivot+1,high);
            }
        }

        public static int Partition(int[]array,int low,int high) {
            int pivotkey;
            pivotkey = array[low];
            while (low<high)
            {
                while (low<high&&array[high]>=pivotkey)
                {
                    //若array[high]>=pivotkey,则右边的数大于pivotley,不需要数据交换操作

                    high--;    //high--,判断下一个
                }
                Swap(array,low,high); //将比枢轴记录小的记录交换到低端

                while (low<high&&array[low]<=pivotkey)
                {
                    low++;
                }
                Swap(array,low,high);
            }
            return low;
        }
        //交换两数
        public static  void Swap(int[]array,int i,int j) 
        {
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }


    } 
}

猜你喜欢

转载自blog.csdn.net/qq_35422344/article/details/86228633