0% found this document useful (0 votes)
80 views75 pages

Lecture 4 5 6 Sort - Additional Resources

The document discusses the merge sort algorithm. Merge sort works by recursively dividing an array into single elements and then merging the divided arrays back together in a sorted order. It first divides the array in half, sorts each half via recursive calls, and then uses a merge process to combine the two sorted halves into a single sorted array. This divide and conquer approach results in a runtime of O(n log n) for merge sort.

Uploaded by

Rula Shakrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views75 pages

Lecture 4 5 6 Sort - Additional Resources

The document discusses the merge sort algorithm. Merge sort works by recursively dividing an array into single elements and then merging the divided arrays back together in a sorted order. It first divides the array in half, sorts each half via recursive calls, and then uses a merge process to combine the two sorted halves into a single sorted array. This divide and conquer approach results in a runtime of O(n log n) for merge sort.

Uploaded by

Rula Shakrah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

Data Structure & Algorithm

Additional resources for Lecture 4,5,6 : Sort Algorithms -Part 2

Dr. Mohamed Ezz


Selection Sort
Selection sort example

3
Selection sort example 2
Index
0 1 2 3 4 5 6 7

Value
27 63 1 72 64 58 14 9

1st pass
1 63 27 72 64 58 14 9

2nd pass
1 9 27 72 64 58 14 63

3rd pass
1 9 14 72 64 58 27 63

4
Algorithm
 1. Do steps 2–4 for i = 0 up to n –1 .
 2. Do step 3 for j = i+1 up to n –1.
 3. Locate the index m of the smallest element among {si . .
sn} .
 4. Swap si and sm.

5
Sorting Program
public class SortArray { public static void swap(int[]
a, int x,int y) {
public static void main(String[] args) { int temp= a[x];
int[] a = {42,77,35,12,101,5}; a[x]=a[y];
print(a); a[y]=temp
selectionSort(a); }
print(a);
} ///end main
public static void print(int[] a) {
System.out.printf("{%d", a[0]);
for (int i = 1; i < a.length; i++) {
System.out.printf(", %d", a[i]);
}
System.out.println("}");
}

6
Selection sort code
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
// find index of smallest element
int smallest_element_index = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[smallest_element_index ]) {
smallest_element_index = j;
}
}

// swap smallest element with a[i]


swap(a, i, smallest_element_index );
}
}//end class

7
Selection sort runtime
 Running time for input size n:
 In practice, a bit faster than bubble sort. Why?

n −1 n −1 n −1

 1 =  (n − 1 − (i + 1) + 1)
i = 0 j =i +1 i =0
n −1
=  (n − i − 1)
i =0
n −1 n −1 n −1
= n1 −  i − 1
i =0 i =0 i =0

T(n) = n 2 −
(n − 1)n
− n = 0.5n 2 − 0.5n
2
= ( n 2 )
8
Insertion Sort
Insertion sort
 Simple sorting algorithm.
 n-1 passes over the array
 At the end of pass i, the elements that occupied A[0]…A[i]
originally are still in those spots and in sorted order.

2 15 8 1 17 10 12 5

0 1 2 3 4 5 6 7
after
2 8 15 1 17 10 12 5
pass 2
0 1 2 3 4 5 6 7
after
1 2 8 15 17 10 12 5
pass 3
0 1 2 3 4 5 6 7
10
Insertion sort example

11
Algorithm
 1. Do steps 2–5 for i = 1 up to n –1.
 2. Hold the element si in a temporary space.
 3. Do step 4 for j = i down to 0 as long as sj-1>
temporary space.
 4. Shift the subsequence {sj . . . si–1} up one position to
{sj+1 . . . si}.
 5. Copy the held value of si (tempary) into sj.

12
Sorting Program
public class SortArray { public static void swap(int[]
a, int x,int y) {
public static void main(String[] args) { int temp= a[x];
int[] a = {42,77,35,12,101,5}; a[x]=a[y];
print(a); a[y]=temp
insertionSort(a); }
print(a);
} ///end main
public static void print(int[] a) {
System.out.printf("{%d", a[0]);
for (int i = 1; i < a.length; i++) {
System.out.printf(", %d", a[i]);
}
System.out.println("}");
}

13
Insertion sort code
public static void insertionSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];

// slide elements down to make room for a[i]


int j = i;
while (j > 0 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}

a[j] = temp;
}
}

14
Insertion sort runtime
 worst case: reverse-ordered elements in array.
n −1
(n − 1)n
T(n)=  i = 1 + 2 + 3 + ... + ( n − 1) = = 0.5n 2 − 0.5n
i =1 2
= ( n 2 )
 best case: array is in sorted ascending order.
n −1
T(n)= 1 = n −1 = (n)
i=1
 average case: each element is about halfway in order.
n −1
i 1 (n −1)n
T(n)=  = (1+ 2 + 3...+ (n −1)) =
 i=1 2 2 4
= (n 2 )
15
Comparing sorts
Bubble Selection Insertion

Comparison Swap Comparison Swap Comparison Swap

Worst O(n2) O(n2) O(n2) O(n) O(n2) O(n2)

Average O(n2) O(n2) O(n2) O(n) O(n2) O(n2)

Best O(n2) 0 O(n) O(n) O(n) n copy

16
Merge Sort
Merge sort
 merge sort: orders a list of values by recursively
dividing the list in half until each sub-list has one
element, then recombining
 Invented by John von Neumann in 1945

 Another "divide and conquer" algorithm


 divide the list into two roughly equal parts
 conquer by sorting the two parts
 recursively divide each part in half, continuing until a part
contains only one element (one element is sorted)
 combine the two parts into one sorted list

18
Merge sort idea
 Divide the array into two halves.
 Recursively sort the two halves (using merge sort).
 Use merge to combine the two arrays.

mergeSort(0, n/2-1) mergeSort(n/2, n-1)

sort sort
merge(0, n/2, n-1)

19
98 23 45 14 6 67 33 42

20
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

21
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

22
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge

24
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23

Merge

25
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98

Merge

26
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

27
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge

28
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge

29
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

30
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge

31
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge

32
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge

33
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge

34
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge

35
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98

36
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98

37
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge

38
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge

39
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge

40
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98

41
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge

42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge

43
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge

44
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge

45
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge

46
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge

47
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge

48
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge

49
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
50
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
51
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
52
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
53
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
54
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
55
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
56
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
57
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
58
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

59
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98

60
Merging two sorted arrays
 merge operation:
 Given two sorted arrays, merge operation produces a sorted array
with all the elements of the two arrays

A 6 13 18 21 B 4 8 9 20

C 4 6 8 9 13 18 20 21

Running time of merge: O(n), where n is the number of


elements in the merged array.
when merging two sorted parts of the same array, we'll need a temporary
array to store the merged whole

61
Merge sort code
public static void sort(int[] a) {
int[] temp = new int[a.length];
mergeSort(a, temp, 0, a.length - 1);
}

private static void mergeSort(int[] a, int[] temp,


int left, int right) {
if (left >= right) { // base case
return;
}

// sort the two halves


int mid = (left + right) / 2;
mergeSort(a, temp, left, mid);//sort left half
mergeSort(a, temp, mid+1, right); //sort right half

// merge the sorted halves into a sorted whole


merge(a, temp, left, right);
}

62
Merge code
private static void merge(int[] a, int[] temp,
int left, int right) {
int mid = (left + right) / 2;
int count = right - left + 1;
int l = left; // counter indexes for L, R
int r = mid + 1;
// main loop to copy the halves into the temp array
for (int i = 0; i < count; i++)
if (r > right) { // finished right; use left
temp[i] = a[l++];
} else if (l > mid) { // finished left; use right
temp[i] = a[r++];
} else if (a[l] < a[r]) { // left is smaller (better)
temp[i] = a[l++];
} else { // right is smaller (better)
temp[i] = a[r++];
}
// copy sorted temp array back into main array
for (int i = 0; i < count; i++) {
a[left + i] = temp[i];
}
}

63
mergeSort(0,0)
mergeSort(0,3) mergeSort(0,1)

mergeSort(1,1)

merge(0,1)

mergeSort(0,7)
mergeSort(0,3) mergeSort(0,1)

mergeSort(2,3) mergeSort(2,2)

mergeSort(3,3)

mergeSort(0,7) merge(2,3)
mergeSort(0,3) mergeSort(0,1)

mergeSort(2,3)

mergeSort(0,7)

merge(0,3)
mergeSort(0,3)

mergeSort(4,4)
mergeSort(4,5)

mergeSort(5,5)

mergeSort(4,7) merge(4,5)

mergeSort(0,7)
mergeSort(0,3)

mergeSort(4,5)

mergeSort(4,7)

mergeSort(6,7) mergeSort(6,6)

mergeSort(0,7)
mergeSort(7,7)

merge(6,7)
mergeSort(0,3)

mergeSort(4,5)

mergeSort(4,7)

mergeSort(6,7)

mergeSort(0,7)

merge(4,7)

merge(0,7)
mergeSort(0,3)

mergeSort(4,7)

mergeSort(0,7)

merge(0,7)
mergeSort(0,7
)
Merge sort example 2
13 6 21 18 9 4 8 20
0 7

13 6 21 18 9 4 8 20
0 3 4 7

13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7

13 6 21 18 9 4 8 20
0 1 2 3 4 5 6 7

6 13 18 21 4 9 8 20
0 1 2 3 4 5 6 7

6 13 18 21 4 8 9 20
0 3 4 7

4 6 8 9 13 18 20 21
0 7
72
Merge sort runtime
 Let T(n) be runtime of merge sort on n items
 T(0) = 1
 T(1) = 2*T(0) + 1
 T(2) = 2*T(1) + 2
 T(4) = 2*T(2) + 4
 T(8) = 2*T(4) + 8
 ...
 T(n/2) = 2*T(n/4) + n/2
 T(n) = 2*T(n/2) + n

 Substitute to solve for T(n)

73
Repeated Substitution Method
T(n) = 2*T(n/2) + n
T(n/2) = 2*T(n/4) + n/2
T(n) = 2*(2*T(n/4) + n/2) + n
T(n) = 4*T(n/4) + 2n
T(n) = 8*T(n/8) + 3n → T(n) = 23 T(n/23) + 3n
...
T(n) = 2k T(n/2k) + kn
What is k? How many times can you cut n in half?
Setting k = log2 n.
T(n) = 2log n T(n/2log n) + (log n) n
T(n) = n * T(n/n) + n log n
T(n) = n * T(1) + n log n
T(n) = n * 1 + n log n
T(n) = n + n log n
T(n) = O(n log n)

74
Sorting Classification
External
In memory sorting
sorting
Specialized
Comparison sorting
Sorting
# of tape
O(N2) O(N log N) O(N)
accesses
1. Bubble Sort 1. Merge Sort 1. Bucket Sort 1. Simple External
2. Selection Sort 2. Quick Sort 2. Radix Sort 2. Merge Sort
3. Insertion Sort 3. Heap Sort 3. Variations
4. Shell Sort

75

You might also like