public class Program { static void Main(string[] args) { int[] array = new int[] { 49, 38, 65, 97, 76, 13, 27 }; SelectionSort(array); foreach (var item in array) { Console.WriteLine(item+" "); } Console.ReadKey(); } static void SelectionSort(int[] array) { int temp; int pos; for (int i = 0; i < array.Length-1; i++) { pos = i; for (int j = i+1; j < array.Length; j++) { if (array[j]<array[pos]) { pos = j; } } temp = array[i]; array[i] = array[pos]; array[pos] = temp; } } #region 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; } #endregion }
public class Program { static void Main(string[] args) { List<int> list = new List<int>() { 16,7,3,20,17,8}; HeapSort(list); foreach (var item in list) { Console.WriteLine(item+" "); } Console.ReadKey(); } public static void HeapSort(List<int> list) { for (int i = list.Count/2-1; i >=0; i--) { HeapAdjust(list,i,list.Count-1); } for (int i = list.Count-1; i >0; i--) { int temp = list[0]; list[0] = list[i]; list[i] = temp; HeapAdjust(list, 0, i); } } public static void HeapAdjust(List<int> list,int parent,int length) { int temp = list[parent]; int child = 2 * parent + 1; while (child<length) { if (child + 1 < length && list[child] < list[child + 1]) child++; if (temp>=list[child]) { break; } list[parent] = list[child]; parent = child; child = 2 * parent+1; } list[parent] = temp; } #region 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 SelectionSort(int[] array) { int temp; int pos; for (int i = 0; i < array.Length - 1; i++) { pos = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < array[pos]) { pos = j; } } temp = array[i]; array[i] = array[pos]; array[pos] = temp; } } 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; } #endregion }
堆排序前多少
public class Program { static void Main(string[] args) { List<int> list = new List<int>() { 16,7,3,20,17,8}; var result= HeapSort(list,5); foreach (var item in result) { Console.WriteLine(item+" "); } Console.ReadKey(); } public static List<int> HeapSort(List<int> list,int top) { List<int> topNode = new List<int>(); for (int i = list.Count/2-1; i >=0; i--) { HeapAdjust(list,i,list.Count-1); } for (int i = list.Count-1; i >0; i--) { int temp = list[0]; list[0] = list[i]; list[i] = temp; topNode.Add(temp); HeapAdjust(list, 0, i); } return topNode; } public static void HeapAdjust(List<int> list,int parent,int length) { int temp = list[parent]; int child = 2 * parent + 1; while (child<length) { if (child + 1 < length && list[child] < list[child + 1]) child++; if (temp>=list[child]) { break; } list[parent] = list[child]; parent = child; child = 2 * parent+1; } list[parent] = temp; } }
直接选择排序的时间复杂度为:O(n^2)
堆排序的时间复杂度:O(NlogN)