冒泡,快速
交换排序 :冒泡排序,快速排序
选择排序:直接选择排序,堆排序
插入排序:直接插入排序,希尔排序
合并排序:合并排序
public class Program { static void Main(string[] args) { int[] array=new int[10]; Random rd = new Random(); for (int i = 0; i < array.Length; i++) { array[i] = rd.Next(0,10); } display(array); BubbleSort(array); display(array); Console.ReadKey(); } static void display(int[] array) { foreach (var item in array) { Console.Write(item + "\t"); } Console.WriteLine(); } static void BubbleSort(int[] array) { bool flag = true; int temp; for (int i = 0; i < array.Length-1&&flag; i++) { flag = false; for (int j = array.Length-1; j >i; j--) { if (array[j]<array[j-1]) { temp = array[j]; array[j] = array[j - 1]; array[j - 1] = temp; flag = true; } } display(array); } } }
public class Program { static void Main(string[] args) { for (int i = 0; i < 5; i++) { List<int> list=new List<int>(); for (int j = 0; j < 2000; j++) { Thread.Sleep(1); list.Add(new Random((int)DateTime.Now.Ticks).Next(0,10000)); } Stopwatch sw = new Stopwatch(); sw.Start(); var result = list.OrderBy(p => p).ToList(); sw.Stop(); Console.WriteLine("\n耗时:" + sw.ElapsedMilliseconds + "前十:" + string.Join(",", result.Take(10).ToList())); sw.Restart(); sw.Start(); //var bresult= BubbleSort(list); sw.Stop(); Console.WriteLine("耗时:"+sw.ElapsedMilliseconds+"前十:"+string.Join(",",list.Take(10).ToList())); } Console.ReadKey(); } static void BubbleSort(List<int> list) { int temp; bool flag = true; for (int i = 0; i < list.Count-1&&flag; i++) { flag = false; for (int j = list.Count-1; j >i; j--) { if (list[j]<list[j-1]) { temp = list[j]; list[j]=list[j-1]; list[j - 1] = temp; flag = true; } } } //return list; } }
public class Program { static void Main(string[] args) { List<int> list = new List<int>() { 49, 38, 65, 97, 76, 13, 27 }; QuickSort(list,0,list.Count-1); foreach (var item in list) { Console.Write(item+","); } Console.ReadKey(); } static void QuickSort(List<int> list,int low,int high) { if (low>=high) { throw new Exception(); } int index=QuickSortUnit(list,low,high); QuickSortUnit(list,low,index-1); QuickSortUnit(list,index+1,high); } static int QuickSortUnit(List<int> list,int low,int high) { var key = list[low]; while (low<high) { while (low < high && list[high] >= key) high--; list[low] = list[high]; while (low < high && list[low] <= key) low++; list[high] = list[low]; } list[low] = key; return high; } }
public class Program { static void Main(string[] args) { for (int i = 0; i < 5; i++) { List<int> list = new List<int>(); for (int j = 0; j < 2000; j++) { Thread.Sleep(1); list.Add(new Random((int)DateTime.Now.Ticks).Next(0,10000)); } Console.WriteLine("\n list"+string.Join(",",list.Take(10).ToList())); Stopwatch sw = new Stopwatch(); sw.Start(); var result = list.OrderBy(p=>p); sw.Stop(); Console.WriteLine("耗时:"+sw.ElapsedMilliseconds+" "+string.Join(",",result.Take(10).ToList())); sw.Restart(); Console.WriteLine("list"+string.Join(",",list.Take(10).ToList())); sw.Start(); QuickSort(list,0,list.Count-1); sw.Stop(); Console.WriteLine("耗时:"+sw.ElapsedMilliseconds+" "+string.Join(",",result.Take(10).ToList())); } Console.ReadKey(); } static void QuickSort(List<int> list,int low,int high) { if (low>=high) { throw new Exception(); } int index=QuickSortUnit(list,low,high); QuickSortUnit(list,low,index-1); QuickSortUnit(list,index+1,high); } static int QuickSortUnit(List<int> list,int low,int high) { var key = list[low]; while (low<high) { while (low < high && list[high] >= key) high--; list[low] = list[high]; while (low < high && list[low] <= key) low++; list[high] = list[low]; } list[low] = key; return high; } }
static void BubbleSort(List<int> list) { int temp; bool flag=true; for (int i = 0; i < list.Count-1&&flag; i++) { flag = false; for (int j = list.Count-1; j > i; j--) { if (list[j]>list[j-1]) { temp = list[j]; list[j] = list[j - 1]; list[j - 1] = temp; flag = true; } } } } static void QuickSort(List<int> list,int low,int high) { if (low>=high) { //throw new Exception(); return; } int index = QuickSortUnit(list,low,high); QuickSortUnit(list,low,index-1); QuickSortUnit(list,index+1,high); } static int QuickSortUnit(List<int> list,int low,int high) { int key = list[low]; while (low<high) { while (low < high && list[high] >= key) high--; list[low] = list[high]; while (low < high && list[low] <= key) low++; list[high] = list[low]; } list[low] = key; return high; }
冒泡的时间复杂度为: 0(n) - 0(n^2)
快排的时间复杂度为:
平均复杂度: N(logN)
最坏复杂度: 0(n^2)