In quick sort no. of swappings :
case 1) [1,2,3,4,5] , last element as pivot
no. of swappings = 5+4+3+2+1 {when 5 is pivot then 1,2,3,4 are swapped with itself, similarly for others}
= (n-1)+(n-2)+.......+2+1
= (n(n-1))/2
= O(n^2)
case 2)[5,4,3,2,1] , last element as pivot
no. of swappings = 1 + 1 + 1 + 1 + 1
= (n)
=O(n)
eg: [5,4,3,2,1] {1 is pivot, 1 and 5 will be swapped}
[1] [4,3,2,5] {5 is pivot, 5 will be swapped with itself}
[1] [4,3,2] [5] {2 is pivot, 4 and 2 will be swapped}
[1] [2][3,4] [5] {4 is pivot, 4 will be swapped with itself}
[1] [2] [3] [4] [5] {3 is pivot, 3 will be swapped with itself}
case 3) [2,2,2,2,2], last element as pivot
no. of swappings = 1 + 1 + 1+ 1 +1
= n
= O(n)
Am I correct ???? and in best case is no of swappings = O(log n) ???