Sorting algorithm (v)--quick sort

Source: Internet
Author: User

Basic ideas

Fast sequencing is also based on the divide-and-conquer algorithm. The steps are as follows:

(1) Select a datum element, usually select the first element or the last element;

(2) The records to be sorted by a trip are divided into two separate parts, and some of the recorded element values are smaller than the datum element values. The element value of another part of record is larger than the Datum value;

(3) The Datum element is in the correct position after the sequence;

(4) The two parts of the record are then continued to be sorted in the same way until the entire sequence is ordered.


, the first round of the fast sorting process, first select the first element as a datum point, starting from the right side of the first element to scan, find the first element smaller than 57 (19) Stop, the two swap positions, and then start scanning from the left side , find the first ratio of 57 The large element (68) stops, the two exchange positions, and the cycle continues until 57 cannot find the exchangeable element until the end of this round of quick sorting.

At this point, more than 57 of the elements are on the left, more than 57 of the elements are on the right side, respectively, the array segments on both sides continue to be quickly sorted, and so on, and finally the whole array in order.


Java implementation

Quick sort public void Quiksort () {recursivequiksort (0,array.length-1); }/** * Recursive quick sort * @param the lowest subscript of the low array * @param the maximum subscript for the high array */private void Recursiv             Equiksort (int low,int high) {if (Low>=high) {return;  }else{int pivot = Array[low];  The first element is the Datum int partition =partition (low,high,pivot);                                        The array is divided, the elements smaller than pivot are in the lower segment, the elements larger than pivot are in the high segment display ();  Recursivequiksort (low,partition-1);  A fast sequencing recursivequiksort (Partition+1,high) is performed for the lower segment after partitioning. Quickly sort the divided high-order segment}}/** * Divide the subscript low to high array with pivot as a benchmark * @param the minimum subscript for the low array segment * @param the maximum subscript for the high array segment * @param pivot element of the division * @return the subscript for the position of the datum element when the partition is complete */private int partition (int l               Ow,int High,int pivot) {while (Low
  

Algorithm analysis

In the merge sort, we calculate the time complexity in detail, the fast sort and the merge sort take the divide and conquer algorithm, its time complexity is also O (n*log2n).

This is generally the case for the split algorithm, which divides the data items into two groups by recursive method, and then calls itself to process each set of data separately. The algorithm is actually based on 2, and the running time is proportional to the n*log2n .

For fast sorting, the ideal state is a randomly distributed data, that is, our arbitrarily selected pivot is in the middle, half of the element is less than it, and half of the element is greater than it. When data is arranged from small to large or from large to small, the efficiency of fast sorting is the lowest and the time complexity is increased to O (N2).

It is really easy to select the first element for a hub, but when it is the maximum or minimum, the efficiency of the quick sort is severely reduced. If the selected element is the median of an array, it is naturally the best choice, but it is possible to iterate through the arrays to determine the median value, a process that may take longer than the sorting time and outweigh the costs. The tradeoff is to find the first, last, and middle elements of the array, selecting the median of the three as the pivot, avoiding the pivot being the most value, and not having to look for the median value as it would be in all elements. This method is referred to as the " three-entry method " (Median-of-three).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sorting algorithm (v)--quick sort

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.