/** * @author lautturi.com * Java example: */ import java.util.*; public class Lautturi { public static void main(String[] args) { int[] arr = { 1, 3, 7, 10, 4, 8, 9, 5, 6, 2 }; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr)); } static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, high); return (i + 1); } static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivot = partition(arr, low, high); quickSort(arr, low, pivot - 1); quickSort(arr, pivot + 1, high); } } }
output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]