Lecture 10 (Sorting)
Lecture 10 (Sorting)
ةد ا م يف تارضاحم:
Lecture # 10
Sorting
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Sorting 220
Outline
:Sorting •
.Introduction
.Selection Sort
.Insertion Sort
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Sorting 221
Sorting
• Sorting is the process of rearranging a given set of objects in a
specific order (ascending or descending).
Some of them are good for sorting small sets of data, while
others are good for sorting large sets of data.
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Sorting 222
Sorting (cont…)
• In this article, you'll learn about:
Bubble Sort.
Selection Sort.
Insertion Sort.
Merge Sort.
Quick Sort.
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Sorting 223
Sorting (cont…)
• The goal is to come up with better and more efficient sorts.
1) Space/storage requirements:
Additional/temporary/intermediate arrays required?
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort 224
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 225
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 226
1 2 3 4 5 6
:Original 77 42 35 12 101 5
1 2 3 4 5 6
77Sw ap4
42 77 35 12 101 5
2
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 227
1 2 3 4 5 6
Sw 77 ap3
42 35 77 12 101 5
5
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 228
1 2 3 4 5 6
Sw 77 ap1
42 35 12 77 101 5
2
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 229
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 230
1 2 3 4 5 6
ap 5
42 35 12 77 5 Sw 101
101
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 231
1 2 3 4 5 6
42 35 12 77 5 101
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 232
Original: 25 57 48 37 12 92 86 33
Pass 1: 25 57 48 37 12 92 86 33
33 86 92 12 37 57 48 25
33 86 92 12 57 37 48 25
33 86 92 57 12 37 48 25
33 92 86 57 12 37 48 25
92 33 86 57 12 37 48 25
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 233
Pass 2: 25 48 37 12 57 86 33 92
92 33 86 57 12 48 37 25
92 33 86 57 48 12 37 25
92 86 33 57 48 12 37 25
Pass 3: 25 37 12 48 57 33 86 92
92 86 33 57 48 37 12 25
92 86 57 33 48 37 12 25
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort example 234
Pass 5: 12 25 37 33 48 57 86 92
92 86 57 48 37 33 25 12
Pass 6: 12 25 33 37 48 57 86 92
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Bubble Sort code 235
void
BubbleSort(int
a[])
{
for (int i =
0; i < size;
i++)
{
for (int j
= 1; j <
size - i;
j++)
{
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Bubble Sort runtime 236
1 (n i)
i 0 j 1 i 0
n 1
i
i 0
( n 21 ) n
O(n2 )
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Bubble Sort 238
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Selection Sort 239
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Selection Sort example 240
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Selection Sort example 241
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Selection Sort example 242
Initial: 33 86 92 12 37
9248 57 25
:Pass 1 I 92 86 33 12 37 48
8657 25
:Pass 2 I 86 92 33 12 37 48 57 25
57
:Pass 3 I 5748
86 92 12 37 48 33 25
:Pass 4 I 48 5737
86 92 37 12 33 25
:Pass 5 I33
37 48 57 86 92 12 33 25
:Pass 6 25 I 33 37 48 57 86 92 12 25
:Pass 7 I 25 33 37 48 57 86 92 12
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Selection Sort code 243
1 ( n (i 1) 1)
i0 j i1 i0
n 1
(n i)
i0
n1
i
i0
( n 21 ) n
O(n2 )
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Selection Sort runtime 245
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Selection Sort example 246
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Insertion Sort example 249
Original: 2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
Pass 1: 2 15 8 1 17 10 12 5
0 1 2 3 4 5 6 7
Pass 2: 2 8 15 1 17 10 12 5
0 1 2 3 4 5 6 7
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Insertion Sort example 250
Pass 3: 1 2 8 15 17 10 12 5
0 1 2 3 4 5 6 7
Pass 4: 1 2 8 15 17 10 12 5
0 1 2 3 4 5 6 7
Pass 5: 1 2 8 10 15 17 12 5
0 1 2 3 4 5 6 7
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Insertion Sort example 251
Pass 6: 1 2 8 10 12 15 17 5
0 1 2 3 4 5 6 7
Pass 7: 1 2 5 8 10 12 15 17
0 1 2 3 4 5 6 7
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Insertion Sort example 252
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Insertion Sort example 253
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Insertion Sort code 254
;a[j] = temp
}
}
.Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept Algorithms and Data Structures: Lecture (10)
Insertion Sort runtime 255
i 1 2
O(n2 )
1 n 1 O (n )
i1
i 1 2 2 4
O(n2 )
Dr. Asim Abdallah Elshiekh, UofK, Fac. of Math. Sc., Computer Dept. Algorithms and Data Structures: Lecture (10)
Insertion Sort 256
• Number of exchanges:
Best case: O(n).
Worst case: O(n2).