Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mergesort.

Similar presentations


Presentation on theme: "Mergesort."— Presentation transcript:

1 Mergesort

2 Mergesort: Algorithm void mergesort( Elem[] a, Elem[] temp, int Ieft, int right ) { int I, j, k, mid = (left+right)/2 if( left == right ) return mergesort( a, temp, left, mid); mergesort( a, temp, mid+1, right); // do the merge operation for( i = left; i <= mid; i++ ) temp[i] = a[i] for( j = 1; j <= right-mid; j++ ) temp[ right-j+1 ] = a[ j+mid ] // merge sublists back to array for( i=left, j=right, k=left; k<=right; k++ ) { if( temp[ i ] < temp[ j ] ) a[k] = temp[ i++ ] else a[k]=temp[ j-- ] }

3 Mergesort: Illustration

4 Mergesort: Illustration

5 Mergesort: Illustration
85 24 63 45

6 Mergesort: Illustration
63 45 85 24

7 Mergesort: Illustration
63 45 85 24

8 Mergesort: Illustration
24 63 45 85

9 Mergesort: Illustration
24 63 45 85

10 Mergesort: Illustration
24 85 63 45

11 Mergesort: Illustration
24 85 63 45

12 Mergesort: Illustration
24 85 63 45

13 Mergesort: Illustration
24 85 63 45

14 Mergesort: Illustration
24 85 63 45

15 Mergesort: Illustration
24 85 45 63

16 Mergesort: Illustration
24 85 45 63

17 Mergesort: Illustration
24 85 45 63

18 Mergesort: Illustration
24 85 45 63

19 Mergesort: Illustration
24 85 45 63

20 Mergesort: Illustration
24 85 45 63

21 Mergesort: Illustration
24 85 45 63

22 Mergesort: Illustration
24 85 45 63

23 Mergesort: Illustration
24 45 85 63

24 Mergesort: Illustration
24 45 85 63

25 Mergesort: Illustration
85

26 Mergesort: Illustration
85

27 Mergesort: Illustration

28 Mergesort: Illustration

29 Mergesort: Illustration

30 Mergesort: Illustration
17 31 96 50

31 Mergesort: Illustration
96 50 17 31

32 Mergesort: Illustration
96 50 17 31

33 Mergesort: Illustration
17 96 50 31

34 Mergesort: Illustration
17 96 50 31

35 Mergesort: Illustration
17 31 96 50

36 Mergesort: Illustration
17 31 96 50

37 Mergesort: Illustration
17 31 96 50

38 Mergesort: Illustration
17 31 96 50

39 Mergesort: Illustration
17 31 96 50

40 Mergesort: Illustration
17 31 50 96

41 Mergesort: Illustration
17 31 50 96

42 Mergesort: Illustration
17 31 50 96

43 Mergesort: Illustration
17 31 50 96

44 Mergesort: Illustration
17 31 50 96

45 Mergesort: Illustration
17 31 50 96

46 Mergesort: Illustration
17 31 50 96

47 Mergesort: Illustration
17 31 50 96

48 Mergesort: Illustration
17 31 50 96

49 Mergesort: Illustration
17 31 50 96

50 Mergesort: Illustration

51 Mergesort: Illustration

52 Mergesort: Illustration

53 Mergesort: Illustration

54 Mergesort: Illustration
17

55 Mergesort: Illustration
17

56 Mergesort: Illustration
17 24

57 Mergesort: Illustration
17 24

58 Mergesort: Illustration
50 96

59 Mergesort: Illustration
50 96

60 Mergesort: Illustration
63 85 50 96

61 Mergesort: Illustration
63 85 50 96

62 Mergesort: Illustration
63 85 96

63 Mergesort: Illustration
63 85 96

64 Mergesort: Illustration
85 96

65 Mergesort: Illustration
85 96

66 Mergesort: Illustration
96

67 Mergesort: Illustration
96

68 Mergesort: Illustration

69 Mergesort: Illustration

70 Mergesort: Illustration

71 Mergesort: Time complexity
Best, worst, average-case Each merge operation takes 0(k) time for 2 lists each k/2 elements long (merged into one list k elements long) There will be log2n levels 1st level: 2 n/2 long lists to be merged into 1 n long list 2nd level: 4 n/4 long lists to be merged into 2 n/2 long lists 3rd level: 8 n/8 long lists to be merged into 4 n/4 long lists Time Complexity: O(nlog2n)

72 Heapsort: Algorithm void heapify(Elem[] a, 1, n )
{ for( i = n/2; i > 0; i--) { buildheap(a, i, n ) } void buildheap(Elem[] a, i, n ) { int j = 2*i int b = a[i] while( j <= n) { if( j+1 <=n && a[ j] < a[ j+1] ) j = j+1; if( b < a[ j] ) a[i] = a[ j ] i = j j = 2*i a[ j/2 ] = b void Heapsort( Elem[] a, 1, n) { heapify( a, 1, n ) for( i = n; i>=2; i--) { swap( a[1], a[ i ] ) buildheap( a, 1, i-1)

73 Heapsort: Illustration

74 Heapsort: Time complexity
heapify() takes O(n) time n buildheap()s each take O( log2n ) time Best, worst, average-case O(nlog2n)

75 Binsort: Algorithm void binsort( Elem[] a, int n ) {
List[] B = new List[ MAX_KEY_VALUE ] for( i=0; I<n; I++ ) B[ a[i].key ].append( a[i] ) int index = 0 for( i=0; i< MAX_KEY_VALUE; i++) { for( B[i].first(); B[i].isInList(); B[i].next() ) { a[i] = B[i].currentValue() }

76 Bucketsort: Algorithm
Put a range of values in bins and then sort the contents of each bin and then output the values of each bin in order.

77 Radixsort: Algorithm void radixsort( Elem[] a, Elem[] B, int n, int k, int r, Elem[] count) { // count[i] stores the number of records in bin[i] for( int i=0, rtok=1; i<k; i++, rtok*=r) //for k digits { for( int j=0; j<r; j++) count[j]=0; //initialize count //Count the no. of records for each bin on this pass for( j=0; j<n; j++ ) count[ (a[j].key / rtok) % r ] ++ // Index B: count [j] will be index for last slot of bin j for( j=1; j<r; j++ ) count[j] = count[j-1] + count[j] // put records into bins working from bottom of each bin // since bins fill from the bottom, j counts downwards for( j=n-1; j>=0; j--) B[--count[ (a[j].key / rtok) % r ] ] = a[j] for( j=0; j<n; j++ ) a[j] = B[j] //copy B back into a }


Download ppt "Mergesort."

Similar presentations


Ads by Google