6. Quick sorting of comparison sorts

Source: Internet
Author: User
Tags sorts

Quick sort (short) because of its high efficiency (average O (Nlogn)) is often examined in the written question.

The first step in the fast line is to select a "cardinality" that will be exchanged with the other numbers using this "base". and this "base" choice will affect the efficiency of the fast row, but if you choose the cardinality to choose the base will put the cart before the horse. For example, to find the best cardinality, you need to find the median in the entire backlog, but finding the median is actually expensive. The selection of cardinality is usually the first object in the sorted sequence or an object in the middle or the last object. In this paper, we take the first element as an example to make a brief analysis of the fast row.

Select the first element 7 as the cardinality, taking the waiting sequence {6, 5, 3, 1, 2, 4, 6} as an example.

After selecting the cardinality, you need to compare and contrast the array elements, how to compare and who to compare? The second step is to set a "sentinel" for each of the first and last elements of the array.

Choose a good base, set up the Sentinel after the next is to start the comparison, the base is compared with the last Sentinel J, if greater than Sentinel J and exchange with the Sentinel I+1.

At this point the cardinality is no longer compared to Sentinel J, but compared to Sentinel I, if the cardinality is greater than Sentinel I, then the Sentinel will move backwards until greater than the base exchange while the Sentinel J-1.

Repeat the above steps, and the cardinality is compared to Sentinel J.

The final result shows the position of sentinel i = the position of Sentinel J, at which point the cardinality is assigned to this position.

In this way, the number to the left of the base 6 is less than it, the number on the right is greater than it, then the same steps are used recursively to select the cardinality, set the Sentinel, and then complete the sorting.

  Java

1  PackageCom.algorithm.sort.quick;2 3 Importjava.util.Arrays;4 5 /**6 * Quick Sort7 * Created by Yulinfeng on 2017/6/26.8  */9  Public classQuick {Ten      Public Static voidMain (string[] args) { One         int[] Nums = {6, 5, 3, 1, 7, 2, 4}; ANums = QuickSort (nums, 0, Nums.length-1); - System.out.println (arrays.tostring (nums)); -     } the      -     /** - * Quick Sort -      * @paramnums array sequence to sort +      * @paramThe first element index of the left array -      * @paramRight array last element index +      * @returnsequence of sequential arrays A      */ at     Private Static int[] QuickSort (int[] Nums,intLeftintRight ) { -         if(Left <Right ) { -             inttemp = Nums[left];//cardinality -             inti = left;//Sentinel I -             intj = right;//Sentinel J -              while(I <j) { in                  while(I < J && Nums[j] >=temp) { -j--; to                 } +                 if(I <j) { -Nums[i] =Nums[j]; thei++; *                 } $                  while(I < J && Nums[i] <temp) {Panax Notoginsengi++; -                 } the                  while(I <j) { +NUMS[J] =Nums[i]; Aj--; the                 } +             } -Nums[i] =temp; $QuickSort (Nums, left, i-1); $QuickSort (Nums, i + 1, right); -         } -         returnnums; the     } -}

Python3

1 #Quick Sort2 defQuick_sort (Nums, left, right):3     ifLeft <Right :4temp = Nums[left]#cardinality5i = Left#Sentinel I6j = Right#Sentinel J7          whileI <J:8              whileI < J andNUMS[J] >=Temp:9J-= 1Ten             ifI <J: OneNums[i] =Nums[j] Ai + = 1 -              whileI < J andNums[i] <Temp: -i + = 1 the             ifI <J: -NUMS[J] =Nums[i] -J-= 1 -Nums[i] =Temp +Quick_sort (Nums, left, i-1) -Quick_sort (Nums, i + 1, right) +      A     returnNums at  -Nums = [6, 5, 3, 1, 7, 2, 4] -Nums = Quick_sort (nums, 0, Len (nums)-1) - Print(nums)

6. Quick sorting of comparison sorts

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.