Professional Documents
Culture Documents
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Example
• The element in position Array[0] is certainly sorted. How does it work?
• Thus, move on to insert the second character, D,
into the appropriate location to maintain the • Each element Array[j] is taken one at a time
alphabetical order. from j = 0 to n-1.
SORTED UNSORTED
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
1
Insertion Sort technique. Insertion Sort technique.
SORTED UNSORTED SORTED
UNSORTED
Array D G Z F B E Array B D F G Z E
Index 0 1 2 3 4 5 Index 0 1 2 3 4 5
SORTED
SORTED UNSORTED
Array D F G Z B E Array B D E F G Z
Index 0 1 2 3 4 5 Index 0 1 2 3 4 5
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
2
Complexity
Selection Sort
• Worst situation: data in reverse order. The
inner loop is executed the maximum number The general idea of
of times. Thus the complexity of the insertion the selection sort
sort in this worst possible case is quadratic or is that for each
O(n2). slot, find the
element that
belongs there.
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Selection Sort Algorithm • The inner loop in the above algorithm finds
the location of the next smallest element in
for i = 0 to n-2
the array (the location of the next smallest
temp = a[i] element in the array).
loc = i;
for j = i+1 to n-1
• The outer loop moves along the array as the
if a[j] < a[loc]
elements are sorted.
loc = j
a[i] = a[loc]
a[loc] = temp • What is the complexity of selection sort?
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
MergeSort
Exercise:
• Merging involves the combination of two or more • Write a function which merges two sorted arrays of
ordered files into a single ordered file. length n (n >0) and m (m >0) respectively.
• Example: Merge the two files:
– 503, 703 , 765 • The function will have the following form:
087, 503, 512, 677, 703, 765
– 087, 512, 677 /**Merges the sorted subarrays A[p...q] and
• This is solved by comparing the two smallest items, A[q+1…r] to form a single sorted array A[p … r] */
output the smallest, and then repeat the process. MERGE (A,p,q,r)
Be careful when one of the two files become exhausted!
This Algorithm should be of complexity O(n)
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
3
Merge - example Merge - example
A 1 13 24 26 2 15 27 38 Compare 1 and 2, A 1 13 24 26 2 15 27 38
p q start r 1 is added to C, p q r
then compare 13 and 2.
start = q+1;
1 13 24 26 2 15 27 38
p q+1 C 1 2 13 15 24 26 27 38
1 p r
pos
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
4
Divide and Conquer
Merging Two Sequences (cont.)
• Divide:To solve a problem, it is subdivided into sub
problems each of which is similar to the given
problem.
• Conquer: Each of the sub problems is solved
independently
• Combine: The solutions to the sub problems are
combined in order to obtain the solution to the
original problem
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
5
Towers of Hanoi 2 Towers of Hanoi 2
Sub Problem 1: Get n-1 disks
from A to B
A B C A B C
n n-1
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
n-1 n
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Back to MergeSort…
Towers of Hanoi 2
• First move the n-1 smallest disks from A to C • In a Divide and conquer algorithm
exposing the nth smallest disk on peg A.
– two half-sized problems are solved
• Move that disk from A to B
recursively
• Then move the n-1 smallest disks from C to B
• The movement of the n-1 smallest disks is
accomplished by a recursive application of the
• MergeSort is such an algorithm
procedure
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
6
Merge Sort Merge-Sort
• Using this Divide and Conquer Strategy:
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
7
Merge-Sort (cont.)
Merge-Sort (cont.)
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
8
Mergesort
Merge sort 5,2,4,6,1,3,2,6
• MERGESORT(A,p,r) sorts the elements in the
1 2 2 3 4 5 6 6 subarray A[p..r]
MERGESORT(A,p,r) MERGESORT(A,p,r)
{ { When an algorithm
if the subarray has more than one element then if ( p < r ) then
contains a recursive
Divide array into two subarrays q = (p+r) / 2
Call MERGESORT on subarray 1 MERGESORT(A,p,q) call to itself its
Call MERGESORT on subarray 2 MERGESORT(A,q+1,r) running time can be
Merge these SORTED subarrays MERGE(A,p,q,r)
} } described by a
recurrence equation
When an algorithm contains a recursive call to itself its or recurrence
running time can be described by a recurrence equation
or recurrence.
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
9
Quicksort Quicksort
Given a list of elements, • And so on in a recursive manner until all the
– take a partitioning element sublists are empty, at which point the (total) list
is sorted
– and create a (sub)list
• Partitioning can be effected simultaneously,
• such that all elements to the left of the scanning left to right and right to left,
partitioning element are less than it, interchanging elements in the wrong parts of
• and all elements to the right of it are the list
greater than it. • The partitioning element is then placed
– Now repeat this partitioning effort on each between the resultant sublists (which are then
of these two sublist partitioned in the same manner)
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Example Example
G B S E V M P H D C ? G B C E V M P H D S ?
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
i=0 i=2
first = 0 first = 0
loc = 10 loc = 9
last = 9 last = 9
CS210/SE202
pivot = G CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
10
Example Example
i stops at 4 (a[4] or V>= G), and loc at 8 (a[8] or D <= G). i and loc continue to move until i > loc
These values are then swapped. This happens when i points to M and loc points to D
G B C E D M P H V S ? G B C E D M P H V S ?
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
11
Quicksort Quicksort
Given a list of elements, • And so on in a recursive manner until all the
sublists are empty, at which point the (total) list
– take a partitioning element is sorted
– and create a (sub)list • Partitioning can be effected simultaneously,
• such that all elements to the left of the scanning left to right and right to left,
partitioning element are less than it, interchanging elements in the wrong parts of
• and all elements to the right of it are the list
greater than it. • The partitioning element is then placed
– Now repeat this partitioning effort on each between the resultant sublists (which are then
of these two sublist partitioned in the same manner)
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
10 9 8 11 4 99
• A divide-and-conquer algorithm is one
that divides the problem into smaller
problems upon which it performs the
sentinel
same process.
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
12
Quicksort Quicksort
do{ do{
do { do {
10 9 8 11 4 99 i = i+1;
10 9 8 11 4 99 i = i+1;
} while (a[i] < pivot && (i < last)); } while (a[i] < pivot && (i < last));
do { i j do {
loc = loc-1; loc = loc-1;
QS(A, , ) } while (a[loc] > pivot); QS(A,1 ,6 ) } while (a[loc] > pivot);
if (i < loc){ if (i < loc){
L: swap (a(i), a(loc)) L: 1 swap (a(i), a(loc))
R: } R: 6 }
i: i j } while (i < loc); i: 1 } while (i < loc);
j: j: 6
pivot: swap (a(first), a(loc)) pivot: 10 swap (a(first), a(loc))
} }
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Quicksort Quicksort
do{ 10 9 8 4 11 99 do{
do { do {
10 9 8 11 4 99 i = i+1; i = i+1;
} while (a[i] < pivot && (i < last)); i j } while (a[i] < pivot && (i < last));
i j do { swap do {
loc = loc-1; loc = loc-1;
QS(A,1 ,6 ) } while (a[loc] > pivot); QS(A,1 ,6 ) } while (a[loc] > pivot);
if (i < loc){ if (i < loc){
L: 1 swap (a(i), a(loc)) L: 1 swap (a(i), a(loc))
R: 6 } R: 6 }
i: 1234 } while (i < loc); i: 1234 } while (i < loc);
j: 65 j: 65
pivot: 10 swap (a(first), a(loc)) pivot: 10 swap (a(first), a(loc))
} }
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Quicksort Quicksort
do{ do{
10 9 8 4 11 99 do { 4 9 8 10 11 99 do {
i = i+1; i = i+1;
} while (a[i] < pivot && (i < last)); } while (a[i] < pivot && (i < last));
j i j i
do { do {
loc = loc-1; loc = loc-1;
QS(A,1 ,6 ) } while (a[loc] > pivot); QS(A,1 ,6 ) } while (a[loc] > pivot);
if (i < loc){ if (i < loc){
L: 1 swap (a(i), a(loc)) L: 1 swap (a(i), a(loc))
R: 6 } R: 6 }
i: 12345 } while (i < loc); i: 12345 } while (i < loc);
j: 654 j: 654
pivot: 10 swap (a(first), a(loc)) pivot: 10 swap (a(first), a(loc))
} }
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
13
Quicksort
QS(A,1,6)
Quicksort
L: 1
4 9 8 10 11 99 R: 6 4 9 8 10 11 99
i: 1 2 3 4 5
j: 6 5 4
pivot: 10 i j
QS(A,1,4) QS(A,5,6)
L: 1 L: 5
QS(A,1,4) QS(A,5,6)
R: 4 R: 6
i j i: 1 i: 5
L: 1 L: 5
R: 4 R: 6 j: 4 j: 6
i: i: pivot: 4 pivot: 11
j: j:
CS210/SE202 pivot: 4 pivot: 11 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
Quicksort
QS(A,1,4)
Quicksort
L: 1
R: 4
4 9 8 10 11 99 i j i: 1 2
j: 4 3 2 1
pivot: 4
j i 4 9 8 10 11 99
QS(A,1,4) QS(A,5,6)
L: 1 L: 5
R: 4 R: 6 QS(A,1,1) QS(A,2,4)
i: 1 2 i: 5 L: 1 L: 2
j: 4 3 2 1 j: 6 R: 1 R: 4
pivot: 4 pivot: 11 i: i:
j: j:
CS210/SE202 CS210/SE202 pivot: 4 pivot: 9
Data Structures and Algorithms Data Structures and Algorithms
Quicksort Quicksort
4 9 8 10 11 99 4 9 8 10 11 99
i j
i j
L: 2 L: 5 L: 2 L: 5
L: 1
R: 4 R: 6 R: 4 R: 6
R: 1
i: i: 5 i: 2 i: 5
i:
j: j: 6 j: 4 j: 6
j:
pivot: 9 pivot: 11 pivot: 9 pivot: 11
pivot: 4
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
14
Quicksort
Quicksort QS(A,2,4)
i j
4 9 8 10 11 99 L: 2
R: 4
4 8 9 10 11 99 i: 234
j i j: 43
pivot: 9
QS(A,2,4) QS(A,5,6)
QS(A,2,3) QS(A,4,4) QS(A,5,6)
L: 2 L: 5
R: 4 R: 6 L: 2 L: 4 L: 5
i: 234 i: 5 R: 3 R: 4 R: 6
j: 43 j: 6 i: i: i: 5
pivot: 9 pivot: 11 j: j: j: 6
CS210/SE202 CS210/SE202 pivot: 8 pivot: 10 pivot: 11
Data Structures and Algorithms Data Structures and Algorithms
Quicksort Quicksort
4 8 9 10 11 99 4 8 9 10 11 99
i j j i
L: 2 L: 4 L: 5 L: 2 L: 4 L: 5
R: 3 R: 4 R: 6 R: 3 R: 4 R: 6
i: 2 i: i: 5 i: 2 3 i: i: 5
j: 3 j: j: 6 j: 3 2 j: j: 6
CS210/SE202 pivot: 8 pivot: 10 pivot: 11 CS210/SE202 pivot: 8 pivot: 10 pivot: 11
Data Structures and Algorithms Data Structures and Algorithms
Quicksort QS(A,2,3)
L: 2 Quicksort
R: 3
i: 2 3
j: 3 2 4 8 9 10 11 99
4 8 9 10 11 99 pivot: 8
i j i j
L: 2 L: 3 L: 4 L: 5
R: 2 R: 3 R: 4 R: 6
i: i: i: i: 5
j: j: j: j: 6
CS210/SE202 pivot: 8 pivot: 9 CS210/SE202 pivot: 10 pivot: 11
Data Structures and Algorithms Data Structures and Algorithms
15
QS(A,5,6)
Quicksort
L: 5
Quicksort R: 6
• Fastest known sorting algorithm
i: 5
j: 6 • Relies on recursion and relatively simple
4 8 9 10 11 99 pivot: 11
i j to understand
QS(A,5,5) QS(A,6,6)
L: 5 L: 6
R: 5 R: 6
i: i:
j: j:
pivot: 11 pivot: 99
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
16
Median-of-three Partitioning A partitioning strategy
• This is an attempt to pick a better than average
• We look at a simple strategy for partitioning
pivot. and then at the advantages of median-of-three
• We take a sample of three numbers and find partitioning
their median, the first, middle and last • First get the pivot out of the way by swapping it
elements. with the last element.
– E.g. input: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0
– leftmost is 8, rightmost is 0, middle is 6 8 1 4 9 0 3 5 2 7 6
– Here 6 is the pivot. • Move all small elements to the left part of the
array and all large elements to the right part
– small and large are relative to the pivot.
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
• AS the algorithm continues i stops at 9 and j • scan continues with i stopping at 9 and j
stops at 5. elements that i and j skip are stopping at 3. However i and j have crossed
guaranteed to be correctly placed. positions so a swap would be useless. We can
see that 3 is already correctly placed.
2 1 4 9 0 3 5 8 7 6
i j 2 1 4 5 0 3 9 8 7 6
j i
• elements 9 and 5 are then swapped. • All but two items are corrrectly placed, so swap
element in position i with last cell (the pivot).
2 1 4 5 0 3 9 8 7 6 2 1 4 5 0 3 6 8 7 9
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
17
Keys equal to the pivot Median-of-three partitioning
• We require that both i and j stop when they • With median-of-three partitioning we do a
encounter an element equal to the pivot. simple optimisation that saves comparisons and
simiplifies the code.
• This avoids a worst-case run-time for the
algorithm which is the same as using the first • The easiest way to find the median of the first,
element as the pivot. middle and last elements is to sort them.
8 1 4 9 6 3 5 2 7 0
the original array
0 1 4 9 6 3 5 2 7 8 0 1 4 9 6 3 5 2 7 8
result of sorting (first, middle, last)
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
CS210/SE202 CS210/SE202
Data Structures and Algorithms Data Structures and Algorithms
18