1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.code.note.test;
import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.concurrent.locks.ReentrantLock;
public class Main {
public void quickSort(int[] arr, int left, int right) {
if (left >= right) return;
int p = partition(arr, left, right); quickSort(arr, left, p - 1); quickSort(arr, p + 1, right); }
private int partition(int[] arr, int left, int right) { int pivot = arr[left]; int l = left; int r = right; while (l < r) { while (arr[l] < pivot && l < r) { l++; }
while (arr[r] > pivot && l < r) { r--; } swap(arr, l, r); }
swap(arr, l, r); return r; }
private void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }
public static void main(String[] args) { Main m = new Main(); int[] arr = {6, 5, 2, 7, 3, 9, 8, 4, 10, 1}; m.quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr));
} }
|