Professional Documents
Culture Documents
CSEE2123: OOP and Data Structures: Searching & Sorting Algorithms
CSEE2123: OOP and Data Structures: Searching & Sorting Algorithms
Fall 2018
Searching & Sorting Algorithms
Lecture 27
M. Tahir Awan
(mtahir@cust.edu.pk)
Capital University of Science & Technology (CUST),
Islamabad
Advanced Data Structures: Algorithms
• Algorithm is a step-by-step procedure, in order
to get the desired output from data structures
• Algorithms are used to manipulate data in data
structures
• Algorithms used in Data Structures
–Searching
–Sorting
–Traversal
–Hashing
–Recursion
Class Big-Oh
constant O(1)
logarithmic O(log2 N)
linear O(N)
log-linear O(N log2 N)
quadratic O(N2)
cubic O(N3)
... ...
exponential O(2N)
index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103
return -1;
}
• Sorting Algorithms
–Bubble Sort
–Selection Sort
–Insertion Sort
–Merge Sort
–Quick Sort
12
Bubble Sort
• In Bubble sort, each pair of adjacent elements is
compared and are swapped if not in order
• “Bubble Up” algorithm will move largest value to
its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed in order
• In each pass of bubble sort, one element is
sorted
• Bubble sort has Maximum of N-1 passes and can
finish early if no swapping occurs
• Bubble sort is a simple but slow algorithm
Bubble Sort : Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value 18 12 -4 22 27 30 36 50 68 7 91
index 0 1 2 3 4 5 6 7 8 9 10
value 12 -4 18 22 27 30 36 50 7 68 91
index 0 1 2 3 4 5 6 7 8 9 10
value -4 12 18 22 27 30 36 7 50 68 91
Bubble Sort : Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value -4 12 18 22 27 7 30 36 50 68 91
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 27 30 36 50 68 91
Bubble Sort : Example 2
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
N – 1 Passes
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort : Pseudo Code
• procedure BubbleSort ( List : array of items )
for i = 0 to Loop-1 do:
for j = 0 to Loop-1-i do:
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 22 27 30 36 50 91 68 18
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 22 27 30 36 50 91 68 18
Selection Sort : Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 30 36 50 91 68 27
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 27 30 36 50 68 91
Selection Sort : Pseudo Code
• procedure Selection sort ( List : array of items )
for i = 0 to N – 1
minIndex = i
for j = i+1 to n
if List[j] < List[minIndex] then
minIndex = j;
end if
end for
if minIndex != i then
swap List[minIndex] and List[i]
end if
end for
end procedure
12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 26
Selection Sort Implementation
• Two methods of insertion sort will be discussed
–Selection Sort using Arrays
–Selection Sort using Linked List
index 0 1 2 3 4 5 6 7 8 9 10
value 18 22 12 -4 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value 12 18 22 -4 27 30 36 50 91 68 7
Insertion Sort : Array Example
• Initial array:
index 0 1 2 3 4 5 6 7 8 9 10
value 22 18 12 -4 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value -4 12 18 22 27 30 36 50 91 68 7
index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 22 27 30 36 50 68 91
Insertion Sort : Pseudo Code
• procedure Insertion sort ( List : array of items )
for i = 0 to N-1 do:
valueToInsert = List [i]
insertIndex = i
while insertIndex > 0 and List [ insertIndex-1] >
valueToInsert do:
List [ insertIndex ] = List [ insertIndex-1]
insertIndex = insertIndex-1
end while
List [ insertIndex] = valueToInsert
end for
end procedure
12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 39
Array based List : Class
• A C++ Class to class ABList{
private:
implement Array int max_size;
based List int Length;
int* List;
• Function
InsertionSort() public:
performs Insertion ABList(int);
~ABList();
Sort bool isEmpty();
int getLength();
int getItem(int);
void insertItem(int value);
void removeItem(int);
void InsertionSort();
void displayList();
void clearList();
};