19
06/14/22 03:44 PM AIIS 1

11 - Comparision of merge and quick sort

  • Upload
    fayazwe

  • View
    50

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 11 - Comparision of merge and quick sort

04/11/23 05:35 AM AIIS 1

Page 2: 11 - Comparision of merge and quick sort

OUTLINE

IntroductionAlgorithm for merge sortExampleQuick SortAlgorithm For Quick SortExample

04/11/23 05:35 AM 2AIIS

Page 3: 11 - Comparision of merge and quick sort

What Is Meant By Merge Sort ? Merge sort is northing but the given

array is divided into two parts. i) LEFT Part ii) RIGHT Part Both the left part and the right part

should be in sorted recursively. Latter the sorted left part and the

sorted right part are finally merged in to a single sorted array.

04/11/23 05:35 AM 3AIIS

Page 4: 11 - Comparision of merge and quick sort

The process of merging of two sorted vectors into a single sorted vector is called as “simple merging”.

The only condition in merge sort is that the both vectors should be sorted either in Ascending order (or) Descending order.

04/11/23 05:35 AM 4AIIS

Page 5: 11 - Comparision of merge and quick sort

Algorithm For MERGE Sort Merge sort (a, low, high){ if (low < high) { mid = (low+high)/2; merge sort(a,low,mid); merge sort(a,mid+1,high); simple

merge(a,low,mid,high); }}

04/11/23 05:35 AM 5AIIS

Page 6: 11 - Comparision of merge and quick sort

simple merge(a,low,mid,high){ while(i<=mid && j<=high) { if(a[i] < a[j]) { c[k] = a[i]; i = i+1; k = k+1; } else { c[k] = a[j]; j = j+1; k = k+1;

04/11/23 05:35 AM 6AIIS

Page 7: 11 - Comparision of merge and quick sort

} }while (i <= mid) c[k++] = a[i++]; while (j <= high) c[k++] = a[j++]; for (i= low;i<= high; i++) a[i] = c[i];

04/11/23 05:35 AM 7AIIS

Page 8: 11 - Comparision of merge and quick sort

Example for Merge sort

7,5,9,2,3,1,4,6,10,8

7,5,9,2,3 1,4,6,10,8

7,5,9 1,4,6 10,82,3

7,5

7

9 2 3 1,4 6 10 8

5 1 4

04/11/23 05:35 AM 8AIIS

Page 9: 11 - Comparision of merge and quick sort

5,7 9 2 3 1,4 6 10 8

5,7,9 1,4,6 10,82,3

1,2,3,4,5,6,7,8,9,10

2,3,7,5,9 1,4,6,10,8

04/11/23 05:35 AM 9AIIS

Page 10: 11 - Comparision of merge and quick sort

What is Quick sort ?- Quick sort is a very popular sorting method .Quack

sort is also known as partition exchange sorting. - The name comes from the fact that quick sort

can sort a list of data elements significantly faster than any of the common sorting algorithms.

- This algorithm is based on the fact that it is faster and easier to sort two small arrays in to one large one.

- The basic strategy of quick sort is to divide and

conquer.

04/11/23 05:35 AM 10AIIS

Page 11: 11 - Comparision of merge and quick sort

Algorithm for Quick sort

Quick-sort (a ,low ,high) { if (low<high) { mid = partition (a ,low ,high); quicksort (a ,low ,mid-1); quicksort ( a, mid+1 ,high); } } partition (a ,low, high) { while (I <= j) { do i++; while (key >= a[i] );

04/11/23 05:35 AM 11AIIS

Page 12: 11 - Comparision of merge and quick sort

do j--; while (key <= a[j]); if (I < j) temp = a[i]; a[i] = a[j]; a[j] = temp; } temp = a[low]; a[low] = a[j]; a[j] = temp; return j; }

04/11/23 05:35 AM 12AIIS

Page 13: 11 - Comparision of merge and quick sort

Example for Quick sort:

33 77 88 55 22 11 99 55 44

The box element is the pivot element, Blue elements are less or equal to pivot & brown elements are larger than pivot.

04/11/23 05:35 AM 13AIIS

Page 14: 11 - Comparision of merge and quick sort

33 77 88 55 22 11 99 55 44

33 77 88 44 22 11 99 55 55

The elements 4<5 therefore it is interchanged (swapped).

04/11/23 05:35 AM 14AIIS

Page 15: 11 - Comparision of merge and quick sort

33 77 88 44 22 11 99 55 55

The elements 4<7 & 2<8 therefore it is interchanged (swapped).

After swapping the array elements are as follows:

33 44 22 77 88 11 99 55 55

04/11/23 05:35 AM 15AIIS

Page 16: 11 - Comparision of merge and quick sort

33 44 22 77 88 11 99 55 55

The elements 1<7 & 5<8 therefore it is interchanged (swapped). After swapping the array elements are as follows:

33 44 22 11 55 77 99 88 55

04/11/23 05:35 AM 16AIIS

Page 17: 11 - Comparision of merge and quick sort

33 44 22 11 55 77 99 88 55

The elements pivot 5<7 therefore it is interchanged (swapped). After swapping the array elements are as follows:

33 44 22 11 55 55 99 88 77

04/11/23 05:35 AM 17AIIS

Page 18: 11 - Comparision of merge and quick sort

The pivot has taken its place & the greater elements are on the right side & lesser elements are on the left side. The elements are shown below,

33 44 22 11 55 55 99 88 77

Repeat the steps until all the elements get sorted. The elements after sorting will be:

11 22 33 44 55 55 77 88 99

04/11/23 05:35 AM 18AIIS

Page 19: 11 - Comparision of merge and quick sort

04/11/23 05:35 AM 19AIIS