Observing quadratic behavior with quicksort - O(n^2)

前端 未结 3 651
迷失自我
迷失自我 2020-12-06 18:24

The quicksort algorithm has an average time complexity of O(n*log(n)) and a worst case complexity of O(n^2).

Assuming some variant of Hoare’s quicksort algorithm, wh

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 18:38

    To expand on what Bragboy said, instead of only running:

    quicksort(array);
    

    Run:

    shuffle(array);
    quicksort(array);
    

    Where the definition of shuffle() could be:

    shuffle(array){
        for(int i = array.length; i > 0; i--){
            r= random number % i;
            swap(array[i], array[r]);
        }
    }
    

    Doing so will, likely, deal with the case of getting input which makes quicksort() slow.

提交回复
热议问题