Java enables quick sorting

Source: Internet
Author: User

An improvement of the bubble sort in the quick sort, if the initial record sequence is ordered or basically ordered by the keyword, degenerate to bubble sort.

Basic ideas

By a lying sort of data to be sorted into separate two parts, one part of all the data is smaller than the other part of all the data, and then the second method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

A trip to the sorting process is as follows:


Specific code

public class QuickSort {public static void main (string[] args) {//TODO auto-generated method stubint[] List = {4,6,1,7,9, 10,2,56,24,0,3}; QuickSort qs = new QuickSort () qs.quicksort (list, 0, list.length-1); for (int i=0; i<list.length; i++) { System.out.print (list[i]+ "");}} public void QuickSort (int[] list, int. Low, int.) {if (Low < high) {int middle = getmiddle (list, low, high); QuickSort (l ist, low, middle-1); QuickSort (list, middle+1, High);}} public int getmiddle (int[] list, int. Low, Int. high) {int tmp = list[low];//Select the first number as pivot while (low < high) {//Use an index to traverse the entire sequence from backward forward while (Low < high && List[high] > tmp) {high--;} List[low] = list[high];while (Low < high && List[low] < tmp) {low++;} List[high] = List[low];} List[low] = Tmp;return low;}}
Performance Analysis

The average performance is the best among all orders of the same order of magnitude O (NLONGN). In terms of average time, it is currently considered to be the best internal sorting method.

Improved

Pivot selection

Pivot selection has a direct effect on the performance of fast sorting, and the worst case scenario is the partitioning of two extremely asymmetric sub-sequences (one length of 1 and another length of n-1), at which point the complexity is O (n*n). You can select the median in the first and middle elements as the pivot, reducing the possibility of dividing the asymmetry.


Java enables quick sorting

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.