Merge Sort 2-Computer Programming For Aeronautical Engineering And Sciences-Lecture Slides, Slides of Aeronautical Engineering

Prof. Balamohan Pawar delivered this lecture at Allahabad University for Aeronautical Engineering and Computer Programming course. Its main points are: Sort, Merge, Analysis, Heaps, Array, Binary, Tree, Location, Build, Recursion

Typology: Slides

2011/2012

Uploaded on 07/20/2012

savitha_48
savitha_48 🇮🇳

3

(1)

109 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
5
Merge Sort
5
96
67
12
17
42
11
47
5
96
67
12
17
42
11
47
5
96
67
12
17
42
11
47
5
96
67
12
17
42
11
47
47
11
17
42 47
17
42
11
67
12
5
96
67
12
5
96
5
11
12
47
17
42
67
96
6
Merge Sort Analysis Analysis
MergeSort(A, left, right) T(n)
if (left < right) O(1)
mid := (left + right) / 2; O(1)
MergeSort(A, left, mid); T(n/2)
MergeSort(A, mid+1, right); T(n/2)
Merge(A, left, mid, right); O(n)
Statement Work
T(n) = O(1) when n = 1,
2T(n/2) + O(n) when n > 1
T(n) = O(1) when n = 1,
2T(n/2) + O(n) when n > 1
Recurrence Equation
Recurrence Equation
7
Solving Recurrences:
Iteration
>+
=
=1
1
)( ncn
b
n
aT
nc
nT
8
T(n) =
aT(n/b) + cn
a(aT(n/b/b) + cn/b) + cn
a2T(n/b2) + cna/b + cn
a2T(n/b2)+ cn(a/b + 1)
a2(aT(n/b2/b) + cn/b2) + cn(a/b + 1)
a3T(n/b3) + cn(a2/b2) + cn(a/b + 1)
a3T(n/b3) + cn(a2/b2+ a/b + 1)
akT(n/bk) + cn(ak-1/bk-1 + ak-2/bk-2 + … + a2/b2+ a/b + 1)
>+
=
=1
1
)( ncn
b
n
aT
nc
nT
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Merge Sort 2-Computer Programming For Aeronautical Engineering And Sciences-Lecture Slides and more Slides Aeronautical Engineering in PDF only on Docsity!

5

Merge Sort^4747471147111111471142421742421742

Merge Sort Analysis Analysis MergeSort (A, left, right)

T(n)

if^ (left < right)

O(1)mid := (left + right) / 2;^ O(1)MergeSort(A, left, mid);^ T(n/2)MergeSort(A, mid+1, right);^ T(n/2)Merge(A, left, mid, right);^ O(n)

Statement^

Work

T(n) = O(1)^

when n = 1,

2T(n/2) + O(n)^

when n > 1

T(n) = O(1)^

when n = 1,

2T(n/2) + O(n)^

when n > 1Recurrence EquationRecurrence Equation

7

Solving Recurrences:Iteration⎧⎪ ⎨^ ⎪⎩

=^

nncnb

aT

nc

nT

-^ T(n) =aT(n/b) + cna(aT(n/b/b) + cn/b) + cn^22 aT(n/b) + cna/b + cn^22 aT(n/b) + cn(a/b + 1)^22 a(aT(n/b/b) + cn/b 2 ) + cn(a/b + 1) (^33 22) aT(n/b) + cn(a/b) + cn(a/b + 1) (^33 22) aT(n/b) + cn(a/b+ a/b + 1)…k k k-1k-1^ k-2k-2^2 aT(n/b ) + cn(a/b + a/b^ + … + a/b =⎧⎪⎞⎛⎨>+⎟⎜ ⎪⎩⎠⎝^2 + a/b + 1) (^1) nc n = (^) )( nT (^1) ncnaTb docsity.com

9

  • So we havek^ k^ – T(n) = a^ T(n/b^ ) + cn(a

k-1k-1^22 /b^ + ... + a/b+ a/b + 1)

k• For n = bk^ – T(n) = aT(1) + cn(a

=^1 nc ⎧⎪ n ⎞⎛= (^) )( nT ⎨>+^1 ncnaT ⎟⎜ ⎪ b ⎩⎠⎝ k-1k-1 (^22) /b + ... + a/b+ a/b + 1)kk-1k-1 (^22) = ac + cn(a /b + ... + a/b+ a/b + 1)k k-1k-1 (^22) = ca+ cn(a /b + ... + a/b+ a/b + 1)k k k-1k-1 (^22) = cna/b + cn(a/b + ... + a/b+ a/b + 1)k k (^22) = cn (a /b + ... + a/b+ a/b + 1)

  • So with k = log

nb^ kk (^22) – T(n) = cn(a/b+ ... + a^ /b+ a/b + 1)

  • What if a = b?– T(n) = cn(k + 1)= cn(log^ b^

=^1 nc ⎧⎪ n ⎞⎛= (^) )( nT ⎨>+^1 ncnaT ⎟⎜ ⎪ b ⎩⎠⎝ n + 1)= O(n log n)

11 Heapsort: Best of two worlds• Merge sort– Advantage: O(n log n)• Insertion sort– Advantage: Can sort in place, and efficientfor nearly sorted arrays Heapsort^ Combines the advantage of Merge andInsertion sort^

Heaps

  • Is the tree below complete?

16 14 10 8 7

9 3

A^ complete binary tree^2 4

is a full binary tree that

has all leaves at the same depthHeaps are^ nearly

complete binary trees

docsity.com

17 Is the tree a Heap?^1614

Heap_Array =^

Now it is a heap^1614

Heap_Array =^14

19 Heapify Heapify^ (A,^ i)lchild^ :=^ Left(i)rchild^ :=^ Right(i)^ if^ (lchild^ <=^ heap_size

and^ A[lchild]^ >^ A[i])

then largest^ :=^ lchild else largest^ :=^ i if (rchild^ <=^ heap_size^

and^ A[rchild]^ >^ A[largest])

then

Move an element in location^ largest^ :=^ rchild^ if^ (largest^ /=^ i)^ thenSwap (A,^ i,^ largest)^ Heapify (A,^ largest)

i^ to satisfy heap property

Creating a heap• Given an unsorted array build_heap (A, Size)heap_size(A) := Size; for i in^ ⎣Size/2⎦^ downto

^1 Heapify(A, i);

docsity.com

21 Build_Heap 1 24 3 16 9 10 14

Heap_Array =

Heapify(Heap_Array, 5) – Does nothing^2 41 3 14 8

Build_Heap 1 24 3 16 9 10 14

Heap_Array =

Heapify(Heap_Array,4)^2 1

4 6 8 105 7 9 23 Build_Heap 1 144 3 16 9 10 2

Heap_Array =

Heapify(Heap_Array,3)^1 2

4 5 6 7 8 9 10

Build_Heap 1 144 10 16 9 3 2

Heap_Array =

Heapify(Heap_Array,2)^1 2

4 5 6 7 8 9 10

docsity.com

29 Heap 14 816 10 7 9 3 2 4 1 Heap_Array =

6 8 105 7 9

Heap Sort BuildHeap(A); for i in size^ downto^^2 Swap(A[1], A[i])heap_size:= heap_size -1;Heapify(A, 1);

docsity.com