Knowledge in quick sorting: randomization fast sorting

Source: Internet
Author: User
In general, the random selection of pivot elements is very safe unless the random number generator has a problem (this is not as rare as you think ), because random hub elements cannot always produce inferior segmentation. On the other hand, the generation of random numbers is generally expensive, and the average running time of the rest of the algorithm cannot be reduced. The algorithm is similar to the example in the previous Introduction to Algorithms. you only need to add a random number to the Partition call. for details, see

The previous article discussed several methods to select the hub element. In fact, the second method is to randomly select the element as the hub element. In this article, we will implement a randomization sorting.

The algorithm is similar to the preceding example in Introduction to Algorithms. you only need to add a random number to the Partition call. For more information, see the program.

C code:

# Include "stdio. h "# include" math. h "# include" stdlib. h "int num = 10; void swap (int * a, int * B) {int tmp; tmp = * a; * a = * B; * B = tmp ;} void PrintArray (int arr []) {int I; for (I = 0; I <num; ++ I) {printf ("% d ", arr [I]) ;}} int Partition (int * arr, int beg, int end) {int j; int sentinel = arr [end]; int I = beg-1; for (j = beg; j <= end-1; ++ j) {if (arr [j] <= sentinel) {I ++; swap (& arr [I], & arr [j]) ;}} swap (& arr [I + 1], & arr [end]); printf ("\ n sorting process:"); PrintArray (arr); return I + 1;} int RandomPartition (int * arr, int beg, int end) {int I = beg + rand () % (end-beg + 1); swap (& arr [I], & arr [end]); return Partition (arr, beg, end);} void RandomQuickSort (int * arr, int beg, int end) {if (beg <end) {int partition = RandomPartition (arr, beg, end ); printf ("\ n random select arr [% d] (% d)", Random, arr [random]); RandomQuickSort (arr, beg, random-1 ); printf ("\ n random select arr [% d] (% d)", Random, arr [random]); RandomQuickSort (arr, random + 1, end );}} int main () {int I; int arr [10]; srand (time (0); for (I = 0; I <10; I ++) {arr [I] = rand () % 100 + 1; // printf ("% d", rand () % 100 + 1);} printf ("initial array: "); PrintArray (arr); RandomQuickSort (arr, 0, num-1); printf (" \ n Final result: "); PrintArray (arr); return 0 ;}

Program running result:

Initial array: 79 36 68 39 10 96 60 84 21 sorting process: 79 36 68 39 10 59 60 21 84 96 random selection arr [8] (84) sorting process: 21 10 36 39 79 59 60 68 [84] 96 random selection arr [2] (36) sorting process: 10 21 [36] 39 79 59 60 68 84 96 random select arr [1] (21) random select arr [1] (21) random select arr [2] (36) sorting process: 10 21 [36] 39 79 60 68 84 96 96 random selection arr [3] (39) random selection arr [3] (39) sorting process: 10 21 36 [39] 68 59 60 79 84 96 random selection arr [7] (79) sorting process: 10 21 36 39 60 59 68 [79] 84 96 random selection arr [6] (68) sorting process: 10 21 36 39 59 60 [68] 79 84 96 random select arr [4] (59) random select arr [4] (59) random select arr [6] (68) random selection of arr [7] (79) random selection of arr [8] (84) final result: 10 21 36 39 59 60 68 79 [84] 96 Process returned 0 (0x0) execution time: 0.582 sPress any key to continue.

In general, the random selection of pivot elements is very safe unless the random number generator has a problem (this is not as rare as you think ), because random hub elements cannot always produce inferior segmentation. On the other hand, the generation of random numbers is generally expensive, and the average running time of the rest of the algorithm cannot be reduced.

For example, the running result of the above program shows that the generation of many random numbers does not have an effective effect on sorting, and it takes a lot of time to generate these random numbers. Of course, you can also choose to optimize the random number generator, which will lead to more research.

Additional reading

The topic list of this article is as follows:

  1. Knowledge in quick sorting: start with guesses
  2. Knowledge in quick sorting: Let's look at the question of ball.
  3. Knowledge in quick sorting: Information entropy
  4. Knowledge in quick sorting: the process of quick sorting
  5. Knowledge in quick sorting: Hall and quick sorting
  6. Knowledge in quick sorting: Implementation of Hall's fast sorting
  7. Knowledge in quick sorting: Key element selection and algorithm efficiency
  8. Knowledge in quick sorting: randomization fast sorting

This article is available at http://www.nowamagic.net/librarys/veda/detail/2398.

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.