几种排序算法的c#实现

来源:互联网 发布:淘宝云客服考试 编辑:程序博客网 时间:2024/06/06 01:38

几种排序算法的实现

冒泡排序

  • 在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整
  • 让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换.
using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Sort{    class Program    {        static void Main(string[] args)        {            List<Person> persons=new List<Person>()            {                new Person(4),                new Person(3),                new Person(2),                new Person(5)            };            BubbleSort(persons,Person.Compare);            foreach (Person person in persons)            {                Console.WriteLine(person);            }            Console.ReadKey();        }        static void BubbleSort<T>(List<T> list,Func<T,T,bool> Compare)        {            for (int i = 0; i < list.Count; i++)            {                for (int j = 0; j < list.Count - i - 1; j++)                {                    if (Compare(list[j],list[j+1]))                    {                        T temp = list[j+1];                        list[j + 1] = list[j];                        list[j] = temp;                    }                }            }        }        class Person        {            private int num;            public Person(int Num)            {                num = Num;            }            public static bool Compare(Person p1, Person p2)            {                if (p1.num > p2.num)                    return true;                else                {                    return false;                }            }            public override string ToString()            {                return num.ToString();            }        }    }}

选择排序

  • 在要排序的一组数中,选出最小的一个数与第一个位置的数交换;
  • 然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
using System.CodeDom;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace SelectSort{    class Program    {        static void Main(string[] args)        {            int[] array=new int[4]{2,3,4,1};            Sort(array);            foreach (int temp in array)            {                Console.WriteLine(temp);            }            Console.ReadKey();        }        static void Sort(int[] array)        {            for (int i = 0; i < array.Length-1; i++)            {                int iTemp = array[i];                int iPos = i;                for (int j = i+1; j < array.Length; j++)                {                    if (array[j] < iTemp)                    {                        iTemp = array[j];                        iPos = j;                    }                }                array[iPos] = array[i];                array[i] = iTemp;            }        }    }}

直接插入排序

  • 直接插入排序:在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经是排好顺序的
  • 现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。如此反复循环,直到全部排好顺序。
using System.Collections.Generic;using System.Globalization;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InsertSort{    class Program    {        static void Main(string[] args)        {            int[] array = new int[4] { 2, 3, 4, 1 };            //不能再静态方法里调用非静态成员            Sort(array);            foreach (int temp in array)            {                Console.WriteLine(temp);            }            Console.ReadKey();        }        static void Sort(int[] array)        {            //外层循环标示并决定待比较的数值(array[i])            for (int i = 1; i < array.Length; i++)            {                int j = i - 1;                int temp = array[i];                //内层循环为待比较的数值确定其最终位置,当前一数字(array[j])比待比较的数值大的情况下继续循环,                //直到找到比待比较数值小的并将待比较数值置入其后一位置,结束该次循环                for (; j >= 0 && array[j]>temp; j--)                {                    array[j + 1] = array[j];                }                array[j + 1] = temp;            }        }    }}

快速排序

  • 快速排序:快速排序是对冒泡排序的一种改进,它的基本思想是通过一趟扫描后,使得排序序列的长度能大幅减小
  • 快速排序通过一遍扫描,就能确保某个数(以它为基准点)的左边各数都比他小,右边各数都比它大,
  • 然后又用同样的方法处理它左右两边的数,直到基准点左右只有一个元素为止。
using System.Collections.Generic;using System.Diagnostics.Eventing.Reader;using System.Linq;using System.Text;using System.Threading.Tasks;namespace QuickSort{    class Program    {        static void Main(string[] args)        {            int[] array = new int[4] { 2, 3, 4, 1 };            //Sort(array,0,array.Length-1);            Stack<int> stack=new Stack<int>();            stack.Push(0);            stack.Push(array.Length-1);            while (stack.Count>0)            {                int low = stack.Pop();                int high = stack.Pop();                if (low > high)                {                    int temp = low;                    low = high;                    high = temp;                }                Sort(array,low,high,stack);            }            foreach (int temp in array)            {                Console.WriteLine(temp);            }            Console.ReadKey();        }        static int Partition(int[] array,int left,int right)        {            int pivot = array[left];//将首元素作为枢纽            while (left<right)            {                //从右到左,寻找首个小于pivot的元素                while (array[right] >= pivot && left < right)                    right--;                //执行到此,right已指向从右端其首个小于pivot的元素                //执行替换                array[left] = array[right];                //从左向右,寻找首个大于pivot的元素                while (array[left] <= pivot && left < right)                    left++;                //执行到此,left已指向从左端起首个大于pivot的元素                //执行替换                array[right] = array[left];            }            //退出while循环,执行至此,必定是left=right的情况            //left(或right)值向的即是枢纽位置,定位该趟排序的枢纽并将位置返回            array[left] = pivot;            return left;        }        //快速排序的递归实现        //static void Sort(int[] array, int left, int right)        //{        //    if (left<right)        //    {        //        int middle = Partition(array, left, right);        //        //对枢纽左端排序        //        Sort(array,left,middle-1);        //        //对枢纽右端排序        //        Sort(array,middle+1,right);        //    }        //}        //快速排序的非递归实现        static void Sort(int[] array, int left, int right, Stack<int> stack)        {             int low = left;             int high = right;             int temp = array[low];            while (high > low)            {                while (low < high && temp <= array[high])                {                    high--;                }                if (high > low)                {                    array[low] = array[high];                    array[high] = temp;                }                while (low < high && temp >= array[low])                {                    low++;                }                if (high > low)                {                    array[high] = array[low];                    array[low] = temp;                }                if (low == high)                {                    if (left < low - 1)                    {                        stack.Push(left);                        stack.Push(low - 1);                    }                    if (right > low + 1)                    {                        stack.Push(low + 1);                        stack.Push(right);                    }                }            }        }    }}
0 0
原创粉丝点击