Java Sorting Algorithm (5): quick sorting and java Sorting Algorithm

Source: Internet
Author: User

Java Sorting Algorithm (5): quick sorting and java Sorting Algorithm

Java Sorting Algorithm (5): Fast sorting

Fast sorting is a very fast exchange sorting algorithm. Its basic idea is very simple. Any data (such as the first data) in the data sequence to be sorted is used as the demarcation value, put all elements smaller than it to the left. Put all the elements larger than it to the right. After such a trip, the sequence forms two subsequences. The values of the data elements in the left sequence are smaller than the demarcation values, and those in the right sequence are larger than the demarcation values. Next, recursively sort the Left and Right sequences. Re-select the central element for the two subsequences and adjust it according to this rule. The sorting is successful until only one element is left in each element sub-table.

Ideas

1. Define a variable I, starting from the first index on the left, to find the index of the element greater than the demarcation value, and record it with I.

2. Define a j variable. The j variable starts from the first index on the right, finds the index of the element smaller than the demarcation value, and records it with j.

3. If I <j, the elements at the I and j indexes are exchanged

Repeat steps 1, 2, and 3 until I> = j. You can determine that the data elements on the left of j are smaller than the demarcation value, and the data elements on the Right of j are greater than the demarcation value, finally, we can exchange the elements at the j index.

Time Complexity

Best case (always select the center pivot) Tn = o (nlogn)

Worst case: (the smallest or largest element is always selected as the pivot)

Perform n-1 queries and compare n-I times each time. The total number of comparisons is the maximum; [o (n2)]

The average time complexity is T (n) = o (logn)

Code Implementation

`Package com. spring. test; import com.sun.org. apache. bcel. internal. generic. SWAP; import java. awt. print. printerGraphics;/*** quick sorting */public class QuickSortTest {public static void main (String [] args) {int [] data = new int [] {5, 3, 6, 2, 1, 9, 4, 8, 7}; print (data); quickSort (data, 0, data. length-1); System. out. println ("sorted array"); print (data );} /*** quick sorting ** @ param data * @ param start * @ param end */public static void quickSort (int [] data, int start, int end) {if (start> = end) {return;} // int point = data [start]; int I = start + 1; int j = end; while (true) {while (I <end & data [I] <point) {I ++;} while (I> start & data [j]> point) {j --;} if (I <j) {swap (data, I, j) ;}else {break ;}// exchange the value of j and the demarcation point swap (data, start, j); print (data); // recursion in the subsequence quickSort (data, start, J-1); quickSort (data, j + 1, end );} /*** exchange data */public static void swap (int [] data, int I, int j) {if (I = j) {return ;} data [I] = data [I] + data [j]; data [j] = data [I]-data [j]; data [I] = data [I]-data [j];}/*** print Output */public static void print (int [] data) {for (int I = 0; I <data. length; I ++) {System. out. print (data [I] + "\ t");} System. out. println ();}}`

Running result

Related Keywords:
Related Article

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.