Data structure---Quickly sort Java

Source: Internet
Author: User

Quick Sort (Quicksort) is an improvement to the bubbling sort.

Quick Sort by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.


set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. It is important to note that fast sorting is not a stable sorting algorithm, that is , the relative position of multiple identical values may change at the end of the algorithm.


32






32

12 3 15 25 20 24
24






24 12 3




24 12 3



45
24 12 3 20


45
24 12 3 20 15 25
45
24 12 3 20 15 25 32 45

For the first time, take the first number 32,

From right to left, until you find a number smaller than 32, take the first number 32 and the last number 24, smaller than 32, put 24 in the first position,

Then from left to right, until you find a number larger than 32,

Take the second number 12 and 32 than 32 small, continue to take the third number 3 with 32, smaller than 32, continue to take the fourth number 45 with 32, than 32, put 45 in the last position,

The second time, continue to follow this method, from both sides to the middle, until the left to find the right side, there is no number in the middle, put 32 into the position,

At this time, the 32 left is all smaller than him, the number on the right is bigger than him,


Then use this method, the 32 left all the number, take the first 24,12 as the first number, the last number 25 as the last number, continue than until the end,


Code: package com.wangyq.test;//Quick Sort Public class quick {public int[] quick (int[]  array,int left, int right) {int i = left;int j = right;if ( Left <right) {Int tmp = array[i];d o{while (array[j] > tmp && &NBSP;I&NBSP;&LT;&NBSP;J) {j--;} if (i < j) {array[i] = array[j];i++;} while (array[i] < tmp && i < j) {i++;} if (i < j) {array[j] = array[i];j--;}} while (I&NBSP;!=&NBSP;J); Array[i] = tmp;quick (array, left, i-1); quick (array, i+1,  right);} Return array;} Public static void main (String[] args)  {int[] array = { 32,  12, 3, 45, 15, 25, 20, 24 };array = new quick (). Quick (Array, 0,array.length-1);for  (INT&NBSP;I&NBSP;=&NBSP;0;&NBSP;I&NBSP;&LT;&NBSP;ARRAY.LENGTH;&NBsp;i++) System.out.print (array[i] +  "  ");}} 


This article is from the "Wangyq" blog, make sure to keep this source http://wangyq2013.blog.51cto.com/11978646/1870402

Data structure---Quickly sort Java

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.