Mergesort - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Mergesort

Description:

Mergesort: Illustration. 24 85. 45. 63. 17 31 96 50. Mergesort: Illustration. 24 85. 45 63. 17 31 ... 0(k) time for 2 lists each k/2 elements long (merged into one list k ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 78
Provided by: Pau1377
Category:

less

Transcript and Presenter's Notes

Title: Mergesort


1
Mergesort
2
Mergesort Algorithm
  • void mergesort( Elem a, Elem temp, int Ieft,
    int right )
  • int I, j, k, mid (leftright)/2
  • if( left right ) return
  • mergesort( a, temp, left, mid)
  • mergesort( a, temp, mid1, right)
  • // do the merge operation
  • for( i left i lt mid i )
    tempi ai
  • for( j 1 j lt right-mid j ) temp
    right-j1 a jmid
  • // merge sublists back to array
  • for( ileft, jright, kleft kltright k
    )
  • if( temp i lt temp j ) ak
    temp i
  • else aktemp j--

3
Mergesort Illustration
85 24 63 45 17 31 96 50
4
Mergesort Illustration

85 24 63 45
17 31 96 50
5
Mergesort Illustration


17 31 96 50
85 24
63 45
6
Mergesort Illustration


17 31 96 50

63 45
85
24
7
Mergesort Illustration


17 31 96 50

63 45
85
24
8
Mergesort Illustration


17 31 96 50
24
63 45
85
9
Mergesort Illustration


17 31 96 50
24
63 45
85
10
Mergesort Illustration


17 31 96 50
24 85
63 45
11
Mergesort Illustration


17 31 96 50
24 85
63 45
12
Mergesort Illustration


17 31 96 50
24 85
63 45
13
Mergesort Illustration


17 31 96 50
24 85

63
45
14
Mergesort Illustration


17 31 96 50
24 85

63
45
15
Mergesort Illustration


17 31 96 50
24 85
45
63
16
Mergesort Illustration


17 31 96 50
24 85
45
63
17
Mergesort Illustration


17 31 96 50
24 85
45 63
18
Mergesort Illustration


17 31 96 50
24 85
45 63
19
Mergesort Illustration


17 31 96 50
24 85
45 63
20
Mergesort Illustration


17 31 96 50
24 85
45 63
21
Mergesort Illustration

24
17 31 96 50
85
45 63
22
Mergesort Illustration

24
17 31 96 50
85
45 63
23
Mergesort Illustration

24 45
17 31 96 50
85
63
24
Mergesort Illustration

24 45
17 31 96 50
85
63
25
Mergesort Illustration

24 45 63
17 31 96 50
85

26
Mergesort Illustration

24 45 63
17 31 96 50
85

27
Mergesort Illustration

24 45 63 85
17 31 96 50


28
Mergesort Illustration

24 45 63 85
17 31 96 50


29
Mergesort Illustration

24 45 63 85
17 31 96 50
30
Mergesort Illustration

24 45 63 85

17 31
96 50
31
Mergesort Illustration

24 45 63 85


96 50
17
31
32
Mergesort Illustration

24 45 63 85


96 50
17
31
33
Mergesort Illustration

24 45 63 85

17
96 50
31
34
Mergesort Illustration

24 45 63 85

17
96 50
31
35
Mergesort Illustration

24 45 63 85

17 31
96 50
36
Mergesort Illustration

24 45 63 85

17 31
96 50
37
Mergesort Illustration

24 45 63 85

17 31
96 50
38
Mergesort Illustration

24 45 63 85

17 31

96
50
39
Mergesort Illustration

24 45 63 85

17 31

96
50
40
Mergesort Illustration

24 45 63 85

17 31
50
96
41
Mergesort Illustration

24 45 63 85

17 31
50
96
42
Mergesort Illustration

24 45 63 85

17 31
50 96
43
Mergesort Illustration

24 45 63 85

17 31
50 96
44
Mergesort Illustration

24 45 63 85

17 31
50 96
45
Mergesort Illustration

24 45 63 85

17 31
50 96
46
Mergesort Illustration

24 45 63 85
17
31
50 96
47
Mergesort Illustration

24 45 63 85
17
31
50 96
48
Mergesort Illustration

24 45 63 85
17 31

50 96
49
Mergesort Illustration

24 45 63 85
17 31

50 96
50
Mergesort Illustration

24 45 63 85
17 31 50 96

51
Mergesort Illustration

24 45 63 85
17 31 50 96

52
Mergesort Illustration

24 45 63 85
17 31 50 96
53
Mergesort Illustration

24 45 63 85
17 31 50 96
54
Mergesort Illustration
17
24 45 63 85
31 50 96
55
Mergesort Illustration
17
24 45 63 85
31 50 96
56
Mergesort Illustration
17 24
45 63 85
31 50 96
57
Mergesort Illustration
17 24
45 63 85
31 50 96
58
Mergesort Illustration
17 24 31
45 63 85
50 96
59
Mergesort Illustration
17 24 31
45 63 85
50 96
60
Mergesort Illustration
17 24 31 45
63 85
50 96
61
Mergesort Illustration
17 24 31 45
63 85
50 96
62
Mergesort Illustration
17 24 31 45 50
63 85
96
63
Mergesort Illustration
17 24 31 45 50
63 85
96
64
Mergesort Illustration
17 24 31 45 50 63
85
96
65
Mergesort Illustration
17 24 31 45 50 63
85
96
66
Mergesort Illustration
17 24 31 45 50 63 85

96
67
Mergesort Illustration
17 24 31 45 50 63 85

96
68
Mergesort Illustration
17 24 31 45 50 63 85 96


69
Mergesort Illustration
17 24 31 45 50 63 85 96


70
Mergesort Illustration
17 24 31 45 50 63 85 96
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 gt 0 i--)
  • buildheap(a, i, n )
  • void buildheap(Elem a, i, n )
  • int j 2i
  • int b ai
  • while( j lt n)
  • if( j1 ltn a j lt a j1 ) j
    j1
  • if( b lt a j ) ai a j
  • i j
  • j 2i
  • a j/2 b
  • void Heapsort( Elem a, 1, n)
  • heapify( a, 1, n )

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( i0 Iltn I )
  • B ai.key .append( ai )
  • int index 0
  • for( i0 ilt MAX_KEY_VALUE i)
  • for( Bi.first() Bi.isInList()
    Bi.next() )
  • ai Bi.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)
  • // counti stores the number of records in
    bini
  • for( int i0, rtok1 iltk i, rtokr) //for k
    digits
  • for( int j0 jltr j) countj0
    //initialize count
  • //Count the no. of records for each bin on this
    pass
  • for( j0 jltn j ) count (aj.key /
    rtok) r
  • // Index B count j will be index for last
    slot of bin j
  • for( j1 jltr j ) countj countj-1
    countj
  • // put records into bins working from bottom of
    each bin
  • // since bins fill from the bottom, j counts
    downwards
  • for( jn-1 jgt0 j--) B--count (aj.key /
    rtok) r aj
  • for( j0 jltn j ) aj Bj //copy B
    back into a
Write a Comment
User Comments (0)
About PowerShow.com