Java implementation of fast sorting algorithm

Source: Internet
Author: User

1) by a trip sorting the 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 this method to the two parts of the data are quickly sorted, the entire sorting process can be recursively

Example:

Package Com.neuedu.algorithm;public class QuickSort {//Quick sort public static void main (String [] args) {int [] array= {323,6,6 6,78,23,567,-1}; System.out.println ("Before sorting:"); for (int i:array) {System.out.print (i+ "  ");} QuickSort (array, 0, array.length-1);}   private static void QuickSort (int[] arr, int _left, int _right) {   int left=_left;   int right=_right;   int temp = 0;if (left <= right) {   //the element to be sorted has at least two cases temp=arr[left];while (left!=right) {while (right>left& &ARR[RIGHT]>=TEMP)//Find the first number on the right that is less than the base right--;arr[left]=arr[right];while (Right>left&&arr[left] <=TEMP)//Find the first number on the left that is greater than the base left++;arr[right]=arr[left];           Arr[right] = temp;    Datum element homing           QuickSort (arr,_left,left-1);  Recursively sorts the elements to the left of the Datum element           QuickSort (arr, right+1,_right);  Recursively sort the base element to the right of       }}}      

  

Analysis

The time of fast sorting is mainly spent on the division operation, the interval of length k is divided, the total need k-1 the comparison of the key words.

The worst-case scenario is that each time the selected datum is the smallest (or largest) record of the keyword in the current unordered region, the result is that the sub-interval to the left of the Datum is empty (or the right sub-interval is empty), and the number of records in another non-empty sub-interval is divided by only one less than the number of records Time Complexity of O (n*n)

In the best case, the datum for each partition is the "median" record of the current unordered region, and the result is that the length of the two unordered sub-ranges of the datum is roughly equal to the left and right. Total number of keyword comparisons: O (NLGN)

Although the worst time for fast sorting is O (N2), in terms of average performance, it is the fastest in the internal sorting algorithm based on the keyword comparison, and hence the name of the fast sort. Its average time complexity is O (NLGN).

Java implementation of fast sorting algorithm

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.