1 /*插入排序(insert Sort) O(n^2)*/ 2 public List<int> InsertSort(int[] arr) 3 { 4 List<int> tempArr = new List<int>(); 5 tempArr.Add(arr[0]); 6 bool flag = false; 7 for (int i = 1; i < arr.Length; i++) 8 { 9 for (int j = 0; j < tempArr.Count; j++) 10 { 11 if (arr[i] < tempArr[j]) 12 { 13 tempArr.Insert(j, arr[i]); 14 flag = true; 15 break; 16 } 17 } 18 if (!flag) 19 { 20 tempArr.Add(arr[i]); 21 } 22 flag = false; 23 } 24 return tempArr; 25 }
1 /*归并排序(merge Sort) O(nlog2(n)) 非递归*/ 2 public List<int> MergeSort(int[] arr) 3 { 4 List<int> tempArr = new List<int>();//保存最后得到的数组 5 int twoDouble = 2;//保存2的幂次方 6 List<int> preArr = new List<int>();//保存前一个数组 7 for(int i=0;i<arr.Length;i++){ 8 preArr.Add(arr[i]); 9 } 10 while (arr.Length > twoDouble/2)//当数组的长度小于2的幂次方的长度时,终止 11 { 12 List<int> subArr = new List<int>();//拿到合并后的数组 13 tempArr.Clear(); 14 if (arr.Length / twoDouble > 0)//不是最后一组 15 { 16 for (int i = 0; i < arr.Length / twoDouble; i++) 17 {//一定可以有2个或以上的组,而且是可以平分的 18 List<int> leftArr = new List<int>(); 19 List<int> rightArr = new List<int>(); 20 for (int j = i * twoDouble; j < (i * twoDouble + twoDouble / 2); j++) 21 { 22 leftArr.Add(preArr[j]); 23 } 24 for (int j = (i * twoDouble + twoDouble / 2); j < (i * twoDouble + twoDouble); j++) 25 { 26 rightArr.Add(preArr[j]); 27 } 28 subArr = MergeSortSubProgram(leftArr, rightArr); 29 for (int j = 0; j < subArr.Count; j++) //将拿到的合并后的数组添加到preArr 30 { 31 tempArr.Add(subArr[j]); 32 } 33 subArr.Clear(); 34 } 35 if ((arr.Length - (arr.Length / twoDouble) * twoDouble) <= twoDouble / 2)//如果最后的数不能分为2组 36 { 37 for (int i = (arr.Length / twoDouble) * twoDouble; i < arr.Length; i++) 38 { 39 tempArr.Add(preArr[i]); 40 } 41 } 42 else { 43 List<int> leftArr = new List<int>(); 44 List<int> rightArr = new List<int>(); 45 for (int i = (arr.Length / twoDouble) * twoDouble; i < (arr.Length / twoDouble) * twoDouble+twoDouble / 2; i++) 46 { 47 leftArr.Add(preArr[i]); 48 } 49 for (int i = (arr.Length / twoDouble) * twoDouble + twoDouble / 2; i < arr.Length; i++) 50 { 51 rightArr.Add(preArr[i]); 52 } 53 subArr = MergeSortSubProgram(leftArr, rightArr);//最后一组合并 54 for (int i = 0; i < subArr.Count; i++) 55 { 56 tempArr.Add(subArr[i]); 57 } 58 subArr.Clear(); 59 } 60 } 61 else if (arr.Length / twoDouble == 0) //最后一组 62 { 63 List<int> leftArr = new List<int>(); 64 List<int> rightArr = new List<int>(); 65 for (int i = 0; i < twoDouble / 2; i++) { 66 leftArr.Add(preArr[i]); 67 } 68 for (int i = twoDouble / 2; i < arr.Length; i++) { 69 rightArr.Add(preArr[i]); 70 } 71 subArr = MergeSortSubProgram(leftArr, rightArr);//最后一组合并 72 for (int i = 0; i < subArr.Count; i++) { 73 tempArr.Add(subArr[i]); 74 } 75 subArr.Clear(); 76 } 77 78 //preArr = tempArr;//使preArr指向tempArr了 79 twoDouble *= 2; 80 preArr.Clear(); 81 for (int x = 0; x < tempArr.Count; x++) { 82 Console.Write(tempArr[x] + " "); 83 preArr.Add(tempArr[x]); 84 } 85 Console.WriteLine(); 86 } 87 return tempArr; 88 } 89 //实现左右2边数组从小到大合并 90 List<int> MergeSortSubProgram(List<int> leftArr,List<int> rightArr) 91 { 92 List<int> tempArr = new List<int>(); 93 bool flag = true; 94 int leftPos = 0, rightPos = 0; 95 96 while (flag) { 97 if (leftArr[leftPos] < rightArr[rightPos]) //当左边的数比右边小 98 { 99 tempArr.Add(leftArr[leftPos]); 100 leftPos++; 101 } 102 else { 103 tempArr.Add(rightArr[rightPos]); 104 rightPos++; 105 } 106 //当其中一组到底之后 107 if (leftPos == leftArr.Count || rightPos == rightArr.Count) { 108 flag = false; 109 if (leftPos == leftArr.Count) { 110 for (int i = rightPos; i < rightArr.Count; i++) { 111 tempArr.Add(rightArr[i]); 112 } 113 } 114 else if (rightPos == rightArr.Count) { 115 for (int i = leftPos; i < leftArr.Count; i++) { 116 tempArr.Add(leftArr[i]); 117 } 118 } 119 } 120 } 121 122 return tempArr; 123 }