You are on page 1of 42

CSEE2123: OOP and Data Structures

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

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 2


Algorithm Complexity :
Big O Notation
• Common asymptotic notations

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)

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 3


Searching Algorithms
• Searching Algorithms :
–Sequential Search
–Binary Search

• Search algorithms can be used to :


– Determine whether a particular item is in the
list or not
– Find and remove the item from the list
– Insert an item in the list if not already present

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 4


Sequential Search : Example
• Search 33 in the array
• Key = 33

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 5


Sequential Search : Performance
Analysis
• To analyzing a search algorithm for an array of
size N, count the number of key comparisons

• Worst Case : If the search item is not in the list,


we make N comparisons

• Best Case : if search item is the first item in the


list, we make 1 comparison

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 6


Binary Search : Example
• Search 31 in the array
• Key = 42
• First = 0 , Last = 16

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

First Mid Last

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 7


Array based List : Binary Search
• Binary Search an int ABList::BinarySearch (int Key)
{
item in the list and int first = 0;
return its position int last = Length - 1;
(index) if found int mid;

• If item is not found , while (first <= last) {


return -1 mid = (first + last) / 2;
if (list[mid] == Key )
return mid;
else if (list[mid] > Key )
last = mid - 1;
else
first = mid + 1;
}

return -1;
}

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 8


Binary Search : Performance Analysis
• Every iteration of Binary Search Loop cuts size
of array by half and makes two comparisons
• Maximum Iterations = m + 1 , where

• Best Case : Item to search is middle item of the


List
• Worst Case : Maximum Comparisons

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 9


Searching : Practice Questions
• Implement Sequential and Binary search
algorithms for array based lists and linked lists
• Write a program to find the number of
comparisons using the binary search and
sequential search algorithms
• Modify Sequential Search for sorted arrays such
that when Key to search is larger than the
number in the array, terminate the search
process and display „Number not found‟.

CSEE2123:OOP & Data Structures © M. Tahir Awan, CUST 10


Sorting Algorithms
Sorting Algorithms
• Sorting is arranging data in a specific order i.e.
ascending or descending order
• Sorting can be used to make searching efficient
i.e. binary search

• 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

• After 1st, 2nd, and 3rd passes:

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

• After 4th , 5th , and (N-1) Passes:


index 0 1 2 3 4 5 6 7 8 9 10
value -4 12 18 22 27 30 7 36 50 68 91

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:

if List[j] > List[j+1] then


swap( List[j], List[j+1] ) // Swap Values
end if
end for
end for
end procedure
return list
12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 17
Array based List : Class
• A C++ Class to class ABList{
private:
implement Array int max_size;
based List int Length;
int* List;
• Function BubbleSort()
performs Bubble public:
Sorting ABList(int);
~ABList();
bool isEmpty();
int getLength();
int getItem(int);
void insertItem(int value);
void removeItem(int);
void BubbleSort();
void displayList();
void clearList();
};

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 18


Array based List : Bubble Sort
• Nested Loops are void ABList:: BubbleSort ()
required to {
int i, j, swap;
implement Bubble for (i=0; i < (Length-1 ); i++)
sort. {
for (j=0; j<Length-i-1; j++)
• Each iteration of {
outer-loop
completes one pass if (List[j] > List [j+1])
{
of Bubble sort swap = List [j];
• In N-1 passes, List is List [j] = List [j+1];
List [j+1] = swap;
sorted }
}
}

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 19


Bubble Sorting : Performance
Analysis
• To analyzing the bubble sort for an array of size
N, count the number of key comparisons

• Best Case : if data is already in order, no


swapping occurs in one pass of bubble sort

• Worst Case : In one pass of Bubble sort, one


item is sorted. Hence N-1 passes required

• Bubble sort is the slowest sorting algorithm


12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 20
Practice Question
• Implement Bubble Sort in descending order on
an array based list
• Use Bubble sort to sort names of 10 students in
alphabetical order in array based list

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 21


Selection Sort
Selection Sort
• Selection sort is simple and in-place sorting
algorithm
• In selection sort, list is divided into two parts:
sorted list , unsorted list. Initially sorted list is
empty and unsorted list is the entire list.
• Smallest element in the unsorted list is selected
and swapped with the first element. It becomes
first element of the sorted list
• In each pass, sorted list grows and unsorted list
shrinks by 1
• In N-1 passes, all of the list is sorted and
unsorted list becomes empty
• The algorithm:
12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 23
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

• After 1st, 2nd, and 3rd passes:


index 0 1 2 3 4 5 6 7 8 9 10
value -4 18 12 22 27 30 36 50 91 68 7

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

• After 4th , 5th , and (N-1) Passes:


index 0 1 2 3 4 5 6 7 8 9 10
value -4 7 12 18 27 30 36 50 91 68 22

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

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 27


Array based List : Class
• A C++ Class to class ABList{
private:
implement Array int max_size;
based List int Length;
int* List;
• Function
SelectionSort() public:
performs Selection ABList(int);
~ABList();
Sort bool isEmpty();
int getLength();
int getItem(int);
void insertItem(int value);
void removeItem(int);
void SelectionSort();
void displayList();
void clearList();
};

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 28


Array based List : Selection Sort
• Nested Loops are void ABList :: SelectionSort()
required to {
int i, j, swap, indexMin;
implement Selection for (i = 0; i < Length-1; i++) {
Sort
• Each iteration of indexMin = i;
for (j = i+1;j < Length;j++)
outer-loop {
completes one pass if ( List[j] < List[indexMin])
of Selection sort indexMin = j;
}
• In N-1 passes, List is if ( indexMin != i ) {
sorted swap = List[indexMin];
List[indexMin] = List[i];
List[i] = swap;
}
}
}

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 29


Selection Sort : Performance Analysis
• To analyzing the selection sort for an array of
size N, count the number of key comparisons

• Worst Case : In one pass of Selection sort, one


item is sorted. Hence N-1 passes required

• Selection sort is fast as compared to Bubble sort


on average

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 30


Practice Question
• Implement Selection Sort in descending order on
an array based list
• Implement Selection Sort to sort data in
ascending order in a linked list

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 31


Insertion Sort
Insertion Sort
• Idea of insertion sort is like sorting Playing
Cards in your hand

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 33


Insertion Sort
• Insertion sort is an in-place sorting algorithm
that divides the list into sorted and unsorted
sub-lists
• In each pass one element from unsorted list is
moved and inserted at its proper position in
sorted list
• In each pass sorted sub-list grows by one and
unsorted list is reduced
• In N-1 passes whole list is sorted

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 34


Insertion Sort Implementation
• Two methods of insertion sort will be discussed
–Insertion Sort using Arrays
–Insertion Sort using Linked List

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 35


Insertion Sort using Arrays
• One pass of Insertion Sort

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 36


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

• After 1st, 2nd, and 3rd passes:


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 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

• After 4th , 5th , and (N-1) Passes:


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 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();
};

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 40


Array based List : Insertion Sort
void ABList :: InsertionSort() {
• Nested Loops are int i, j, InsertIndex, Value;
required to for (i = 1; i < Length; i++) {
implement
Value = List[i];
Insertion Sort InsertIndex = i;
• Each iteration of
while (InsertIndex > 0 &&
outer-loop List[InsertIndex -1] > Value) {
completes one List[InsertIndex ] = List[InsertIndex-1];
InsertIndex --;
pass of Insertion }
sort
if (InsertIndex != i) {
List[InsertIndex ] = Value;
}
}
}

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 41


Animation of Sorting Algorithms
• Link below shows animation of different sorting
algorithms
• http://www.ee.ryerson.ca/~courses/coe428/sortin
g/insertionsort.html

12/27/2018 CSEE1133:Data Structures © M. Tahir Awan, CUST 42

You might also like