8
ata Structur Abhishek Pachisia B.Tech(IT)-II yr 09SETIT144

Quick sort

Embed Size (px)

DESCRIPTION

Subject: Data Structure in C.

Citation preview

Page 1: Quick sort

Data Structure

Abhishek Pachisia B.Tech(IT)-II yr09SETIT144

Page 2: Quick sort

Quick SortIt is one of the fastest sorting techniques available.Like binary search, this method uses a recursive, divide and conquer strategyThe basic idea is to separate a list of items into two parts, surrounding a distinguished item called the pivot. At the end of the process, one part will contain items smaller than the pivot and the other part will contain items larger than the pivot.The performance of the whole Quicksort algorithm depends on how well the pivot is taken.

Page 3: Quick sort

Let the unsorted list (let it be a)be…

14 23 11 5 8 0 2 9 4 20

1. Let us take the middle element as pivot.2. Then we arrange the array such that all elements greater then 8 is on right

side and others on left side.(This is the first subdivision producing two sub list).

3. Now, each sub list is subdivided in exactly the same manner. This process continues until all sub lists are in order. The list is then sorted. This is a recursive process.

Unsorted List

Procedure To Sort A List:

Sorted List

0 2 2 3 4 5 8 9 11 14 20

Page 4: Quick sort

SORTING THE GIVEN LISTWe choose the value as pivot.(Preferably middle element). As in binary search, the index of this value is found by (first+last)/2

where first and last are the indices of the initial and final items in the list.

We then identify a left_arrow and right_arrow on the far left and far right, respectively.(left_arrow and right_arrow initially represent the lowest and highest indices of the vector items).

It looks like:14 23 11 5 8 0 2 9 4 20

Pivot

left_arrow right_arrow

Page 5: Quick sort

Starting on the right, the right_arrow is moved left until a value less than or equal to the pivot is encountered. In a similar manner, left_arrow is moved right until a value greater than or equal to the pivot is encountered. This is the situation just encountered. Now the contents of the two vector items are swapped to produce

14 23 11 5 8 0 2 9 4 20

Pivot

left_arrow

right_arrow

We continue by moving right_arrow left and moving left_arrow right to produce. Pivot

left_arrow

4 3 2 2 5 0 8 11 9 14 20

right_arrow

Page 6: Quick sort

Since right_arrow < left_arrow is TRUE, the first subdivision is complete. At this stage, numbers smaller than pivot are on the left side and numbers larger than pivot are on the right side. This produces two sub lists ,they are: Pivot

Smaller Numbers Larger Numbers

Each sub list can now be sorted by the same function using recursive call to the sorting function.List obtained after sorting is :

84 3 2 2 5 0 11 9 14 20

Sorted List

0 2 2 3 4 5 8 9 11 14 20

Page 7: Quick sort

void QuickSort(apvector<int> &list, int left, int right)

{int pivot, leftArrow, rightArrow, n;

  leftArrow = left; rightArrow = right;

n=left - right + 1; pivot = list[(left + right) / 2]; do{

while (list[rightArrow] > pivot) --rightArrow;

while (list[leftArrow] < pivot) ++leftArrow;

if (leftArrow <= rightArrow) {

Swap_Data(list[leftArrow], list[rightArrow]); ++leftArrow; --rightArrow;level++;printf(“\nLevel %d : ”,level);efficiency(n);

} } while (rightArrow >= leftArrow);if (left < rightArrow)

QuickSort(list, left, rightArrow); if (leftArrow < right)

QuickSort(list, leftArrow, right); }

So

rt F

un

cti

on

Co

de

OUTPUT

Total efficiency

Page 8: Quick sort

Since each element ultimately ends up in the correct position, the algorithmcorrectly sorts. But how long does it take?.On this basis it is divided into following three cases.

1. Best Case2. Worst Case3. Average Case

Best Case for Quick SortThe best case for divide-and-conquer algorithms comes when we split the input as evenly as possible. Thus in the best case, each sub problem is of size n/2. The partition step on each sub problem is linear in its size. the total efficiency(time taken) in the best case is O(nlog2n).Worst Case for QuicksortSuppose instead our pivot element splits the array as unequally as possible. Thus instead of n/2 elements in the smaller half, we get zero, meaning that the pivot element is the biggest or smallest element in the array. The Average Case for QuicksortSuppose we pick the pivot element at random in an array of n keys Half the time, the pivot element will be from the centre half of the sorted array. Whenever the pivot element is from positions n/4 to 3n/4, the larger remaining sub-array contains at most 3n/4 elements.

Cases In Quick Sort

That’s All