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

前端 未结 3 676
迷失自我
迷失自我 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:42

    Hoare’s Quicksort algorithm chooses a random pivot. For reproducible results I'd suggest Scowen's modification which, among other things, chooses the middle item from the input. For this variant a sawtooth pattern with the pivot being the smallest appears to be the worst case input:

    starting with           {  j   i   h   g   f   a   e   d   c   b  }
    compare 1 to 6          { (j)  i   h   g   f  (a)  e   d   c   b  }
    compare 6 to 10         {  j   i   h   g   f  (a)  e   d   c  (b) }
    compare 6 to 9          {  j   i   h   g   f  (a)  e   d  (c)  b  }
    compare 6 to 8          {  j   i   h   g   f  (a)  e  (d)  c   b  }
    compare 6 to 7          {  j   i   h   g   f  (a) (e)  d   c   b  }
    swap 1<=>6              { (a)  i   h   g   f  (j)  e   d   c   b  }
    compare 1 to 5          { (a)  i   h   g  (f)  j   e   d   c   b  }
    compare 1 to 4          { (a)  i   h  (g)  f   j   e   d   c   b  }
    compare 1 to 3          { (a)  i  (h)  g   f   j   e   d   c   b  }
    compare 1 to 2          { (a) (i)  h   g   f   j   e   d   c   b  }
    compare 2 to 6          {  a  (i)  h   g   f  (j)  e   d   c   b  }
    compare 3 to 6          {  a   i  (h)  g   f  (j)  e   d   c   b  }
    compare 4 to 6          {  a   i   h  (g)  f  (j)  e   d   c   b  }
    compare 5 to 6          {  a   i   h   g  (f) (j)  e   d   c   b  }
    compare and swap 6<=>10 {  a   i   h   g   f  (b)  e   d   c  (j) }
    compare 7 to 10         {  a   i   h   g   f   b  (e)  d   c  (j) }
    compare 8 to 10         {  a   i   h   g   f   b   e  (d)  c  (j) }
    compare 9 to 10         {  a   i   h   g   f   b   e   d  (c) (j) }
    compare 2 to 6          {  a  (i)  h   g   f  (b)  e   d   c   j  }
    compare 6 to 9          {  a   i   h   g   f  (b)  e   d  (c)  j  }
    compare 6 to 8          {  a   i   h   g   f  (b)  e  (d)  c   j  }
    compare 6 to 7          {  a   i   h   g   f  (b) (e)  d   c   j  }
    swap 2<=>6              {  a  (b)  h   g   f  (i)  e   d   c   j  }
    compare 2 to 5          {  a  (b)  h   g  (f)  i   e   d   c   j  }
    compare 2 to 4          {  a  (b)  h  (g)  f   i   e   d   c   j  }
    compare 2 to 3          {  a  (b) (h)  g   f   i   e   d   c   j  }
    compare 3 to 6          {  a   b  (h)  g   f  (i)  e   d   c   j  }
    compare 4 to 6          {  a   b   h  (g)  f  (i)  e   d   c   j  }
    compare 5 to 6          {  a   b   h   g  (f) (i)  e   d   c   j  }
    compare and swap 6<=>9  {  a   b   h   g   f  (c)  e   d  (i)  j  }
    compare 7 to 9          {  a   b   h   g   f   c  (e)  d  (i)  j  }
    compare 8 to 9          {  a   b   h   g   f   c   e  (d) (i)  j  }
    compare 3 to 6          {  a   b  (h)  g   f  (c)  e   d   i   j  }
    compare 6 to 8          {  a   b   h   g   f  (c)  e  (d)  i   j  }
    compare 6 to 7          {  a   b   h   g   f  (c) (e)  d   i   j  }
    swap 3<=>6              {  a   b  (c)  g   f  (h)  e   d   i   j  }
    compare 3 to 5          {  a   b  (c)  g  (f)  h   e   d   i   j  }
    compare 3 to 4          {  a   b  (c) (g)  f   h   e   d   i   j  }
    compare 4 to 6          {  a   b   c  (g)  f  (h)  e   d   i   j  }
    compare 5 to 6          {  a   b   c   g  (f) (h)  e   d   i   j  }
    compare and swap 6<=>8  {  a   b   c   g   f  (d)  e  (h)  i   j  }
    compare 7 to 8          {  a   b   c   g   f   d  (e) (h)  i   j  }
    compare 4 to 6          {  a   b   c  (g)  f  (d)  e   h   i   j  }
    compare 6 to 7          {  a   b   c   g   f  (d) (e)  h   i   j  }
    swap 4<=>6              {  a   b   c  (d)  f  (g)  e   h   i   j  }
    compare 4 to 5          {  a   b   c  (d) (f)  g   e   h   i   j  }
    compare 5 to 6          {  a   b   c   d  (f) (g)  e   h   i   j  }
    compare and swap 6<=>7  {  a   b   c   d   f  (e) (g)  h   i   j  }
    compare and swap 5<=>6  {  a   b   c   d  (e) (f)  g   h   i   j  }
    

提交回复
热议问题