Improving the Quick sort

前端 未结 14 1307
终归单人心
终归单人心 2021-02-05 22:33

If possible, how can I improve the following quick sort(performance wise). Any suggestions?

void main()
    {
      quick(a,0,n-1);
    }

    void quick(i         


        
14条回答
  •  有刺的猬
    2021-02-05 22:57

    Per your code, when sorted lenght is 10, the deepest stack looks like

    #0  partition (a=0x7fff5ac42180, lower=3, upper=5) 
    #1  0x000000000040062f in quick (a=0x7fff5ac42180, lower=3, upper=5) 
    #2  0x0000000000400656 in quick (a=0x7fff5ac42180, lower=0, upper=5) 
    #3  0x0000000000400644 in quick (a=0x7fff5ac42180, lower=0, upper=8) 
    #4  0x0000000000400644 in quick (a=0x7fff5ac42180, lower=0, upper=9) 
    #5  0x00000000004005c3 in main 
    

    Besides the algo itself, if we consider the system behavior such as stack activities as well, something else except normal call stack costs (push/pop) might downgrade the performance a lots, e.g. context switching if multi-task system, you know most OS are multi-task, and deeper the stack higher costs the switching, plus cache miss or cachline boundary across.

    I believe in this case if the length become bigger, you'll lose when comparing to some other sorting algos.

    for example, when length is up to 40, the stack may look like (may more entries than shown as below):

    #0  partition (a=0x7fff24810cd0, lower=8, upper=9) 
    #1  0x000000000040065d in quick (a=0x7fff24810cd0, lower=8, upper=9)  
    #2  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=8, upper=10) 
    #3  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=8, upper=14) 
    #4  0x0000000000400684 in quick (a=0x7fff24810cd0, lower=4, upper=14) 
    #5  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=4, upper=18) 
    #6  0x0000000000400684 in quick (a=0x7fff24810cd0, lower=0, upper=18) 
    #7  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=0, upper=26) 
    #8  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=0, upper=38) 
    #9  0x0000000000400672 in quick (a=0x7fff24810cd0, lower=0, upper=39) 
    #10 0x00000000004005ee in main  
    

    too deeper the stack.

    Function inlining is helpful too, but it increases code footprint of the final executable. I agree with @Swiss, parallel programming might help you so much.

提交回复
热议问题