Sorting and Searching
Sorting and Searching
Chapter 7 of textbook 1
General problem
Given a set of N orderable items, put them in order
• Insertion
• Exchanging
• Selection
• Merging
• Distribution
Run-time
The run time of the sorting algorithms we will look at fall into one of three
categories:
Q(n) Q(n ln(n)) O(n2)
P = 1;
Looking at first element only, and we do not change.
P = 2;
Temp = 8;
34 > Temp, so second element is set to 34.
We have reached the end of the list. We stop there. Thus,
first position is set equal to Temp;
After second pass;
8 34 64 51 32 21
Temp = 64, 34 < 64, so stop at 3rd position and set 3rd
position = 64
After third pass: 8 34 64 51 32 21
Now P = 6,
We have 8 21 32 34 51 64
Pseudo Code
Assume that the list is stored in an array, A (can do with a
linked list as well)
Insertion Sort(A[],int N)
{
for (P = 1; P < N; P++)
{
Temp = A[P];
for (j = P; j > 0 and A[j-1] > Temp; j--) A[j] = A[j-1];
A[j] = Temp;
} }
Quiz
Sort the sequence 3, 1, 4, 1, 5, 9, 2, 6, 5 using insertion sort
Inversions
Consider the following three lists:
1 16 12 26 25 35 33 58 45 42 56 67 83 75 74 86 81 88 99 95
1 17 21 42 24 27 32 35 45 47 57 23 66 69 70 76 87 85 95 99
22 20 81 38 95 84 99 12 79 44 26 87 96 10 48 80 1 31 16 92
Any Algorithm that sorts by exchanging adjacent elements has O(n2) on average
Number of Inversions
Let us consider the number of inversions in our first three lists:
1 16 12 26 25 35 33 58 45 42 56 67 83 75 74 86 81 88 99 95
1 17 21 42 24 27 32 35 45 47 57 23 66 69 70 76 87 85 95 99
22 20 81 38 95 84 99 12 79 44 26 87 96 10 48 80 1 31 16 92
has 13 inversions:
(16, 12) (26, 25) (35, 33) (58, 45) (58, 42) (58, 56) (45, 42)
(83, 75) (83, 74) (83, 81) (75, 74) (86, 81) (99, 95)
(81, 12) (81, 79) (81, 44) (81, 26) (81, 10) (81, 48) (81, 80) (81, 1) (81, 16) (81, 31)
(38, 12) (38, 26) (38, 10) (38, 1) (38, 16) (38, 31) (95, 84) (95, 12) (95, 79) (95, 44)
(95, 26) (95, 87) (95, 10) (95, 48) (95, 80) (95, 1) (95, 16) (95, 31) (95, 92) (84, 12)
(84, 79) (84, 44) (84, 26) (84, 10) (84, 48) (84, 80) (84, 1) (84, 16) (84, 31) (99, 12)
(99, 79) (99, 44) (99, 26) (99, 87) (99, 96) (99, 10) (99, 48) (99, 80) (99, 1) (99, 16)
(99, 31) (99, 92) (12, 10) (12, 1) (79, 44) (79, 26) (79, 10) (79, 48) (79, 1) (79, 16)
(79, 31) (44, 26) (44, 10) (44, 1) (44, 16) (44, 31) (26, 10) (26, 1) (26, 16) (87, 10)
(87, 48) (87, 80) (87, 1) (87, 16) (87, 31) (96, 10) (96, 48) (96, 80) (96, 1) (96, 16)
(96, 31) (96, 92) (10, 1) (48, 1) (48, 16) (48, 31) (80, 1) (80, 16) (80, 31) (31, 16)
Give the code and analyze the worst-, average-, and best-case of this
algorithm. Compare this algorithm with insertion sort.
Merge Sort
Sort 34 8 64 : 8, 34, 64
arrayout[k] = array2[i2];
++i2;
}
++k;
}
Merging Two Lists
We’re not finished yet, we have to empty out the remaining
array
The algorithm:
• Split the list into two approximately equal sub-lists
• Recursively call merge sort on both sub lists
• Merge the resulting sorted lists
The Merge Sort Algorithm
Question:
• we split the list into two sub-lists and sorted them
• how should we sort those lists?
Answer (theoretical):
• if the size of these sub-lists is > 1, use merge sort again
• if the sub-lists are of length 1, do nothing: a list of length
one is sorted
Code of Merge Sort
Run-time
The following table summarizes the run-times of merge sort
T(n) = 2T(N/2) + n => T(n) = Qnlogn)
• Insertion sort
• Merge sort