data structures : sort explained

22
0 1 2 3 4 5 23 78 45 8 32 56 i=0 min=23 j=1 min=23 j=2 min=23 j=3 min=8 j=4 min=8 j=5 min=8 8 78 45 23 32 56 i=1 8 23 45 78 32 56

Upload: jibrael-jos

Post on 22-May-2015

1.558 views

Category:

Technology


1 download

DESCRIPTION

Sorting Explained in class using MS Excel instead of Powerpoint or the Black Board Examples based on Foruzan and Gilberg books on Data Structures

TRANSCRIPT

Page 1: Data Structures : Sort Explained

0 1 2 3 4 523 78 45 8 32 56

i=0 min=23 for (i=0;i<n;i++)j=1 min=23 minVal=arr[i]j=2 min=23 for (j=i+1;j<n;j++)j=3 min=8 if minVal >arr[j]j=4 min=8 minVal=arr[j]j=5 min=8

8 78 45 23 32 56i=1

8 23 45 78 32 56

Page 2: Data Structures : Sort Explained

Loop 1 Loop 2pass 1 23 14 78 45 8 32 56 Original j=1 swap 23 14

14 23 78 45 8 32 56 j=2 no swap14 23 78 45 8 32 56 j=3 swap 78 4514 23 45 78 8 32 56 j=4 swap 78 814 23 45 8 78 32 56 j=5 swap 78 3214 23 45 8 32 78 56 j=6 swap 78 56

14 23 45 8 32 56 78

SORTED

Page 3: Data Structures : Sort Explained

23 78 45 8 32 56 Original

Insert 78 23 78 45 8 32 56 Pass 1

Insert 45 after 78 before 23 23 45 78 8 32 56 Pass 2

Insert 8 before 23 8 23 45 78 32 56 Pass 3

insert 32 after 45 before 23 8 23 32 45 78 56 Pass 4

Insert 56 after 78 8 23 32 45 56 78 Pass 5

SORTED

Page 4: Data Structures : Sort Explained

Wall

1 j k n

place k element in appropriate place by walking back from j

Page 5: Data Structures : Sort Explained

Array to be Sorted 72 62 14 9 30 21 80 25 70 55

Arr Index 0 1 2 3 4 5 6 7 8 91st Incre k=5 Walker Current

72 62 14 9 30 21 80 25 70 55 SWAP21 62 14 9 30 77 80 25 70 5521 62 14 9 30 77 80 25 70 5521 62 14 9 30 77 80 25 70 55 Why Insertion Sort ??21 62 14 9 30 77 80 25 70 55

9Arr Index 0 1 2 3 4 5 6 7 8 9 arr[1]1st Incre k=3 Walker Current

21 62 14 9 30 77 80 25 70 55 SWAP9 62 14 21 30 77 80 25 70 55 SWAP9 30 14 21 62 77 80 25 70 55 99 30 14 21 62 77 80 25 70 55 arr[1]9 30 14 21 62 77 80 25 70 55 SWAP FALLBACK 25 is placed in the inserted when we 9 25 14 21 30 77 80 62 70 55 SWAP consider only our segment9 25 14 21 30 70 80 62 77 55 SWAP9 25 14 21 30 70 55 62 77 80

Arr Index 0 1 2 3 4 5 6 7 8 91st Incre k=1 WalCurrent

9 25 14 21 30 70 55 62 77 809 25 14 21 30 70 55 62 77 80 SWAP9 14 25 21 30 70 55 62 77 80 SWAP9 14 21 25 30 70 55 62 77 80

Page 6: Data Structures : Sort Explained

9 14 21 25 30 70 55 62 77 809 14 21 25 30 70 55 62 77 80 SWAP9 14 21 25 30 55 70 62 77 80 SWAP9 14 21 25 30 55 62 70 77 809 14 21 25 30 55 62 70 77 80 SORTED

Page 7: Data Structures : Sort Explained

Why Insertion Sort ??Wall

15 22 36 25 6arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k]

Wall

15 22 25 36 6arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k]

25 is placed in the inserted when we consider only our segment

Page 8: Data Structures : Sort Explained

78 21 14 97 87 62 74 85 76 45 84 2262 21 14 97 87 78 74 85 76 45 84 2222 21 14 97 87 78 74 85 76 45 84 62

Determine Pivot 22 21 14 97 87 62 74 85 76 45 84 78

array Index 0 1 2 3 4 5 6 7 8 9 10 1162 21 14 97 87 22 74 85 76 45 84 78

Sort Left:1 Looking for number > than 62 (pivot)Sort Left:2Sort Left:3Sort Right :11 Looking for number < than 62 (pivot)Sort Right :10Sort Right :9

62 21 14 45 87 22 74 85 76 97 84 78 SWAP…

Sort Left :4Sort Right moves to 5

62 21 14 45 22 87 74 85 76 97 84 78 SWAP0,11

62 22 21 14 45 62 87 74 85 76 97 84 78 Move Pivot Back< 62 >=62

0,322

5,11 14 21 22 45 62 84 74 85 76 78 87 9787

0,114

3,3 14 21 22 45 62 76 74 78 84 8545

Page 9: Data Structures : Sort Explained

1,121

5,984

5,776 14 21 22 45 62 74 76 78 84 85

5,574

7,778

11,1185

Page 10: Data Structures : Sort Explained

Looking for number > than 62 (pivot)

Looking for number < than 62 (pivot)

Page 11: Data Structures : Sort Explained

Merge i j i14 20 36 10 12 30 40 44 14 20

aux 10 12 14

Merge Sort12 30 21 8 6 9 1 7 12 30 21 8

12 30 8 21 6 9 1 7 12 30 21 8

8 12 21 30 1 6 7 9

1 6 7 8 9 12 21 30

Recursive 12 30 21 8 6 9 1 7Break into 2 array 12 30 21 8 6 9 1 7Break into 4 arrays 12 30 21 8 6 9 1 7

Break into 8 arrays 12 30 21 8 6 9 1 7

Now each single elemnet array is sorted !!!SO Merge 12 30 8 21 6 9 1 7

Page 12: Data Structures : Sort Explained

Merge Again 8 12 21 30 1 6 7 9

Merge Again 1 6 7 8 9 12 21 30

Page 13: Data Structures : Sort Explained

m m+1 j36 10 12 30 40 44

6 9 1 7

6 9 1 7

MergeSort(arr,start,end)//if only 1 elemnetif (start==end) return;//divide into two equal partsm=(i+j)/2mergeSort(arr,start,middle)mergeSort(arr,middle+1,end)merge(arr,start,middle,end)

Page 14: Data Structures : Sort Explained

MergeSort(arr,start,end)//if only 1 elementif (start==end) return;//divide into two equal partsm=(i+j)/2mergeSort(arr,start,middle)mergeSort(arr,middle+1,end)merge(arr,start,middle,end)

Let us analyse Recursion in Merge Sort

Let us say an array has 8 elements, how many times will MergeSort get calledStart End

0 70 30 10 01 12 32 23 34 74 54 45 56 76 67 7

If 100 the call would be ? 199

Page 15: Data Structures : Sort Explained

Create Heap by using reheapUp Original

After Heap 78 32 56 8 23 45

reheapDown 56 32 45 8 23 78 Pass 1

45 32 23 8 56 78 Pass 2

32 8 23 45 56 78 Pass 3

23 8 32 45 56 78 Pass 4

8 23 32 45 56 78 Pass 5

Sorted 8 23 32 45 56 78 Pass 6

Page 16: Data Structures : Sort Explained

111 112 113 114 115 211 311 411 511 611 121 131 141 151 161

Unit Digit01 111 211 311 411 511 611 121 131 141 151 1612 1123 1134 1145 1156

….

Ten Digit Ten Digit0 01 111 211 311 411 511 611 112 113 114 115 1 111 112 1132 121 2 2113 131 3 3114 141 4 4115 151 5 5116 161 6 611

…. ….SORTED 111 112 113

Page 17: Data Structures : Sort Explained

114 115 121 131 141 151 161

114 115 121 131 141 151 161 211 311 411 511 611

Page 18: Data Structures : Sort Explained

Created by Jibrael Jos to explain Sort to M.Sc/MCA StudentsJul-09

Christ College