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 52 53 54 55 56 57 58 59 60 61 62
| package com.zyg.sort;
public class QuickSortAlgorithm { public static void quickSort(int a[], int left, int right) { if (left < right) { int q = partition(a, left, right); <!--more--> quickSort(a, left, q - 1); quickSort(a, q + 1, right); } } public static int partition(int a[], int left, int right) { int pivot = a[left]; while (left < right) { while (left < right && pivot <= a[right]) right--; a[left] = a[right]; while (left < right && pivot >= a[left]) left++; a[right] = a[left]; } a[left] = pivot; return left; } public static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } public static void main(String[] args) { int a[] = { 4, 3, 6, 7, 33, 15, 90, 65 }; quickSort(a, 0, a.length - 1); printArray(a); } }
|