Fast sequencing of Algorithmic Research Java edition

Source: Internet
Author: User

Very early on has been approached the fast sorting algorithm, interview also repeatedly asked, although understand its principle, but never really use code to knock out.
Write about the code of the algorithm must be the principle to understand, or is blind, in reference to the relevant information and their own meditation, write the following code, the middle appeared some bugs, but all quickly resolved
If there is a better optimization algorithm, please do not hesitate to enlighten!!!!
Source:
Package com.zken.test;/** * @author iamzken * Sorting algorithm * Use quick sort algorithm to sort an array from small to large * 2015-8-27 13:40 */public class Sorter {//Get intermediate index , the index of the corresponding keyword divides the array into the left and right parts, where the left side of the index is smaller than the corresponding keyword, the other side of the index corresponding to the key is greater public static int Getmiddle (int[] A, int left, int right) {// Randomly take a keyword, where the left index corresponding to the keyword, equivalent to the left index corresponding to the location was dug a "pit", need the following program to fill this "pit" int key = a[left];//critical condition while (left < right) {// When right > left && a[right] > key indicates that the current right index corresponds to a key that is larger than key and does not need to be swapped while (right > A[right &&) > K EY) {right--;} The program can be executed here to show that the current right index of the corresponding keyword is smaller than key, need to swap, that is, fill "pit" a[left] = a[right];//when left < right && A[left] < Key indicates that the key for the current left index is smaller than key and does not need to be swapped while (< right && A[left] < key) {left++;} The program can be executed here to show that the current left index of the corresponding keyword larger than key, need to exchange, that is, fill "pit" a[right] = A[left];} The program jumps out of the while loop, indicating that left is equal to right, and the following two sentences can also be changed to a[right]=key;return right;a[left] = Key;return left;} public static void QuickSort (Int[] A, int. left, int. right) {//Critical condition if (left < right) {//Get intermediate index int middle = Getmiddle (A, L EFT, right);//The Left sub-array recursively sorts quicksort (A,left,MIDDLE-1);//recursively sort quicksort (A, middle+1, right) on the rightmost sub-array;}} Test program public static void Main (string[] args) {int[] a = new int[]{9,3,7,8,2,0,6}; SYSTEM.OUT.PRINTLN ("Array before quick sort:"), int i = 0;while (i < a.length) {System.out.print (a[i]+ "\ t"); i++;} System.out.println (); Sorter.quicksort (A, 0, a.length-1); System.out.println ("Fast sorted array:"); i = 0;while (i < a.length) {System.out.print (a[i]+ "\ t"); i++;}}


Execution Result:


The array before the quick sort:
9 3 78 2 0 6
Fast-sorted Array:
0 2 36 7 8 9

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fast sequencing of Algorithmic Research Java edition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.