publicclassQuicksort{ publicvoidquicksort(int[] nums, int start, int end){ if (end <= start) return; int q = partition(nums, start, end); quicksort(nums, start, q-1); quicksort(nums, q+1, end); } publicintpartition(int[] nums, int start, int end){ int x = nums[end]; int i = start-1; for (int j = start; j <= end-1; j++) { if (nums[j] <= x){ i++; swap(nums, i, j); } } swap(nums, i+1, end); return i+1; } privatevoidswap(int[] nums, int a, int b){ int tmp = nums[a]; nums[a] = nums[b]; nums[b] = tmp; } }
publicvoidquicksort(int[] nums, int start, int end){ if (start >= end) return; int q = randomPartition(nums, start, end); quicksort(nums, start, q-1); quicksort(nums, q+1, end); } publicintrandomPartition(int[] nums, int start, int end){ Random r = new Random(); int index = start + r.nextInt(end - start); swap(nums, index, end); return partition(nums, start, end); } publicintpartition(int[] nums, int start, int end){ int x = nums[end]; int i = start-1; for (int j = start; j <= end-1; j++) { if (nums[j] <= x){ i++; swap(nums, i, j); } } swap(nums, i+1, end); return i+1; }
publicvoidquicksort(int[] nums, int start, int end){ if (start >= end) return; int q = randomThreeMedianPartition(nums, start, end); quicksort(nums, start, q-1); quicksort(nums, q+1, end); } publicintrandomThreeMedianPartition(int[] nums, int start, int end){ Random r = new Random(); int index = start + r.nextInt(end - start); if (end - start > 3){ int a = start + r.nextInt(end - start); int b = start + r.nextInt(end - start); int c = start + r.nextInt(end - start); index = nums[a] > nums[b] ? (nums[a] < nums[c] ? a : (nums[b] > nums[c] ? b : c)) : (nums[b] < nums[c] ? b : c); } swap(nums, index, end); return partition(nums, start, end); } publicintpartition(int[] nums, int start, int end){ int x = nums[end]; int i = start-1; for (int j = start; j <= end-1; j++) { if (nums[j] <= x){ i++; swap(nums, i, j); } } swap(nums, i+1, end); return i+1; }
publicvoidquick3Way(int[] nums, int start, int end){ if (end <= start) return; int x = nums[start]; //这里可以随机选取或者三数取中选取拆分元素 int lt = start, i = start+1, gt = end; while (i <= gt) { if (nums[i] < x) swap(nums, lt++, i++); elseif (nums[i] > x) swap(nums, i, gt--); else i++; } quick3Way(nums, start, lt-1); quick3Way(nums, gt+1, end); }
publicclassHeapsort{ privateint heapSize; publicvoidheapSort(int[] a){ heapSize = a.length - 1; buildMaxHeap(a); for (int i = heapSize; i >= 2; i--) { swap(a, 1, i); heapSize--; maxHeapify(a, 1); } } publicvoidbuildMaxHeap(int[] a){ for (int i = (heapSize>>1); i >= 1; i--) maxHeapify(a, i); } //maxHeapify的作用是:维护第i个元素最大堆性质,左右孩子都比它小 publicvoidmaxHeapify(int[] a, int i){ int l = 2 * i; //左子结点坐标 int r = 2 * i + 1; //右子结点坐标 int largest = i; //最大元素坐标初始值为i if (l <= heapSize && a[l] > a[i]) largest = l; if (r <= heapSize && a[r] > a[largest]) largest = r; if (largest != i) { swap(a, i, largest); maxHeapify(a, largest); } } publicvoidswap(int[] nums, int a, int b){ int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; } }
publicvoidbubblesort1(int[] nums){ int flag = 0; for (int i = nums.length-1; i >= 1; i--) { flag = 0; for (int j = 0; j < i; j++) { if (nums[j] > nums[j+1]) { swap(nums, j, j+1); flag = j+1; } } i = flag; } }
publicclassSelectionsort{ publicvoidselectionsort(int[] nums){ int max = 0; for (int i = nums.length-1; i >= 1; i--) { max = 0; for (int j = 0; j <= i; j++) { if (nums[j] > nums[max]) { max = j; } } swap(nums, i, max); } } privatevoidswap(int[] nums, int a, int b){ int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; } }