public class BubberSort { public static void main(String[] args) { int[] numbers = {2, 6, 34, 23, 78, 34}; sort(numbers); System.out.println("done"); } public static void sort(int[] numbers) { for (int i = numbers.length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (numbers[j] > numbers[j + 1]) QuickSortL.swap(numbers, j, j + 1); } } } }
public class QuickSortL { public static void main(String[] args) { int[] numbers = {2, 3, 6, 4, 2, 45, 65}; quickSort(numbers, 0, 6); System.out.println("done----"); } public static void quickSort(int[] numbers, int low, int high) { if ((high - low) <= 0) return; int pivotIndex = partation(numbers, low, high); quickSort(numbers, low, pivotIndex - 1); quickSort(numbers, pivotIndex + 1, high); } private static int partation(int[] numbers, int low, int high) { int pivot = numbers[low]; int pivotIndex = low; if (low == high) return low; while (low != high) { while (numbers[high] >= pivot && high > low) high--; numbers = swap(numbers, pivotIndex, high); pivotIndex = high; while (numbers[low] < pivot && low < high) low++; numbers = swap(numbers, pivotIndex, low); pivotIndex = low; } numbers[low] = pivot; return low; } public static int[] swap(int[] numbers, int index1, int index2) { int temp = numbers[index1]; numbers[index1] = numbers[index2]; numbers[index2] = temp; return numbers; } }
浙公网安备 33010602011771号