Quick Sort)

Source: Internet
Author: User

QuickSort is also a Sort Algorithm for input arrays containing n arrays. The worst run time is O (n ^ 2 ). Although the Run Time in the worst case is relatively poor, fast sorting is usually the best practical choice for sorting, because its average performance is quite good and the expected run time is O (nlgn ), in addition, the constant factor hidden in O (nlgn) is very small, and it can also perform local sorting in a virtual environment. I. Fast sorting principle the same is true for fast sorting and Merge Sorting. Based on the division and control method, there are three steps: decomposition, solution, and merge. decomposition: array [low... high] is divided into two (possibly empty) Sub-array [low... temp-1] and array [temp + 1... high] to make array [low... every element in temp-1] is less than or equal to array [temp], while array [temp + 1... every element in high] is greater than array [temp], and the subscript temp is also calculated in this process. Solution: Use recursive call to quickly sort the sub-array [low... temp-1], array [temp + 1... high] sorting; merge: because the two sub-arrays are sorted in place, merging them does not require any operation. The entire array [low... high] is sorted. II. Implementation of quick sorting [cpp] # include <iostream> # include <ctime> # include <cstdlib> # define N 10 using namespace std; // The recursive algorithm void quickSort (int * array, int low, int high) for fast sorting; // calculate the split point int partition (int * array, int low, int high ); // exchange the values of two variables void exchange (int & a, int & B); int main () {// declare an array to be sorted int array [N]; // set the Randomization seed to avoid generating the same random number srand (time (0) each time; for (int I = 0; I <N; I ++) {array [I] = rand () % 101; // use the random function to assign values to arrays. Number Generation random number between 1-} cout <"Before sorting:" <endl; for (int j = 0; j <N; j ++) {cout <array [j] <";}cout <endl <" after sorting: "<endl; // call the quick sort function to sort the array quickSort (array, 0, N-1); for (int k = 0; k <N; k ++) {cout <array [k] <";}cout <endl; return 0 ;}// main void quickSort (int * array, int low, int high) {if (low

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.