Algorithm - Quick Sort(Java)

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

package chimomo.learning.java.algorithm.sort;

import java.util.Arrays;

/**
 * @author Chimomo
 *
 * 

* Introduction: * * Quick Sort is an improvement of Bubble Sort. *

* *

* Basic Thought: * * Quick Sort: * Separate the data to two independent parts by one round of sorting, * of which one part data is smaller than the other. * And then perform Quick Sort separately on the two parts, * the whole process can be recursive, * hereby the whole data becomes sorted sequence. * * One Trip Quick Sort: * Assume the array is a[0]...a[n-1], * first arbitrarily select one element as pivot * (usually set the first element as pivot), * then put all the elements smaller than the pivot before the pivot, * put all the elements bigger than the pivot after the pivot, * this process is called one trip of Quick Sort. *

* *

* Algorithm: * * One trip Quick Sort algorithm is designed as following: * 1. Set two variable i and j, i = 0, j = n - 1. * 2. Set the first element as pivot, i.e. pivot = a[i]. * 3. Search forward from j (j--), * find the first element that a[j] < pivot, * assign a[j] to a[i]. * 4. Search backward from i (i++), * find the first element that a[i] > pivot, * assign a[i] to a[j]. * 5. Repeat step3 and step4 until i == j. * Then, a[i] = pivot, return i. *

* *

* Complexity: * * The average time complexity of Quick Sort is O(nlog2n). *

* *

* Stability: * * Note that Quick Sort is not stable, * i.e. the relative positions of equal elements may changed after Quick Sort. *

*/ public class QuickSort { /** * Quick Sort * * @param a The array to be sorted * @param low The start position of sorting * @param high The end position of sorting */ private static void quickSort(int[] a, int low, int high) { if (low >= high) { return; } int pivot = oneTripQuickSort(a, low, high); // Print each trip of sorting System.out.println(Arrays.toString(a)); // Sort the left part of pivot quickSort(a, low, pivot - 1); // Sort the right part of pivot quickSort(a, pivot + 1, high); } /** * One trip of Quick Sort * * @param a The array to be sorted * @param low The start position of sorting * @param high The end position of sorting * @return The pivot position */ private static int oneTripQuickSort(int[] a, int low, int high) { int i = low; int j = high; // Set the first element as pivot int pivot = a[low]; while (i < j) { // From right to left, find the first element that less than pivot. while (a[j] >= pivot && i < j) { j--; } // Replace a[i] = a[j]; // From left to right, find the first element that greater than pivot. while (a[i] <= pivot && i < j) { i++; } // Replace a[j] = a[i]; } // Hereto, i must be equal to j. i (or j) is the pivot position. a[i] = pivot; // Return the pivot position return i; } /** * Test program * * @param args The arguments */ public static void main(String[] args) { int[] arr = {11, 16, 22, -3, 99, 0, 16, 6, -1, 99}; System.out.println("Before quick sort:"); System.out.println(Arrays.toString(arr)); System.out.println("In quick sort:"); QuickSort.quickSort(arr, 0, arr.length - 1); System.out.println("After quick sort:"); System.out.println(Arrays.toString(arr)); } } /* ------ Running Results ------ Before quick sort: [11, 16, 22, -3, 99, 0, 16, 6, -1, 99] In quick sort: [-1, 6, 0, -3, 11, 99, 16, 22, 16, 99] [-3, -1, 0, 6, 11, 99, 16, 22, 16, 99] [-3, -1, 0, 6, 11, 99, 16, 22, 16, 99] [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99] [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99] [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99] After quick sort: [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99] */

 

你可能感兴趣的:(Algorithm)