How does random shuffling in quick sort help in increasing the efficiency of the code?

前端 未结 5 987
忘掉有多难
忘掉有多难 2020-12-28 20:00

I was going through lecture videos by Robert Sedgwick on algorithms, and he explains that random shuffling ensures we don\'t get to encounter the worst case quadratic time s

5条回答
  •  执笔经年
    2020-12-28 20:19

    Is the video in coursera? Unfortunately, shuffle decrease performance to O(N^2) with data n,n,...,n,1,1,...,1. I have inspected Quick.java with nn11.awk that generate such data.

    $ for N in 10000 20000 30000 40000; do time ./nn11.awk $N | java Quick; done | awk 'NF>1'
    
    real    0m10.732s
    user    0m10.295s
    sys     0m0.948s
    
    real    0m48.057s
    user    0m44.968s
    sys     0m3.193s
    
    real    1m52.109s
    user    1m48.158s
    sys     0m3.634s
    
    real    3m38.336s
    user    3m31.475s
    sys     0m6.253s
    

提交回复
热议问题