Download pdf - Data Structures : Sorting

Transcript
Page 1: Data Structures : Sorting

0 1 2 3 4 5

23 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 56

i=1

8 23 45 78 32 56

Page 2: Data Structures : Sorting

Loop 1 Loop 2

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

14 23 78 45 8 32 56 j=2 no swap

14 23 78 45 8 32 56 j=3 swap 78 45

14 23 45 78 8 32 56 j=4 swap 78 8

14 23 45 8 78 32 56 j=5 swap 78 32

14 23 45 8 32 78 56 j=6 swap 78 56

14 23 45 8 32 56 78

SORTED

Page 3: Data Structures : Sorting

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 : Sorting

Wall

1 j k n

place k element in appropriate place

by walking back from j

Page 5: Data Structures : Sorting

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 9

1st Incre k=5 Walker Current

72 62 14 9 30 21 80 25 70 55 SWAP

21 62 14 9 30 77 80 25 70 55

21 62 14 9 30 77 80 25 70 55

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

21 62 14 9 30 77 80 25 70 55

9

Arr 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 SWAP

9 62 14 21 30 77 80 25 70 55 SWAP

9 30 14 21 62 77 80 25 70 55 9

9 30 14 21 62 77 80 25 70 55 arr[1]

9 30 14 21 62 77 80 25 70 55 SWAP FALLBACK

9 25 14 21 30 77 80 62 70 55 SWAP

9 25 14 21 30 70 80 62 77 55 SWAP

9 25 14 21 30 70 55 62 77 80

Arr Index 0 1 2 3 4 5 6 7 8 9

1st Incre k=1 WalkerCurrent

9 25 14 21 30 70 55 62 77 80

9 25 14 21 30 70 55 62 77 80 SWAP

9 14 25 21 30 70 55 62 77 80 SWAP

9 14 21 25 30 70 55 62 77 80

9 14 21 25 30 70 55 62 77 80

9 14 21 25 30 70 55 62 77 80 SWAP

Page 6: Data Structures : Sorting

9 14 21 25 30 55 70 62 77 80 SWAP

9 14 21 25 30 55 62 70 77 80

9 14 21 25 30 55 62 70 77 80 SORTED

Page 7: Data Structures : Sorting

Why Insertion Sort ??

Wall

15 22 36 25 6

arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k]

Wall

15 22 25 36 6

arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k]

consider only our segment

25 is placed in the inserted when we

Page 8: Data Structures : Sorting

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

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

22 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 11

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

Sort Left:1 Looking for number > than 62 (pivot)

Sort Left:2

Sort Left:3

Sort Right :11 Looking for number < than 62 (pivot)

Sort Right :10

Sort Right :9

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

Sort Left :4

Sort Right moves to 5

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

0,11

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

< 62 >=62

0,3

22

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

87

0,1

14

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

Page 9: Data Structures : Sorting

45

1,1

21

5,9

84

5,7

76 14 21 22 45 62 74 76 78 84 85

5,5

74

7,7

78

11,11

85

Page 10: Data Structures : Sorting

Looking for number > than 62 (pivot)

Looking for number < than 62 (pivot)

Page 11: Data Structures : Sorting

Merge i j i

14 20 36 10 12 30 40 44 14 20

aux 10 12 14

Merge Sort

12 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 7

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

Break 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

Merge Again 8 12 21 30 1 6 7 9

Page 12: Data Structures : Sorting

Merge Again 1 6 7 8 9 12 21 30

Page 13: Data Structures : Sorting

m m+1 j

36 10 12 30 40 44

6 9 1 7

6 9 1 7

MergeSort(arr,start,end)

//if only 1 elemnet

if (start==end) return;

//divide into two equal parts

m=(i+j)/2

mergeSort(arr,start,middle)

mergeSort(arr,middle+1,end)

merge(arr,start,middle,end)

Page 14: Data Structures : Sorting

MergeSort(arr,start,end)

//if only 1 element

if (start==end) return;

//divide into two equal parts

m=(i+j)/2

mergeSort(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 called

Start End

0 7

0 3

0 1

0 0

1 1

2 3

2 2

3 3

4 7

4 5

4 4

5 5

6 7

6 6

7 7

If 100 the call would be ? 199

Page 15: Data Structures : Sorting

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 : Sorting

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

Unit Digit

0

1 111 211 311 411 511 611 121 131 141 151 161

2 112

3 113

4 114

5 115

6

….

Ten Digit Ten Digit

0 0

1 111 211 311 411 511 611 112 113 114 115 1 111 112 113

2 121 2 211

3 131 3 311

4 141 4 411

5 151 5 511

6 161 6 611

…. ….

SORTED 111 112 113

Page 17: Data Structures : Sorting

114 115 121 131 141 151 161

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

Page 18: Data Structures : Sorting

Created by Jibrael Jos to explain Sort to M.Sc/MCA Students

Jul-09

Christ College


Recommended