Home » DAA Quick Sort

DAA Quick Sort

by Online Tutorials Library

Quick sort

It is an algorithm of Divide & Conquer type.

Divide: Rearrange the elements and split arrays into two sub-arrays and an element in between search that each element in left sub array is less than or equal to the average element and each element in the right sub- array is larger than the middle element.

Conquer: Recursively, sort two sub arrays.

Combine: Combine the already sorted array.

Algorithm:

Partition Algorithm:

Partition algorithm rearranges the sub arrays in a place.

Figure: shows the execution trace partition algorithm

DAA Quick sort

Example of Quick Sort:

Let 44 be the Pivot element and scanning done from right to left

Comparing 44 to the right-side elements, and if right-side elements are smaller than 44, then swap it. As 22 is smaller than 44 so swap them.

  2233115577904060994488 

Now comparing 44 to the left side element and the element must be greater than 44 then swap them. As 55 are greater than 44 so swap them.

2233114477904060995588 

Recursively, repeating steps 1 & steps 2 until we get two lists one left from pivot element 44 & one right from pivot element.

2233114077904460995588 

Swap with 77:

2233114044907760995588 

Now, the element on the right side and left side are greater than and smaller than 44 respectively.

Now we get two sorted lists:

DAA Quick sort

And these sublists are sorted under the same process as above done.

These two sorted sublists side by side.

DAA Quick sort
DAA Quick sort

Merging Sublists:

DAA Quick sort

                          SORTED LISTS

Worst Case Analysis: It is the case when items are already in sorted form and we try to sort them again. This will takes lots of time and space.

Equation:

T (1) is time taken by pivot element.

T (n-1) is time taken by remaining element except for pivot element.

N: the number of comparisons required to identify the exact position of itself (every element)

If we compare first element pivot with other, then there will be 5 comparisons.

It means there will be n comparisons if there are n items.

DAA Quick sort

Relational Formula for Worst Case:

DAA Quick sort

Note: for making T (n-4) as T (1) we will put (n-1) in place of ‘4’ and if
We put (n-1) in place of 4 then we have to put (n-2) in place of 3 and (n-3)
In place of 2 and so on.

T(n)=(n-1) T(1) + T(n-(n-1))+(n-(n-2))+(n-(n-3))+(n-(n-4))+n
T (n) = (n-1) T (1) + T (1) + 2 + 3 + 4+…………n
T (n) = (n-1) T (1) +T (1) +2+3+4+………..+n+1-1

[Adding 1 and subtracting 1 for making AP series]

T (n) = (n-1) T (1) +T (1) +1+2+3+4+…….. + n-1
T (n) = (n-1) T (1) +T (1) + DAA Quick sort-1

Stopping Condition: T (1) =0

Because at last there is only one element left and no comparison is required.

T (n) = (n-1) (0) +0+DAA Quick sort-1

DAA Quick sort

Worst Case Complexity of Quick Sort is T (n) =O (n2)

Randomized Quick Sort [Average Case]:

Generally, we assume the first element of the list as the pivot element. In an average Case, the number of chances to get a pivot element is equal to the number of items.

So in general if we take the Kth element to be the pivot element.

Then,

DAA Quick sort

Pivot element will do n comparison and we are doing average case so,

DAA Quick sort

So Relational Formula for Randomized Quick Sort is:

 DAA Quick sort     = n+1 +DAA Quick sort(T(0)+T(1)+T(2)+...T(n-1)+T(n-2)+T(n-3)+...T(0))
= n+1 +DAA Quick sortx2 (T(0)+T(1)+T(2)+...T(n-2)+T(n-1))

Put n=n-1 in eq 1

From eq1 and eq 2

n T (n) – (n-1) T (n-1)= n(n+1)-n(n-1)+2 (T(0)+T(1)+T(2)+?T(n-2)+T(n-1))-2(T(0)+T(1)+T(2)+…T(n-2))
n T(n)- (n-1) T(n-1)= n[n+1-n+1]+2T(n-1)
n T(n)=[2+(n-1)]T(n-1)+2n
n T(n)= n+1 T(n-1)+2n

DAA Quick sort

Put n=n-1 in eq 3

DAA Quick sort

Put 4 eq in 3 eq

DAA Quick sort

Put n=n-2 in eq 3

DAA Quick sort

Put 6 eq in 5 eq

DAA Quick sort

Put n=n-3 in eq 3

DAA Quick sort

Put 8 eq in 7 eq

DAA Quick sort
DAA Quick sort

From 3eq, 5eq, 7eq, 9 eq we get

DAA Quick sort
DAA Quick sort

From 10 eq

DAA Quick sort

Multiply and divide the last term by 2

DAA Quick sort

Is the average case complexity of quick sort for sorting n elements.

3. Quick Sort [Best Case]: In any sorting, best case is the only case in which we don’t make any comparison between elements that is only done when we have only one element to sort.

DAA Quick sort


Next TopicStable Sorting

You may also like