Fast sorting of algorithm notes

Source: Internet
Author: User

Fast sorting of algorithm notes

1.1Algorithm concept --

This algorithm selects an element in the array as the principal element (usually the first one), and then uses this principal element as the reference object to divide the array into two parts. The first part is less than or equal to the principal element, the second part is greater than or equal to the principal component. Use the quick sorting algorithm recursively for the first and second parts until they are assigned to the smallest group.

1.2Time Complexity --

In the worst case, we need to divide the arrays of n elements, which requires n comparisons and n moves. Suppose T (n) is used to represent the time it takes to sort arrays of n elements using a fast sorting algorithm. So

T (n) = T (n/2) + T (n/2) + 2n

Therefore, the time complexity of fast sorting should be T (n) = O (nlogn ).

1.3Program flowchart



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + sLrPC9zdHJvbmc + pc9wpgo8cd48c3ryb25np1_vc3ryb25npjxwcmugy2xhc3m9 "brush: java;"> package lin. algo; public class QuickSort {int [] list; public QuickSort (int [] list) {this. list = list;} public int [] quickSort () {quick (list, 0, list. length-1); return list;} private void quick (int [] list, int first, int last) {if (last> first) {int second xindex = partition (list, first, last); quick (list, first, pivoxIndex-1); quick (list, distinct xindex + 1, last );}} /*** divide the Array Based on the first element. * The input is the original array to be divided, the first element subscript and the last element subscript * @ param list * @ param first * @ param last * @ return the principal component subscript after the grouping */private int partition (int [] list, int first, int last) {// principal element int privot = list [first]; int secondIndex = first + 1; int lastIndex = last; // traverses the query, greater than the principal component on the right, less than on the left // implementation method: Check the two sides, the left is greater than the principal component and the right is less than the principal component of the exchange, // put the principal component in the middle of the two groups while (lastIndex> secondIndex) {// find the first while (secondIndex <= lastIndex & list [secondIndex] <= privot) on the left that is larger than the primary element {secondIndex ++ ;} // find the first while (secondIndex <= lastIndex & list [lastIndex]> privot) {lastIndex --;} on the right, if the sequence is not checked, it indicates that the sequence is found separately. if (secondIndex <lastIndex) {int temp = list [secondIndex]; list [secondIndex] = list [lastIndex]; list [lastIndex] = temp ;}} while (list [lastIndex] >=privot & lastIndex> first) {lastIndex --;} // return the new subscript of the primary element if (privot> list [lastIndex]) {list [first] = list [lastIndex]; list [lastIndex] = privot; return lastIndex ;} else {return first ;}}}


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.