public void Sort() { for (int i = 0; i < A.Length - 1; i++) { GeneralMethod.Swap <T>(A, i, FindMinIndex(i, A.Length - 1)); } }
private int Partition(T[] A, int st, int ed, int partionPos)//SELECT VALUE ON PAPOS AND MAKE THE ARRAY(ST TO ED) INTO TWO PARTS: BEFORE RET SMALLER THAN VALUE AFTER BIGGER { if (partionPos != st) { GeneralMethod.Swap(A, partionPos, st); } T value = A[st]; int boundary = st;//BOUNDARY REFER TO THE LAST SMALLER INDEX for (int i = st + 1; i <= ed; i++) { if (A[i].CompareTo(value) < 0) { if (boundary + 1 != i) { GeneralMethod.Swap(A, i, boundary + 1); } boundary++; } } if (st != boundary) { GeneralMethod.Swap(A, boundary, st); } return(boundary); }
public void Sort() { int dynamicSize = A.Length; Build_Heap();//build heap for (int i = A.Length - 1; i >= 1; i--) { GeneralMethod.Swap(A, i, 0); dynamicSize--; Max_Heaplify(dynamicSize, 0);// } }
private bool BubbleReverse(int st, int ed) { bool hasSwap = false; for (int i = ed - 1; i >= st; i--) { if (A[i].CompareTo(A[i + 1]) > 0) { GeneralMethod.Swap(A, i, i + 1); hasSwap = true; } } return(hasSwap); }
private bool BubbleForward(int st, int ed) { bool hasSwap = false; for (int i = st; i <= ed - 1; i++) { if (A[i].CompareTo(A[i + 1]) > 0) { GeneralMethod.Swap(A, i, i + 1); hasSwap = true; } } return(hasSwap); }
private void Max_Heaplify(int size, int i) { int largestIndex = -1; int leftIndex = GetLeftChildIndex(i); int rightIndex = GetRightChildIndex(i); if (leftIndex <= size - 1 && A[leftIndex].CompareTo(A[i]) > 0) { largestIndex = leftIndex; } else { largestIndex = i; } if (rightIndex <= size - 1 && A[rightIndex].CompareTo(A[largestIndex]) > 0) { largestIndex = rightIndex; } if (largestIndex != i) { GeneralMethod.Swap <T>(A, i, largestIndex); Max_Heaplify(size, largestIndex); } }