Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | nguyenhoangcam |
View: | 221 times |
Download: | 0 times |
of 114
8/10/2019 Chapter 4_Sorting Algorithm
1/114
TrngCao ngCng nghThc
DATA STRUCTURES &
ALGORITHMS
Chapter 4: Sorting Algorithms
Teacher:Van Nguyen
E-mail: [email protected]
Phone: 0985537687
mailto:[email protected]:[email protected]8/10/2019 Chapter 4_Sorting Algorithm
2/114
Data structures and algorithms
Goals
At the end of this chapter you should be able to: Explain interchange sort and its analysis
Explain insertion sort and its analysis
Explain selection sort and its analysis
Explain bubble sort and its analysis Explain heapsort and its analysis
Explain merge sort and its analysis
Explain quicksort and its analysis
Demo
Analysis and compare the algorithms
11/26/2014 2
8/10/2019 Chapter 4_Sorting Algorithm
3/114
8/10/2019 Chapter 4_Sorting Algorithm
4/114
Data structures and algorithms
Introduction to Sorting
The Sorting Problem?
Input:
A sequence of n numbers a1, a2, . . . , an
Output:
Apermutation (reordering) a1, a2, . . . , an of the input
sequence such that a1 a2 an
11/26/2014 4
8/10/2019 Chapter 4_Sorting Algorithm
5/114
Data structures and algorithms
Introduction to Sorting
What is Sorting?Sorting: an operation that segregates items into groups according
to specif ied cri ter ion
EX:
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
11/26/2014 5
8/10/2019 Chapter 4_Sorting Algorithm
6/114
Data structures and algorithms
Introduction to Sorting
Why Study Sorting Algorithms? There are a variety of situations that we can encounter
Do we have randomly ordered keys?
Are all keys distinct?
How large is the set of keys to be ordered?
Need guaranteed performance?
Various algorithms are better suited to some of these
situations
11/26/2014 6
8/10/2019 Chapter 4_Sorting Algorithm
7/114Data structures and algorithms
Introduction to Sorting
Examples: Sorting Books in Library (Dewey system)
Sorting Individuals by Height (Feet and
Inches) Sorting Movies in Blockbuster
(Alphabetical)
Sorting Numbers (Sequential)
11/26/2014 7
8/10/2019 Chapter 4_Sorting Algorithm
8/114Data structures and algorithms
Introduction to Sorting
Types of Sorting Algorithms Interchange sort
Insertion sort
Selection sort
Bubble sort Heap sort
Merge sort
Quick sort
11/26/2014 8
8/10/2019 Chapter 4_Sorting Algorithm
9/114Data structures and algorithms
Interchange sortIdea
Exchange sorts attempt to improve theordering by comparing elements in pairs and
interchanging them if they are not in sorted
order.
This operation is repeated until the table is
sorted.
11/26/2014 9
8/10/2019 Chapter 4_Sorting Algorithm
10/114Data structures and algorithms
Interchange sort - Algorithms
Step 1 : i = 1; // starting from the first row Step 2 : j = i+1; // find the rear element i
Step 3 :
While j n doIf a[j]< a[i] Swap(a[i], a[j]);
j = j+1;
Step 4 : i = i+1;
o If i < n: Step 2
o Else if End
11/26/2014 10
8/10/2019 Chapter 4_Sorting Algorithm
11/114Data structures and algorithms
2 8 5 1 6 4 1512
2 3 4 5 6 7 81
i
j
1
Interchange sortExample
11/26/2014 11
8/10/2019 Chapter 4_Sorting Algorithm
12/114Data structures and algorithms
12 8 5 2 6 4 151
2 3 4 5 6 7 81
i
j
2
Interchange sortExample
11/26/2014 12
8/10/2019 Chapter 4_Sorting Algorithm
13/114Data structures and algorithms
2 12 8 5 6 4 151
2 3 4 5 6 7 81
i
j
4
Interchange sortExample
11/26/2014 13
8/10/2019 Chapter 4_Sorting Algorithm
14/114
Data structures and algorithms
2 4 12 8 6 5 151
2 3 4 5 6 7 81
i
j
5
Interchange sortExample
11/26/2014 14
8/10/2019 Chapter 4_Sorting Algorithm
15/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
Interchange sortExample
11/26/2014 15
8/10/2019 Chapter 4_Sorting Algorithm
16/114
Data structures and algorithms
Interchange sort - Code for Algorithms
template
void interchangeSort (Item a[], int N )
{
int i, j;
for (i = 0 ; i < N-1; i++)
for (j = i + 1; j < N ; j++)
if (a[j ] < a[i])
swap (a[i], a[j]);
}
11/26/2014 16
8/10/2019 Chapter 4_Sorting Algorithm
17/114
Data structures and algorithms
Interchange sortAnalysis
Running time depends on not only the size of thearray but also the contents of the array.
Best-case: O(n2)
Array is already sorted in ascending order.
Swap will not be executed. The number of swap: 0 O(1)
The number of key comparisons: n(n-1)/2O(n2)
11/26/2014 17
8/10/2019 Chapter 4_Sorting Algorithm
18/114
Data structures and algorithms
Interchange sortAnalysis (cont.)
Worst-case:O(n
2
)
Array is in reverse order:
Swap is executed i-1 times, for i = 2,3, , n
The number of moves: n*(n-1)/2 O(n2)
The number of key comparisons: n*(n-1)/2O(n2)
Average-case: O(n2)
We have to look at all possible initial dataorganizations.
So, Interchange Sort is O(n2)
11/26/2014 18
8/10/2019 Chapter 4_Sorting Algorithm
19/114
Data structures and algorithms
Insertion sortIdea
Insertion sort is a simple sorting algorithm that isappropriate for small inputs.
Most common sorting technique used by cardplayers.
The list is divided into two parts: sorted andunsorted.
In each pass, the first element of the unsorted part ispicked up, transferred to the sorted sublist, and
inserted at the appropriate place. A list of nelements will take at most n-1passes to
sort the data.
11/26/2014 19
8/10/2019 Chapter 4_Sorting Algorithm
20/114
Data structures and algorithms
Insertion sort - Algorithms
Step 1: Starting i = 1 // a[0] is orderedStep 2: x = a[i]. Find the appropriate position pos in
the segment a[0] ... a [i-1] to insert x.
Step 3: Move the segment a[pos] ... a [i-1] to have a
place to put x on.
Step 4: a[pos] = x
Step 5: i = i + 1
oIf i
8/10/2019 Chapter 4_Sorting Algorithm
21/114
Data structures and algorithms
2 8 5 1 6 4 1512
2 3 4 5 6 7 81
Insertion sort - Example
11/26/2014 21
8/10/2019 Chapter 4_Sorting Algorithm
22/114
8/10/2019 Chapter 4_Sorting Algorithm
23/114
Data structures and algorithms
12 8 5 1 6 4 152
i
x
2 3 4 5 6 7 81pos
Insert a
3
into 1, 3)
8
Insertion sort - Example
11/26/2014 23
8/10/2019 Chapter 4_Sorting Algorithm
24/114
Data structures and algorithms
8 12 5 1 6 4 152
i
x
2 3 4 5 6 7 81pos
Insert a
4
into 1, 4)
5
Insertion sort - Example
11/26/2014 24
8/10/2019 Chapter 4_Sorting Algorithm
25/114
Data structures and algorithms
5 8 12 1 6 4 152
i
x
2 3 4 5 6 7 81pos
Insert a
5
into 1, 5)
1
Insertion sort - Example
11/26/2014 25
8/10/2019 Chapter 4_Sorting Algorithm
26/114
8/10/2019 Chapter 4_Sorting Algorithm
27/114
Data structures and algorithms
2 5 6 8 12 4 151
i
x
2 3 4 5 6 7 81pos
Insert a
7
into 1, 7)
4
Insertion sort - Example
11/26/2014 27
8/10/2019 Chapter 4_Sorting Algorithm
28/114
Data structures and algorithms
2 4 5 6 8 12 151
i
x
2 3 4 5 6 7 81pos
Insert a
8
into 1, 8)
15
Insertion sort - Example
11/26/2014 28
8/10/2019 Chapter 4_Sorting Algorithm
29/114
Data structures and algorithms
2 4 5 6 8 12 151
pos2 3 4 5 6 7 81
Insertion sort - Example
11/26/2014 29
8/10/2019 Chapter 4_Sorting Algorithm
30/114
Data structures and algorithms
Insertion sort - Code for Algorithms
//increase
template
void insertionSort(Item a[], int n)
{
int pos;
for (int i = 1; i < n; i++)
{
Item x = a[i];
for (pos=i; pos > 0 && x
8/10/2019 Chapter 4_Sorting Algorithm
31/114
Data structures and algorithms
Insertion sortAnalysis
Running time depends on not only the size of thearray but also the contents of the array.
Best-case: O(n)
Array is already sorted in ascending order.
Inner loop will not be executed. The number of moves: 2*(n-1) O(n)
The number of key comparisons: (n-1) O(n)
11/26/2014 31
8/10/2019 Chapter 4_Sorting Algorithm
32/114
Data structures and algorithms
Insertion sortAnalysis (cont.)
Worst-case: O(n2)
Array is in reverse order:
Inner loop is executed i-1 times, for i = 2,3, , n
The number of moves: 2*(n-1)+(1+2+...+n-1)=
2*(n-1)+ n*(n-1)/2 O(n2) The number of key comparisons: (1+2+...+n-1)=
n*(n-1)/2 O(n2)
Average-case: O(n2)
We have to look at all possible initial dataorganizations.
So, Insertion Sort is O(n2)
11/26/2014 32
8/10/2019 Chapter 4_Sorting Algorithm
33/114
Data structures and algorithms
Selection sortIdea
The list is divided into two sublists, sortedand unsorted, which
are divided by an imaginary wall.
We find the smallest element from the unsorted sublist andswap it with the element at the beginning of the unsorted data.
After each selection and swapping, the imaginary wall between
the two sublists move one element ahead, increasing thenumber of sorted elements and decreasing the number ofunsorted ones.
Each time we move one element from the unsorted sublist tothe sorted sublist, we say that we have completed a sort pass.
A list of nelements requires n-1passes to completely rearrangethe data.
11/26/2014 33
8/10/2019 Chapter 4_Sorting Algorithm
34/114
Data structures and algorithms
Selection sortAlgorithms
Step 1: Starting i = 1 Step 2: Find the smallest element a[min] in the
current range from a[i] to a[N]
Step 3: Swap a[min] and a[i]
Step 4: i = i + 1
o If i
8/10/2019 Chapter 4_Sorting Algorithm
35/114
Data structures and algorithms
2 8 5 1 6 4 1512
i
min2 3 4 5 6 7 81
Find MinPos 1, 8) Swap a
i
, a
min
)
Selection sortExample
11/26/2014 35
8/10/2019 Chapter 4_Sorting Algorithm
36/114
Data structures and algorithms
2 8 5 12 6 4 151
i
min2 3 4 5 6 7 81
Find MinPos 2, 8) Swap a
i
, a
min
)
Selection sortExample
11/26/2014 36
8/10/2019 Chapter 4_Sorting Algorithm
37/114
Data structures and algorithms
2 8 5 12 6 4 151
i
min2 3 4 5 6 7 81
Find MinPos 3, 8) Swap a
i
, a
min
)
Selection sortExample
11/26/2014 37
8/10/2019 Chapter 4_Sorting Algorithm
38/114
Data structures and algorithms
2 4 5 12 6 8 151
i
min2 3 4 5 6 7 81
Find MinPos 4, 8) Swap a
i
, a
min
)
Selection sortExample
11/26/2014 38
8/10/2019 Chapter 4_Sorting Algorithm
39/114
Data structures and algorithms
2 4 5 12 6 8 151
i
min2 3 4 5 6 7 81
Find MinPos 5, 8) Swap a
i
, a
min
)
Selection sortExample
11/26/2014 39
8/10/2019 Chapter 4_Sorting Algorithm
40/114
Data structures and algorithms
2 4 5 6 12 8 151
i
min2 3 4 5 6 7 81
Find MinPos 6, 8)
Swap a
i
, a
min
)
Selection sortExample
11/26/2014 40
8/10/2019 Chapter 4_Sorting Algorithm
41/114
Data structures and algorithms
2 4 5 6 8 12 151
i
min2 3 4 5 6 7 81
Find MinPos 7, 8)
Swap a
i
, a
min
)
12 15
Selection sortExample
11/26/2014 41
8/10/2019 Chapter 4_Sorting Algorithm
42/114
Data structures and algorithms
Selection sort - Code for Algorithms
template
void selectionSort (Item a[], int n){
for (int i = 0; i < n-1; i++)
{
int min = i;
for (int j = i+1; j < n; j++)if (a[j] < a[min]) min = j;
swap(a[i], a[min]);
}
}
template < class Object>void swap (Object &lhs, Object &rhs){
Object tmp = lhs;lhs = rhs;rhs = tmp;
}
11/26/2014 42
8/10/2019 Chapter 4_Sorting Algorithm
43/114
Data structures and algorithms
Selection sortAnalysis
In selectionSort function, the outer for loopexecutes n-1 times.
We invoke swap function once at eachiteration.
Total Swaps: n-1
Total Moves: 3*(n-1) (Each swap hasthree moves)
11/26/2014 43
8/10/2019 Chapter 4_Sorting Algorithm
44/114
Data structures and algorithms
Selection sortAnalysis (cont.)
The inner for loop executes the size of theunsorted part minus 1 (from 1 to n-1), and ineach iteration we make one key comparison.
# of key comparisons = 1+2+...+n-1 =
n*(n-1)/2So, Selection sort is O(n2)
The best case, the worst case, and the
average case of the selection sort algorithmare same. all of them are O(n2)
11/26/2014 44
8/10/2019 Chapter 4_Sorting Algorithm
45/114
Data structures and algorithms
Bubble sortIdea
The list is divided into two sublists: sorted andunsorted.
The smallest element is bubbled from the unsortedlist and moved to the sorted sublist.
After that, the wall moves one element ahead,increasing the number of sorted elements anddecreasing the number of unsorted ones.
Each time an element moves from the unsorted part
to the sorted part one sort pass is completed. Given a list of n elements, bubble sort requires up to
n-1 passes to sort the data.
11/26/2014 45
8/10/2019 Chapter 4_Sorting Algorithm
46/114
Data structures and algorithms
Bubble sortAlgorithms
Step 1: Starting i = 1
Step 2: j = N
While j> i do:
o If a[j]
8/10/2019 Chapter 4_Sorting Algorithm
47/114
Data structures and algorithms
2 8 5 1 6 4 1512
2 3 4 5 6 7 81
i
j
1
Bubble sortExample
11/26/2014 47
8/10/2019 Chapter 4_Sorting Algorithm
48/114
Data structures and algorithms
12 2 8 5 4 6 151
2 3 4 5 6 7 81
i
j
2
Bubble sortExample
11/26/2014 48
8/10/2019 Chapter 4_Sorting Algorithm
49/114
Data structures and algorithms
2 12 4 8 5 6 151
2 3 4 5 6 7 81
i
j
4
Bubble sortExample
11/26/2014 49
8/10/2019 Chapter 4_Sorting Algorithm
50/114
Data structures and algorithms
2 4 12 8 5 6 151
2 3 4 5 6 7 81
i
j
5
Bubble sortExample
11/26/2014 50
8/10/2019 Chapter 4_Sorting Algorithm
51/114
Data structures and algorithms
2 4 5 12 8 6 151
2 3 4 5 6 7 81
i
j
6
Bubble sortExample
11/26/2014 51
8/10/2019 Chapter 4_Sorting Algorithm
52/114
Data structures and algorithms
2 4 5 6 12 8 151
2 3 4 5 6 7 81
i
j
8
Bubble sortExample
11/26/2014 52
8/10/2019 Chapter 4_Sorting Algorithm
53/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
i
j
1512
Bubble sortExample
11/26/2014 53
8/10/2019 Chapter 4_Sorting Algorithm
54/114
Data structures and algorithms
Bubble sort - Code for Algorithms
template
void bubbleSort (Item a[], int n){
for (int i = 0; i < n-1; i++)
for (int j = n - 1; j > i; j--)
if (a[j-1] > a[j])
swap(a[j],a[j-1]);
}
11/26/2014 54
A i
8/10/2019 Chapter 4_Sorting Algorithm
55/114
Data structures and algorithms
Bubble sortAnalysis
Best-case: O(n) Array is already sorted in ascending order.
The number of moves: 0 O(1)
The number of key comparisons: (n-1) O(n)
11/26/2014 55
B bbl A l i ( )
8/10/2019 Chapter 4_Sorting Algorithm
56/114
Data structures and algorithms
Bubble sortAnalysis (cont.)
Worst-case: O(n2
) Array is in reverse order:
Outer loop is executed n-1 times,
The number of moves: 3*(1+2+...+n-1) = 3 * n*(n-1)/2
O(n2
) The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2
O(n2)
Average-case: O(n2) We have to look at all possible initial data organizations.
So, Bubble Sort is O(n2)
11/26/2014 56
H H
8/10/2019 Chapter 4_Sorting Algorithm
57/114
Data structures and algorithms
Heap sortHeap
What is a Heap?
A heap is also known as a priority queue and can be
represented by a binary tree with the following
properties:
Structure property: A heap is a completely filledbinary tree with the exception of the bottom row,
which is filled from left to right
Heap Order property: For every node x in theheap, the parent of x greater than or equal to the
value of x. (known as a maxHeap).
11/26/2014 57
8/10/2019 Chapter 4_Sorting Algorithm
58/114
H t H
8/10/2019 Chapter 4_Sorting Algorithm
59/114
Data structures and algorithms
Heap sortHeap
53
44 25
15 21 13 18
3 12 5 7
0 1 2 3 4 5 6 7 8 9 10 11
53 44 25 15 21 13 18 3 12 5 7
For any node i, the following formulas apply:
The index of its parent = i / 2
Index of left child = 2 * i
Index of right child = 2 * i + 1
11/26/2014 59
H t Al ith
8/10/2019 Chapter 4_Sorting Algorithm
60/114
Data structures and algorithms
Heap sortAlgorithms
Step 1: Build Heap Step 2: Swap the first element with the
end element, then reduce size of array to
n-1 Step 3: Continue step 1 and 2 until size of
array is 1
11/26/2014 60
H t E l b ild h
8/10/2019 Chapter 4_Sorting Algorithm
61/114
Data structures and algorithms
2 8 5 1 6 4 1512
2 3 4 5 6 7 81
Heap sortExample build heap
11/26/2014 61
H t E l b ild h
8/10/2019 Chapter 4_Sorting Algorithm
62/114
Data structures and algorithms
152 8 5 1 6 412
2 3 4 5 6 7 81
left
jointjointcurr
5
Heap sortExample build heap
11/26/2014 62
H t E l b ild h
8/10/2019 Chapter 4_Sorting Algorithm
63/114
Data structures and algorithms
2 8 15 1 6 4 512
2 3 4 5 6 7 81
left
jointjointcurr joint
8
Heap sortExample build heap
11/26/2014 63
H t E l b ild h
8/10/2019 Chapter 4_Sorting Algorithm
64/114
Data structures and algorithms
15 8 5 1 6 4 212
2 3 4 5 6 7 81
left
jointjointcurr
Heap sortExample build heap
11/26/2014 64
H t E l b ild h
8/10/2019 Chapter 4_Sorting Algorithm
65/114
Data structures and algorithms
12 8 5 1 6 4 215
2 3 4 5 6 7 81
Heap sortExample build heap
11/26/2014 65
H t E l d hift
8/10/2019 Chapter 4_Sorting Algorithm
66/114
Data structures and algorithms
12 8 5 1 6 4 215
2 3 4 5 6 7 81
right
Swap(a1, aright)
Heap sortExample swap and shift
11/26/2014 66
H t E l d hift
8/10/2019 Chapter 4_Sorting Algorithm
67/114
Data structures and algorithms
12 8 5 1 6 4 152
2 3 4 5 6 7 81
right
Shift(a, 1, right)
jointjointcurr
Heap sortExample swap and shift
11/26/2014 67
Heap sort Example swap and shift
8/10/2019 Chapter 4_Sorting Algorithm
68/114
Data structures and algorithms
5 8 2 1 6 4 1512
2 3 4 5 6 7 81
right
Swap(a1, aright)
Heap sortExample swap and shift
11/26/2014 68
Heap sort Example swap and shift
8/10/2019 Chapter 4_Sorting Algorithm
69/114
Data structures and algorithms
5 8 2 1 6 12 154
2 3 4 5 6 7 81
right
jointjointcurr joint
Shift(a, 1, right)
Heap sortExample swap and shift
11/26/2014 69
Heap sort Example swap and shift
8/10/2019 Chapter 4_Sorting Algorithm
70/114
Data structures and algorithms
5 6 2 1 4 12 158
2 3 4 5 6 7 81
right
Swap(a1, aright)
Heap sortExample swap and shift
11/26/2014 70
Heap sort Example swap and shift
8/10/2019 Chapter 4_Sorting Algorithm
71/114
Data structures and algorithms
5 6 2 1 8 12 154
2 3 4 5 6 7 81
2 4 5 6 8 12 151
Heap sortExample swap and shift
11/26/2014 71
Heap sort Code for Algorithms
8/10/2019 Chapter 4_Sorting Algorithm
72/114
Data structures and algorithms
Heap sort - Code for Algorithms
template voidShift(Item a[], intleft, intright)
{
int x, curr, joint;curr = left; joint = 2 * curr + 1
x = a[curr];
while(joint
8/10/2019 Chapter 4_Sorting Algorithm
73/114
Data structures and algorithms
Heap sort - Code for Algorithms (cont.)
template
voidCreateHeap (Item a[], intN){
int left;
for(left = (N - 1) / 2; left >= 0; left--)
Shift(a, left, N-1);
}
11/26/2014 73
Heap sort Code for Algorithms (cont )
8/10/2019 Chapter 4_Sorting Algorithm
74/114
Data structures and algorithms
Heap sort - Code for Algorithms (cont.)
template
void HeapSort(Item a[], intN){
int right;
CreateHeap (a, N);
right = N - 1;
while (right > 0)
{
Swap (a[0],a[right]);
right--;
Shift (a,0,right);
}
}
11/26/2014 74
Heap sort Analysis
8/10/2019 Chapter 4_Sorting Algorithm
75/114
Data structures and algorithms
Heap sortAnalysis
Step 1- Build heap, O(n) time complexity
Step 2 and 3 perform n swap and shift operations,
each with O(log(n)) time complexity
total time complexity = O(n log(n))
Pros: fast sorting algorithm, memory efficient,
especially for very large values of n.
Cons: slower of the O(n log(n)) sorting algorithms
11/26/2014 75
Merge sort Idea
8/10/2019 Chapter 4_Sorting Algorithm
76/114
Data structures and algorithms
Merge sortIdea
Mergesort algorithm is one of two important divide-
and-conquer sorting algorithms (the other one is
quicksort).
It is a recursive algorithm.
Divides the list into halves, Sort each halve separately, and
Then merge the sorted halves into one sorted
array.
11/26/2014 76
Merge sort Algorithms
8/10/2019 Chapter 4_Sorting Algorithm
77/114
Data structures and algorithms
Merge sortAlgorithms
Step 1: Start with k = 1
Step 2: Distribution of array A = a1, a2, , aninto
two arrays B, C following:
B= a1, ,ak, a2k+1, , a3k,
C=ak+1, , a2k, a3k+1, , a4k
Step 3: From 2 arrays B, C, we merge each pair
subarray and put into array A.
Step 4: k = k * 2o If k
8/10/2019 Chapter 4_Sorting Algorithm
78/114
Data structures and algorithms
2 8 5 1 6 41512
2 3 4 5 6 7 81
k = 1;
Merge sortExample
11/26/2014 78
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
79/114
Data structures and algorithms
2 8 5 1 6 41512
2 3 4 5 6 7 81
k = 1;
Merge sortExample
11/26/2014 79
8/10/2019 Chapter 4_Sorting Algorithm
80/114
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
81/114
Data structures and algorithms
2
8
5
1
6
4
15
12
2 3 4 5 6 7 81
k = 1;
Merge sortExample
11/26/2014 81
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
82/114
Data structures and algorithms
12 5 8 1 6 4 152
2 3 4 5 6 7 81
k = 2;
Merge sortExample
11/26/2014 82
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
83/114
Data structures and algorithms
5
12
8
1
4
6
15
2
2 3 4 5 6 7 81
k = 2;
Merge sort Example
11/26/2014 83
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
84/114
Data structures and algorithms
5
12
8
1
4
6
15
2
2 3 4 5 6 7 81
k = 2;
Merge sort Example
11/26/2014 84
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
85/114
Data structures and algorithms
5 8 12 1 4 6 152
2 3 4 5 6 7 81
k = 4;
Merge sort Example
11/26/2014 85
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
86/114
Data structures and algorithms
1
5
4
8
6
12
15
2
2 3 4 5 6 7 81
k = 4;
Merge sort Example
11/26/2014 86
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
87/114
Data structures and algorithms
1
5
4
8
6
12
15
2
2 3 4 5 6 7 81
k = 4;
Merge sort Example
11/26/2014 87
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
88/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
k = 8;
STOP
Only one subarray
Merge sort Example
11/26/2014 88
Merge sort Example
8/10/2019 Chapter 4_Sorting Algorithm
89/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
Merge sort Example
11/26/2014 89
Merge sort - Code for Algorithms
8/10/2019 Chapter 4_Sorting Algorithm
90/114
Data structures and algorithms
Merge sort Code for Algorithms
template
intb[MAX], c[MAX], nb, nc;
intmin(int a,intb)
{
if (a > b) returnb;
else return a;
}
voidMergeSort (Item a[], intN)
{
intk;
for (k = 1; k < N; k *= 2){
Distribute(a, N, nb, nc, k);
Merge(a, nb, nc, k);
}
}11/26/2014 90
Merge sort - Code for Algorithms (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
91/114
Data structures and algorithms
Merge sort Code for Algorithms (cont.)
template
voidDistribute (Item a[], int N,int &nb, int &nc, int k)
{
int i, pa, pb, pc;
pa = pb = pc = 0;
while (pa < N){
for(i=0; (pa
8/10/2019 Chapter 4_Sorting Algorithm
92/114
Data structures and algorithms
Merge sort Code for Algorithms (cont.)
template
voidMerge (Item a[], int nb, int nc, int k){
int pa, pb, pc;
pa = pb = pc = 0;
while ((pb < nb) && (pc < nc))
MergeSubarr (a, nb, nc, pa, pb, pc, k);
while (pb < nb)
a[pa ++] = b[pb++];
while (pc < nc)
a[pa++] = c[pc++];}
11/26/2014 92
Merge sort - Code for Algorithms (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
93/114
Data structures and algorithms
Merge sort Code for Algorithms (cont.)
template
voidMergeSubarr (inta[], int nb, int nc,int &pa, int &pb, int &pc, int k)
{
int rb, rc;
rb =min (nb, pb + k); rc =min (nc, pb + k);
while ((pb < rb) && (pc < rc))
if(b[pb] < c[pc])
a[pa++] = b[pb++];
else a[pa++] = c[pc++];
while (pb < rb)
a[pa++] = b[pb++];
while (pc < rc)a[pa++] = c[pc++];
}
11/26/2014 93
Merge sort Analysis
8/10/2019 Chapter 4_Sorting Algorithm
94/114
Data structures and algorithms
Merge sort Analysis
Merging two sorted arrays of size k
Best-case:
All the elements in the first array are smaller (or larger) than all the
elements in the second array.
The number of moves: 2k + 2k
The number of key comparisons: k
Worst-case:
The number of moves: 2k + 2k
The number of key comparisons: 2k-1
...... ......
......
0 k-1 0 k-1
0 2k-1
11/26/2014 94
Merge sortAnalysis (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
95/114
Data structures and algorithms
e ge so ys s (co .)
.
.
.
.
.
.
. . . . . . . . . . . . . . . . .
2m
2m-1 2
m-1
2m-2 2m-2 2m-2 2m-2
20
20
level 0 : 1 merge (size 2m-1
)
level 1 : 2 merges (size 2m-2)
level 2 : 4 merges (size 2m-3)
level m
level m-1 : 2m-1 merges (size 20)
11/26/2014 95
8/10/2019 Chapter 4_Sorting Algorithm
96/114
Merge sortAnalysis (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
97/114
Data structures and algorithms
g y ( )
Mergesort is extremely efficient algorithm with
respect to time.
Both worst case and average cases are O (n * log2n )
But, mergesort requires an extra array whose sizeequals to the size of the original array.
If we use a linked list, we do not need an extra array
But, we need space for the links
And, it will be difficult to divide the list into half ( O(n) )
11/26/2014 97
QuicksortIdea
8/10/2019 Chapter 4_Sorting Algorithm
98/114
Data structures and algorithms
Q
Like mergesort, Quicksort is also based on
the divide-and-conquerparadigm.
But it uses this technique in a somewhat opposite
manner,
as all the hard work is done before the recursivecalls.
It works as follows:
1. First, it partitions an array into two parts,2. Then, it sorts the parts independently,
3. Finally, it combines the sorted subsequences by
a simple concatenation.
11/26/2014 98
QuicksortIdea (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
99/114
Data structures and algorithms
Q ( )
The quick-sort algorithm consists of the following three
steps:
1. Divide: Partition the list.
To partition the list, we first choose some element from the
list for which we hope about half the elements will comebefore and half after. Call this element the pivot.
Then we partition the elements so that all those with values
less than the pivot come in one sublist and all those with
greater values come in another.
2. Recursion: Recursively sort the sublists separately.
3. Conquer: Put the sorted sublists together.
11/26/2014 99
QuicksortAlgorithms
8/10/2019 Chapter 4_Sorting Algorithm
100/114
Data structures and algorithms
Q g
Step 1: If left>=right: STOP
Step 2: Choose any element a[k] is pivot (l k r):
x = a [k]; i = l; j = r;
Step 3: Detect and correct pair of elements a[i], a[j] is the
wrong position:
o Step 3a: while (a [i] x) j--;
o Step 3c: If i
8/10/2019 Chapter 4_Sorting Algorithm
101/114
Data structures and algorithms
2 8 5 1 6 4 1512
2 3 4 5 6 7 81
left right
5
STOP
Not less than X
i j
STOP
Not greater than X
PartitionQ p
11/26/2014 101
QuicksortExample
8/10/2019 Chapter 4_Sorting Algorithm
102/114
Data structures and algorithms
2 8 5 1 6 12 154
2 3 4 5 6 7 81
left right
5
STOP
Not less than X
i j
STOP
Not greater than X
PartitionQ p
11/26/2014 102
QuicksortExample
8/10/2019 Chapter 4_Sorting Algorithm
103/114
Data structures and algorithms
2 1 5 8 6 12 154
2 3 4 5 6 7 81
left right
ij
Q p
11/26/2014 103
QuicksortExample
8/10/2019 Chapter 4_Sorting Algorithm
104/114
Data structures and algorithms
6
2 4 5 8 6 12 151
2 3 4 5 6 7 81
left right
i j
STOPNot less than X
STOPNot greater than X
PartitionQ p
11/26/2014 104
QuicksortExample
8/10/2019 Chapter 4_Sorting Algorithm
105/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
left right
ij
Q p
11/26/2014 105
QuicksortExample
8/10/2019 Chapter 4_Sorting Algorithm
106/114
Data structures and algorithms
2 4 5 6 8 12 151
2 3 4 5 6 7 81
p
11/26/2014 106
Quicksort - Code for Algorithms
8/10/2019 Chapter 4_Sorting Algorithm
107/114
Data structures and algorithms
g
template
voidQuickSort (Item a[], intleft, intright)
{
inti, j, x;
if (left right) return;x = a[(left+right) / 2];
i = left; j = right;
while (i < j)
{
while (a[i] < x) i++;
while (a[j] > x) j--;
if (i
8/10/2019 Chapter 4_Sorting Algorithm
108/114
Data structures and algorithms
Worst Case: (assume that we are selecting the mid element as
pivot) The pivot divides the list of size n into two sublists of sizes 0 and n-1.
The number of key comparisons
= n-1 + n-2 + ... + 1
= n2/2n/2 O(n2)
The number of swaps == n-1 + n-1 + n-2 + ... + 1
swaps outside of the for loop swaps inside of the for loop
= n2/2 + n/2 - 1 O(n2)
So, Quicksort is O(n2) in worst case
11/26/2014 108
QuicksortAnalysis (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
109/114
Data structures and algorithms
Quicksort is O(n*log2n) in the best case and average
case.
Quicksort is slow when the array is sorted and we
choose the first element as the pivot.
Although the worst case behavior is not so good, itsaverage case behavior is much better than its worst
case.
So, Quicksort is one of best sorting algorithms using key
comparisons.
11/26/2014 109
Question
8/10/2019 Chapter 4_Sorting Algorithm
110/114
Data structures and algorithms11/26/2014 110
QUESTIONS AND EXERCISES
8/10/2019 Chapter 4_Sorting Algorithm
111/114
Data structures and algorithms11/26/2014 111
1. Write a method:
public static bool isSorted( int[] array, int n)
that returns true if array is sorted; otherwise it returns false.
2. Write a recursive bubble sort on a double array.
3. Write a recursive insertion sort on a double array.
4. Many operations can be performed faster on sorted than on
unsorted data. For which of the following operations is this the case?
(a) Finding an item with minimum value.(b) Computing an average of values.
(c) Finding the middle value (the median).
(d) Finding the value that appears most frequently in the data.
(e) Finding the distinct elements of an array.
(f) Finding a value closest to a given value.(g) Finding whether one word is an anagram(i.e., it contains the same
letters) as another word.
(example:plum and lump)
QUESTIONS AND EXERCISES (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
112/114
Data structures and algorithms11/26/2014 112
5. Class Student have 2 property name andstudentNo. Rewrite some of the sorting methods we
have studied such that each sorts an Object array of
Student objects by name or studentNo.
6. Show the contents of the following integer array:
43, 7, 10, 23, 18, 4, 19, 5, 66, 14when the array is sorted in ascending order using:
(a) bubble sort, (b) selection sort, (c) insertion sort
(d) Interchange sort, (e) heap sort, (f) merge sort, (g)
quicksort
QUESTIONS AND EXERCISES (cont.)
8/10/2019 Chapter 4_Sorting Algorithm
113/114
Data structures and algorithms11/26/2014 113
7. In our implementation of bubble sort, an array was
scanned bottom-up to bubble up the smallest element.What modifications are needed to make it work top-
down to bubble down the largest element?
8. A cocktail shaker sort is a modification of bubble sort
in which the direction of bubbling changes in eachiteration: In one iteration, the smallest element is
bubbled up; in the next, the largest is bubbled down; in
the next the second smallest is bubbled up; and so
forth. Implement this algorithm.
9. Explain why insertion sort works well on partially
sorted arrays.
Thank you !
8/10/2019 Chapter 4_Sorting Algorithm
114/114