// Use this for initialization void Start() { int n = 1900; int[] arr = AlgorithmsHelp.generateRandomArray(n, 1, n); int[] arrCp = AlgorithmsHelp.CopyIntArray(arr, n); int[] arrCp2 = AlgorithmsHelp.CopyIntArray(arr, n); // AlgorithmsHelp.testSort ("MerSort", MergesSort.MergeSort, arr, n); // AlgorithmsHelp.testSort ("QuickSort1", QuickSort, arrCp, n); // AlgorithmsHelp.testSort ("QuickSort2", QuickSort2, arrCp2, n); // AlgorithmsHelp.testSort ("QuickSort3", QuickSort3, arrCp2, n); // // arr = AlgorithmsHelp.generateNearlyOrderArray(n,5); // arrCp = AlgorithmsHelp.CopyIntArray(arr,n); // arrCp2 = AlgorithmsHelp.CopyIntArray (arr,n); // AlgorithmsHelp.testSort ("MerSort", MergesSort.MergeSort, arr, n); // AlgorithmsHelp.testSort ("QuickSort1", QuickSort, arrCp, n); // AlgorithmsHelp.testSort ("QuickSort2", QuickSort2, arrCp2, n); // AlgorithmsHelp.testSort ("QuickSort3", QuickSort3, arrCp2, n); arr = AlgorithmsHelp.generateRandomArray(n, 1, 10); arrCp = AlgorithmsHelp.CopyIntArray(arr, n); arrCp2 = AlgorithmsHelp.CopyIntArray(arr, n); AlgorithmsHelp.testSort("MerSort", MergesSort.MergeSort, arr, n); AlgorithmsHelp.testSort("QuickSort1", QuickSort, arrCp, n); AlgorithmsHelp.testSort("QuickSort2", QuickSort2, arrCp2, n); AlgorithmsHelp.testSort("QuickSort3", QuickSort3, arrCp2, n); // // arr = AlgorithmsHelp.generateRandomArray(n,1,5); // AlgorithmsHelp.testSort ("QuickSort", QuickSort, arr, n); // int[] arr = AlgorithmsHelp.generateNearlyOrderArray(n,5); // AlgorithmsHelp.testSort ("QuickSort", QuickSort, arr, n); }
//t data的的下标,将data[t]的向下调整 private void ShiftDown(int t) { while (2 * t <= count) { int j = 2 * t; //左孩子下标 //存在右孩子且右孩子大于左孩子,则交换的下标j改为右孩子 if (j + 1 <= count && data[indexes[j]].CompareTo(data[indexes[j + 1]]) < 0) { j += 1; } if (data [indexes[t]].CompareTo(data [indexes[j]]) > 0) { break; } else { AlgorithmsHelp.Swap(ref indexes[t], ref indexes[j]); //不对的 // AlgorithmsHelp.Swap (ref reserves[t],ref reserves[j]); //对的 reserves[indexes[t]] = t; reserves [indexes [j]] = j; t = j; } } }
public static void SelectionSort <T>(T[] arr, int n) where T : IComparable { // for (int i = 0; i != n; i++) { // int min = -300; // int index = i; // for (int j = i+1; j != n; j++) { // if (min > arr [j]) { // index = j; // min = arr [j]; // } // } // int temp = arr [i]; // arr [i] = arr [index]; // arr [index] = temp; //// AlgorithmsHelp.Swap (i,index,arr); // // }//错了。写在这里,让我记一辈子吧。 for (int i = 0; i != n; i++) { int minIndex = i; for (int j = minIndex + 1; j != n; j++) { if (arr [j].CompareTo(arr[minIndex]) < 0) { minIndex = j; } } AlgorithmsHelp.Swap(ref arr[i], ref arr[minIndex]); } }
//对数组arr[left...right]进行划分,双路划分 private int _Partition2 <T>(T[] arr, int left, int right) where T : System.IComparable <T> { int randomIndex = Random.Range(left, right + 1); AlgorithmsHelp.Swap(ref arr [left], ref arr [randomIndex]); //arr[left + 1...i)小于,等于v,arr(j,right]大于等于v T v = arr[left]; int i = left + 1; int j = right; while (true) { while (i <= right && arr [i].CompareTo(v) < 0) { i++; } while (j >= left + 1 && arr [j].CompareTo(v) > 0) { j--; } if (i > j) //这里需要记得 { break; } AlgorithmsHelp.Swap(ref arr[i], ref arr[j]); i++; //这里犯过错误,记得 j--; //这里犯过错误,记得 } AlgorithmsHelp.Swap(ref arr[left], ref arr[j]); return(j); }
private void ShiftUp(int count) { int t = count; while (t > 1 && data [t].CompareTo(data [t / 2]) > 0) { AlgorithmsHelp.Swap(ref data[t], ref data[t / 2]); t = t / 2; } }
// 向上调整位置,直到父母节点小于原来的data[index] private void _ShiftUp(int index) { int t = index; while (t / 2 >= 1 && data[t / 2].CompareTo(data[t]) > 0) { AlgorithmsHelp.Swap(ref data[t / 2], ref data[t]); t /= 2; } }
public Item ExtraMinItem() { Debug.Assert(count - 1 >= 0); Item res = data [indexes[1]]; AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]); reserves [indexes [1]] = 1; reserves[indexes[count]] = 0; count--; _ShiftDown(1); return(res); }
// 向上调整位置,直到父母节点小于原来的data[index] private void _ShiftUp(int index) { int t = index; while (t / 2 >= 1 && data[indexes[t / 2]].CompareTo(data[indexes[t]]) > 0) { AlgorithmsHelp.Swap(ref indexes[t / 2], ref indexes[t]); reserves [indexes [t / 2]] = t / 2; reserves [indexes [t]] = t; t /= 2; } }
public int ExtraMinItemIndex() { Debug.Assert(count - 1 >= 0); int res = indexes[1] - 1; AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]); reserves[indexes[1]] = 1; reserves [indexes[count]] = 0; //0 count--; _ShiftDown(1); return(res); }
// Use this for initialization void Start() { int n = 10000; int[] arr = AlgorithmsHelp.generateNearlyOrderArray(n, 0); int[] arrCopy = AlgorithmsHelp.CopyIntArray(arr, n); int needFindValue = 500; int index = HalfFind1(arr, n, needFindValue); int index2 = HalfFind2(arr, n, needFindValue); print(index == -1 ? "No Find Value" : "Find value in + arr " + index + " and arr[index] = " + arr[index]); print(index2 == -1 ? "No Find Value" : "Find value in + arr " + index2 + " and arrCopy[index2] = " + arrCopy[index2]); }
private void ShiftUp(int count) { int t = count; while (t > 1 && data [indexes[t]].CompareTo(data [indexes[t / 2]]) > 0) { AlgorithmsHelp.Swap(ref indexes[t], ref indexes[t / 2]); // AlgorithmsHelp.Swap (ref reserves[t],ref reserves[t/2]); reserves[indexes[t / 2]] = t / 2; reserves [indexes [t]] = t; t = t / 2; } }
// Use this for initialization void Start() { int n = 1000; int[] arr = AlgorithmsHelp.generateRandomArray(n, 1, 3); int[] arrcopy = AlgorithmsHelp.CopyIntArray(arr, n); AlgorithmsHelp.testSort("BubbleSort", BubblableSort, arr, n); AlgorithmsHelp.printData(arr); AlgorithmsHelp.testSort("BubbleSort1", BubblableSort1, arrcopy, n); AlgorithmsHelp.printData(arrcopy); }
//取出最大的元素的索引 public int ExtractBigItemIndex() { Debug.Assert(count > 0); int ret = indexes[1] - 1; AlgorithmsHelp.Swap(ref indexes[1], ref indexes[count]); reserves [indexes [count]] = 0; reserves [indexes [1]] = 1; count--; ShiftDown(1); return(ret); }
public void BubblableSort <T>(T[] arr, int n) where T : IComparable <T> { while (n != 0) { for (int i = 0; i != n - 1; i++) { if (arr [i].CompareTo(arr [i + 1]) > 0) { AlgorithmsHelp.Swap(ref arr[i], ref arr[i + 1]); } } n--; } }
// Use this for initialization void Start() { int n = 30000000; int[] a = AlgorithmsHelp.generateRandomArray(n, 1, 10 * n); int[] sameA = AlgorithmsHelp.CopyIntArray(a, n); int[] same2A = AlgorithmsHelp.CopyIntArray(a, n); // AlgorithmsHelp.printData (a); AlgorithmsHelp.testSort("Merge Sort", MergeSort, a, n); AlgorithmsHelp.testSort("Merge Sort1", MergeSort1, a, n); AlgorithmsHelp.testSort("Insert Sort1", InsertSort.InsertionSort1, a, n); // AlgorithmsHelp.printData (a); }
public static void InsertionSort <T>(T[] arr, int n) where T : IComparable { for (int i = 1; i != n; i++) { for (int j = i; j > 0; j--) { if (arr [j].CompareTo(arr [j - 1]) < 0) { AlgorithmsHelp.Swap(ref arr [j], ref arr [j - 1]); } else { break; } } } }
//对数组arr进行划分,a[left+1...j-1],a[j],a[j+1...right]; // 1.a[left+1...j] < v, a[j+1...right]>v private int Partition(Edge <float>[] arr, int left, int right) { Edge <float> v = arr [left]; int j = left; //1.a[left+1...j] < v ,a[j+1...right]>v for (int i = left + 1; i <= right; i++) { if (arr [i].CompareTo(v) < 0) { AlgorithmsHelp.Swap(ref arr[j + 1], ref arr[i]); j++; } } AlgorithmsHelp.Swap(ref arr[j], ref arr[left]); return(j); }
//不需要开辟新的空间,进行的原地堆排序 public void HeapSort3 <T>(T[] arr, int n) where T : System.IComparable <T> { int count = n; int t = (count - 1) / 2; while (0 <= t) { _ShiftDown(arr, n, t); t--; } for (int i = n - 1; i >= 1; i--) { AlgorithmsHelp.Swap(ref arr[i], ref arr[0]); _ShiftDown(arr, i, 0); //这里又犯错了。。。 } }
//优化过的bubble public void BubblableSort1 <T>(T[] arr, int n) where T : IComparable <T> { bool swapped = false; do { swapped = false; for (int i = 1; i != n; i++) { if (arr [i].CompareTo(arr [i - 1]) < 0) { AlgorithmsHelp.Swap(ref arr [i], ref arr [i - 1]); swapped = true; } } n--; } while(swapped); }
//对arr[left...right]进行三路快速排序 public static void _QuickSort3 <T> (T[] arr, int left, int right) where T : System.IComparable <T> { if (right - left <= 15) { _InsertSort(arr, left, right); return; } //Partition //arr[left+1...lj] < v , arr[lj+1...i-1]=v , arr[rj...right] < v int randomIndex = Random.Range(left, right + 1); AlgorithmsHelp.Swap(ref arr[randomIndex], ref arr[left]); T v = arr[left]; int lj = left; int rj = right + 1; int i = left + 1; while (i < rj) { if (arr [i].CompareTo(v) < 0) { AlgorithmsHelp.Swap(ref arr[lj + 1], ref arr[i]); lj++; i++; } else if (arr [i].CompareTo(v) > 0) { AlgorithmsHelp.Swap(ref arr[i], ref arr[rj - 1]); rj--; } else if (arr [i].CompareTo(v) == 0) { i++; } } // AlgorithmsHelp.Swap(ref arr[left], ref arr[lj]); _QuickSort3(arr, left, lj - 1); _QuickSort3(arr, rj, right); }
private void _ShiftDown <T>(T[] arr, int n, int t) where T : System.IComparable <T> { while (2 * t + 1 <= n - 1) //这里犯错误了 { int j = 2 * t + 1; if (j + 1 <= n - 1 && arr [j].CompareTo(arr [j + 1]) < 0) { j += 1; } if (arr [t].CompareTo(arr [j]) > 0) { break; } else { AlgorithmsHelp.Swap(ref arr[t], ref arr[j]); t = j; } } }
// Use this for initialization void Start() { int n = 1000000; int[] arr = AlgorithmsHelp.generateRandomArray(n, 1, n); int[] arrCopy1 = AlgorithmsHelp.CopyIntArray(arr, n); int[] arrCopy2 = AlgorithmsHelp.CopyIntArray(arr, n); int[] arrCopy3 = AlgorithmsHelp.CopyIntArray(arr, n); int[] arrCopy4 = AlgorithmsHelp.CopyIntArray(arr, n); AlgorithmsHelp.testSort("Merge Sort", MergesSort.MergeSort, arr, n); AlgorithmsHelp.testSort("Quick Sort 3 ways", QuickSorts.QuickSort3, arrCopy1, n); AlgorithmsHelp.testSort("Heap Sort", HeapSort, arrCopy2, n); AlgorithmsHelp.testSort("Heap Sort2", HeapSort2, arrCopy3, n); AlgorithmsHelp.testSort("Heap Sort3", HeapSort3, arrCopy4, n); // // // arr = AlgorithmsHelp.generateRandomArray (n,1,20); // arrCopy1 = AlgorithmsHelp.CopyIntArray (arr,n); // arrCopy2 = AlgorithmsHelp.CopyIntArray (arr, n); // arrCopy3 = AlgorithmsHelp.CopyIntArray (arr, n); // // // AlgorithmsHelp.testSort ("Merge Sort", MergesSort.MergeSort,arr,n); // AlgorithmsHelp.testSort ("Quick Sort 3 ways",QuickSorts.QuickSort3,arrCopy1,n); // AlgorithmsHelp.testSort ("Heap Sort", HeapSort, arrCopy2, n); // AlgorithmsHelp.testSort ("Heap Sort2", HeapSort2, arrCopy3, n); // // // arr = AlgorithmsHelp.generateNearlyOrderArray (n,20); // arrCopy1 = AlgorithmsHelp.CopyIntArray (arr,n); // arrCopy2 = AlgorithmsHelp.CopyIntArray (arr, n); // arrCopy3 = AlgorithmsHelp.CopyIntArray (arr, n); // // // AlgorithmsHelp.testSort ("Merge Sort", MergesSort.MergeSort,arr,n); // AlgorithmsHelp.testSort ("Quick Sort 3 ways",QuickSorts.QuickSort3,arrCopy1,n); // AlgorithmsHelp.testSort ("Heap Sort", HeapSort, arrCopy2, n); // AlgorithmsHelp.testSort ("Heap Sort2", HeapSort2, arrCopy3, n); }
// 对a[left...right]进行划分 private int _Partition <T>(T[] arr, int left, int right) where T : System.IComparable <T> { int randomIndex = Random.Range(left, right + 1); //n很大,而且基本为有序时会出问题 AlgorithmsHelp.Swap(ref arr[left], ref arr[randomIndex]); T v = arr [left]; int j = left; //a[left+1...j]均小于v,而a[j+1...right]均大于v for (int i = left + 1; i <= right; i++) { if (arr [i].CompareTo(v) < 0) { AlgorithmsHelp.Swap(ref arr [j + 1], ref arr [i]); j++; } } AlgorithmsHelp.Swap(ref arr[left], ref arr[j]); return(j); }
//t data的的下标,将data[t]的向下调整 private void ShiftDown(int t) { while (2 * t <= count) { int j = 2 * t; //左孩子下标 //存在右孩子且右孩子大于左孩子,则交换的下标j改为右孩子 if (j + 1 <= count && data[j].CompareTo(data[j + 1]) < 0) { j += 1; } if (data [t].CompareTo(data [j]) > 0) { break; } else { AlgorithmsHelp.Swap(ref data[t], ref data[j]); t = j; } } }
//向下调整位置,直到该节点处于合适的位置 private void _ShiftDown(int index) { int t = index; while (t * 2 <= count) { int j = t * 2; if (j + 1 <= count && data [j + 1].CompareTo(data [j]) < 0) { j = j + 1; } if (data [t].CompareTo(data [j]) < 0) { break; } else { AlgorithmsHelp.Swap(ref data [t], ref data [j]); t = j; } } }
// Use this for initialization void Start() { // int n = 1000; // //实验1 // int[] arr = AlgorithmsHelp.generateRandomArray (n, 1, n); // int[] sameArr = AlgorithmsHelp.CopyIntArray (arr,n); // AlgorithmsHelp.testSort ("InsertionSort1", InsertionSort1, sameArr, n); // AlgorithmsHelp.printData (sameArr); //// AlgorithmsHelp.testSort ("SelectionSort", SelectSort.SelectionSort,arr,n); //// AlgorithmsHelp.printData (sameArr); // AlgorithmsHelp.testSort ("InsertionSort", InsertionSort, arr, n); // AlgorithmsHelp.printData (arr); //实验2 比较有序数组的,选择排序的超快性 int n = 10000; int[] arr = AlgorithmsHelp.generateNearlyOrderArray(n, 10); int[] sameArr = AlgorithmsHelp.CopyIntArray(arr, n); AlgorithmsHelp.testSort("InsertionSort1", InsertionSort1, arr, n); AlgorithmsHelp.printData(arr); AlgorithmsHelp.testSort("SelectionSort", SelectSort.SelectionSort, sameArr, n); AlgorithmsHelp.printData(sameArr); }
//向下调整位置,直到该节点处于合适的位置 private void _ShiftDown(int index) { int t = index; while (t * 2 <= count) { int j = t * 2; if (j + 1 <= count && data [indexes[j + 1]].CompareTo(data [indexes[j]]) < 0) { j = j + 1; } if (data [indexes[t]].CompareTo(data [indexes[j]]) < 0) { break; } else { AlgorithmsHelp.Swap(ref indexes[t], ref indexes[j]); reserves [indexes [t / 2]] = t / 2; reserves [indexes [t]] = t; t = j; } } }
// Use this for initialization void Start() { //测试极小堆 print("------------------测试极小堆------------------"); int capicity = 100; int n = 10; int[] arr = AlgorithmsHelp.generateRandomArray(n, 1, 20); MinHeap <int> minHeap = new MinHeap <int> (100); MinHeap <int> minarrHeap = new MinHeap <int> (arr, 100); for (int i = 0; i != n; i++) { minHeap.Insert(arr[i]); } minHeap.print(); minarrHeap.print(); string str = ""; while (minHeap.Size() > 0) { str += minHeap.ExtraMinItem() + " "; } Debug.Log(str); testChangeInNormalHeap(minarrHeap); print("------------------------------------------"); print("------------------测试极小索引堆------------------"); //测试极小索引堆 arr = AlgorithmsHelp.generateRandomArray(n, 1, 20); IndexMinHeap <int> minIndexHeap = new IndexMinHeap <int>(100); IndexMinHeap <int> minarrIndexHeap = new IndexMinHeap <int>(arr, 100); for (int i = 0; i != n; i++) { minIndexHeap.Insert(arr[i]); } minIndexHeap.print(); minarrIndexHeap.print(); str = ""; while (minIndexHeap.Size() > 0) { str += minIndexHeap.ExtraMinItem() + " "; } print("从小到大排序 " + str); testChangeInIndexHeap(minarrIndexHeap); // 测试极大堆 // int capicity = 100; // int n = 10; // Heap<int> maxHeap = new Heap<int> (100); // int[] arr = AlgorithmsHelp.generateRandomArray (n,1,20); // for (int i = 0; i != n; i++) { // maxHeap.Insert (arr[i]); // } // maxHeap.printData (); // // string str = ""; // for (int i = 0; i != n; i++) { // str +=maxHeap.ExtractBigItem()+" "; // } // print ("data[] : " +str); }
// Use this for initialization void Start() { int[] a = { 3, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; string s = ""; foreach (int num in a) { s += num + " "; } print(s + " "); s = ""; SelectionSort(a, 10); foreach (float num in a) { s += num + " "; } print(s + " "); s = ""; float[] b = { 4.4f, 3.3f, 2.2f, 1.1f }; foreach (var num in b) { s += num + " "; } print(s + " "); s = ""; SelectionSort(b, 4); foreach (var num in b) { s += num + " "; } print(s + " "); // 测试模板函数,传入字符串数组 string[] c = { "D", "C", "B", "A" }; s = ""; foreach (var num in c) { s += num + " "; } print(s + " "); s = ""; SelectionSort(c, 4); foreach (var num in c) { s += num + " "; } print(s + " "); Student[] d = { new Student("D", 90), new Student("C", 100), new Student("B", 95), new Student("A", 95) }; s = ""; foreach (var num in d) { print(num); } SelectionSort(d, 4); foreach (var num in d) { print(num); } int n = 10000; // a = null; // a = AlgorithmsHelp.generateRandomArray (n,1,n); // AlgorithmsHelp.printData (a); // SelectionSort(a,n); // AlgorithmsHelp.printData (a); // a = null; a = AlgorithmsHelp.generateRandomArray(n, 1, n); AlgorithmsHelp.printData(a); AlgorithmsHelp.testSort("SelectionSort", SelectionSort, a, n); AlgorithmsHelp.printData(a); int i = 3; float f = i; }