/** * Partition the list according to the value at the pivot index * This method should only partition the part of the array that is between startIndex and endIndex * All values lower than the pivot value should be to the left of the pivot value * and all values higher than the pivot value should be to the right of the pivot value * * This method should return the position of the pivot value after partitioning is complete * * For example: partition([4, 9, 5, 0, 2], 2, 0, 4) * should partition the values between indices 0...4 (inclusive) using the value 5 as the pivot * A possible partitioning would be: [2, 0, 4, 5, 9], this method should return 3 as that is where * the pivot value 5 has ended up. * This method may assume there are no equal values */ public static int partition(ISortList list, int pivotIndex, int leftIndex, int rightIndex) { while (leftIndex < rightIndex) { while (list.compare(leftIndex, pivotIndex) < 0) { leftIndex++; } while (list.compare(rightIndex, pivotIndex) > 0) { rightIndex--; } if (pivotIndex == leftIndex) { pivotIndex = rightIndex; } else if (pivotIndex == rightIndex) { pivotIndex = leftIndex; } list.swap(leftIndex, rightIndex); } return(pivotIndex); }
public static void quicksort(ISortList list) { if (list.Count < 1) { return; } quicksort(list, 0, list.Count - 1); }
public static void quicksort3(ISortList list) { if (list == null || list.Count <= 1) { return; } quicksort3(list, 0, list.Count - 1); }
public static void merge(ISortList list, int leftIndex, int middleIndex, int rightIndex) { int leftSubIndex, rightSubIndex, mergedSubindex; int leftSubSize = middleIndex - leftIndex + 1; int rightSubSize = rightIndex - middleIndex; // Temporary lists ISortList left = new SortList(leftSubSize); ISortList right = new SortList(rightSubSize); // Copy data into temp. lists for (leftSubIndex = 0; leftSubIndex < leftSubSize; leftSubIndex++) { left[leftSubIndex] = list[leftIndex + leftSubIndex]; } for (rightSubIndex = 0; rightSubIndex < rightSubSize; rightSubIndex++) { right[rightSubIndex] = list[middleIndex + 1 + rightSubIndex]; } // Merge temp. lists back into original leftSubIndex = 0; rightSubIndex = 0; mergedSubindex = leftIndex; while (leftSubIndex < leftSubSize && rightSubIndex < rightSubSize) { if (left[leftSubIndex] <= right[rightSubIndex]) { list[mergedSubindex] = left[leftSubIndex]; leftSubIndex++; } else { list[mergedSubindex] = right[rightSubIndex]; rightSubIndex++; } mergedSubindex++; } // Copy any remainders from the left list while (leftSubIndex < leftSubSize) { list[mergedSubindex] = left[leftSubIndex]; leftSubIndex++; mergedSubindex++; } // Copy any remainders from the right list while (rightSubIndex < rightSubSize) { list[mergedSubindex] = right[rightSubIndex]; rightSubIndex++; mergedSubindex++; } }
public static void mergesort(ISortList list) { if (list.Count <= 1) { return; } mergesort(list, 0, list.Count - 1); }
public static void mergesort(ISortList list, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { int middleIndex = (leftIndex + rightIndex) / 2; // Divide both halves mergesort(list, leftIndex, middleIndex); mergesort(list, middleIndex + 1, rightIndex); merge(list, leftIndex, middleIndex, rightIndex); } }
public static void bubblesort(ISortList list) { for (var i = 0; i < list.Count; i++) { for (var j = 0; j < list.Count - i - 1; j++) { if (list.compare(j, j + 1) <= 0) { continue; } list.swap(j, j + 1); } } }
private static void quicksort(ISortList list, int leftIndex, int rightIndex) { if (leftIndex < rightIndex) { int pivotIndex = (leftIndex + rightIndex) / 2; int pivot = partition(list, pivotIndex, leftIndex, rightIndex); if (pivot > 1) { quicksort(list, leftIndex, pivot - 1); } if (pivot + 1 < rightIndex) { quicksort(list, pivot + 1, rightIndex); } } }
private static void quicksort3(ISortList list, int leftIndex, int rightIndex) { if (leftIndex >= rightIndex) { return; } var pivotIndex = leftIndex + 1; if (list.compare(leftIndex, rightIndex) > 0) { if (list.compare(leftIndex, leftIndex / rightIndex) > 0) { pivotIndex = leftIndex; } else { pivotIndex = leftIndex / rightIndex; } } else { if (list.compare(rightIndex, leftIndex / rightIndex) > 0) { pivotIndex = rightIndex; } else { pivotIndex = leftIndex / rightIndex; } } var pivot = partition(list, pivotIndex, leftIndex, rightIndex); if (pivot > 1) { quicksort3(list, leftIndex, pivot - 1); } if (pivot + 1 < rightIndex) { quicksort3(list, pivot + 1, rightIndex); } }
private static void quicksort(ISortList list, int leftIndex, int rightIndex) { if (leftIndex >= rightIndex) { return; } var pivot = partition(list, leftIndex + 1, leftIndex, rightIndex); if (pivot > 1) { quicksort(list, leftIndex, pivot - 1); } if (pivot + 1 < rightIndex) { quicksort(list, pivot + 1, rightIndex); } }
/** * Partition the list according to the value at the pivot index * This method should only partition the part of the array that is between startIndex and endIndex * All values lower than the pivot value should be to the left of the pivot value * and all values higher than the pivot value should be to the right of the pivot value * * This method should return the position of the pivot value after partitioning is complete * * For example: partition([4, 9, 5, 0, 2], 2, 0, 4) * should partition the values between indices 0...4 (inclusive) using the value 5 as the pivot * A possible partitioning would be: [2, 0, 4, 5, 9], this method should return 3 as that is where * the pivot value 5 has ended up. * This method may assume there are no equal values */ public static int partition(ISortList list, int pivotIndex, int leftIndex, int rightIndex) { if (leftIndex == rightIndex || list.Count <= 1) { return(0); // Nothing to swap } while (true) { while (list.compare(leftIndex, pivotIndex) < 0) { leftIndex++; } while (list.compare(rightIndex, pivotIndex) > 0) { rightIndex--; } if (leftIndex < rightIndex) { if (pivotIndex == leftIndex) { pivotIndex = rightIndex; } else if (pivotIndex == rightIndex) { pivotIndex = leftIndex; } list.swap(rightIndex, leftIndex); } else { return(rightIndex); } } }
public static void mergesort(ISortList list, int leftIndex, int rightIndex) { throw new NotImplementedException(); }
public static void mergesort(ISortList list) { throw new NotImplementedException(); }
/** * Partition the list according to the value at the pivot index * This method should only partition the part of the array that is between startIndex and endIndex * All values lower than the pivot value should be to the left of the pivot value * and all values higher than the pivot value should be to the right of the pivot value * * This method should return the position of the pivot value after partitioning is complete * * For example: partition([4, 9, 5, 0, 2], 2, 0, 4) * should partition the values between indices 0...4 (inclusive) using the value 5 as the pivot * A possible partitioning would be: [2, 0, 4, 5, 9], this method should return 3 as that is where * the pivot value 5 has ended up. * This method may assume there are no equal values */ public static int partition(ISortList list, int pivotIndex, int leftIndex, int rightIndex) { throw new NotImplementedException(); }
private static void quicksort(ISortList list, int leftIndex, int rightIndex) { throw new NotImplementedException(); }
public static void mergesort(ISortList list) { mergesort(list, 0, list.Count - 1); }
public static void mergesort(ISortList list, int leftIndex, int rightIndex) { // 1 item can't be sorted if (rightIndex - leftIndex == 0) { return; } // 2 items can be swapped if (rightIndex - leftIndex == 1) { // If left is bigger than right, swap if (list.compare(leftIndex, rightIndex) > 0) { list.swap(leftIndex, rightIndex); } return; } var half = (leftIndex + rightIndex - 1) / 2; // More than 2 items have to be split if (rightIndex - leftIndex >= 2) { mergesort(list, leftIndex, half); mergesort(list, half + 1, rightIndex); } // Merge var currentLeft = leftIndex; var currentRight = half + 1; var tempList = new List <int>(); while (currentLeft <= half || currentRight <= rightIndex) { if (currentLeft <= half && currentRight > rightIndex) { tempList.Add(list[currentLeft]); currentLeft++; } else if (currentRight <= rightIndex && currentLeft > half) { tempList.Add(list[currentRight]); currentRight++; } else if (list.compare(currentLeft, currentRight) < 0) { tempList.Add(list[currentLeft]); currentLeft++; } else { tempList.Add(list[currentRight]); currentRight++; } } // replace the items in the list with the items in the templist for (var i = 0; i < tempList.Count; i++) { list[i + leftIndex] = tempList[i]; } }