Thoughts on a fast sorting error (1)

Source: Internet
Author: User

Thoughts on a fast sorting error (1)
Quick sorting is the best average performance in keyword-based internal sorting algorithms. It adopts the grouping policy, which is both an advantage of quick sorting and Its disadvantage. From the algorithm description of quick sorting, we can find that it has a recursive structure: (1) determine a boundary and divide the array to be sorted into two parts: Left and Right; (2) move all data smaller (larger) than the critical value to the left, and data larger (smaller) than the critical value to the right; (3) at this time, the left and right parts become two independent arrays, respectively performing (1) (2) (3) operations on them until all the data is in an orderly state. According to this description, it is not difficult for us to write the code for quick sorting. I usually encounter sorting problems. As long as the data volume reaches 100, if I don't want to think about it, I will use quick sorting to solve it, however, when I use the following program for testing, the problem occurs:

1 # include <stdio. h> 2 # include <time. h> 3 # include <stdlib. h> 4 5 # define NUM 10000000/* data volume to be sorted */6 7 void quick_sort (double a [], long left, long right); 8 9 int main (void) 10 {11 clock_t t_s, t_e; 12 long I; 13 double a [NUM]; 14 15 srand (time (NULL); 16 for (I = 0; I <NUM; + + I) {17 a [I] = rand (); 18} 19 20 t_s = clock (); 21 quick_sort (a, 0, NUM-1 ); 22 t_e = clock (); 23 double t = (t_e-t_s)/(double) CLOCKS_PER_SEC; /* calculate the order time */24 25 printf ("Quick sort % d items used time: % f s \ n", NUM, t); 26 27 return 0; 28} 29 30 void quick_sort (double a [], long left, long right) 31 {32 long I = left; 33 long j = right; 34 double mid = a [(I + j)/2];/* use the intermediate element as the benchmark for comparison */35 36 while (I <= j) {37 while (a [I] <mid) 38 ++ I; 39 while (mid <a [j]) 40 -- j; 41 if (I <= j) {42 double t = a [I]; 43 a [I] = a [j]; 44 a [j] = t; 45 ++ I; 46 -- j; 47} 48} 49 50 if (I <right) quick_sort (a, I, right); 51 if (left <j) quick_sort (a, left, j); 52}

 

When I run this program on Linux, the "Segmentation fault" error occurs, but this error does not occur when NUM = 1000000. Check the relevant information to learn that this is because the program has too many recursion times. A large number of pressure stacks make the stack space occupied by the program exceed the size specified by the operating system, resulting in memory errors. The result obtained by using the ulimit-s command is 8192. That is to say, my system allocates about 8 MB of stack space to each program by default. Run the ulimit-s unlimited command to change the stack space to the actual memory size, the above program can run smoothly without errors (because Linux does not use Windows to write the stack size to the executable file, so you can only use the ulimit-s method to change it ). Is there a limit on the amount of data that can be processed by quick sorting due to stack restrictions? It would be better to use selective sorting-although it is slow, but at least there will be no errors, so I found this article: Non-Recursive Implementation of Fast sorting. In fact, it is "non-recursive", but the stack managed by the user is used to eliminate recursion. The algorithms are essentially no different, and from the test by the author of this article, the method of using Stacks is slower than the method of recursion (the author explains it as: "the efficiency of using Stacks is higher than that of recursion, however, in this program, the local variables, that is, there is very little data to be pressed each time. The advantages of the stack are not shown, but they are slower ......", I think this is wrong. Because recursion can be understood as having a "stack that the system automatically manages for you", its efficiency must be higher than the stack you manage, besides, you call a new function during the stack play and stack pressure operations to calculate the call expenses. The stack method must be slower than recursion ), however, the advantage of stack here is that you do not need to consider the operating system issues, and the data volume that can be processed is only related to the memory size, the size of the stack space does not have to be limited by the operating system (that is, the stack is used, and the fast sorting is much faster than many sorting algorithms ). In the past, I used to talk about how to select a proper sorting algorithm based on actual problems. However, in my figure, we only use quick sorting and simple sorting. Having encountered this problem, I also had a better understanding of algorithm selection and implementation, and I also learned that using stacks to eliminate recursion is in some cases (such as limited system stack space).

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.