Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Chapter 2 - Elementary Searching and Sorting Algorithms

The document covers elementary searching and sorting algorithms including sequential search, binary search, simple sort, bubble sort, selection sort, and insertion sort. It explains the algorithms, provides pseudocode for their implementations, and analyzes their time complexities. All of the sorting algorithms have a worst-case time complexity of O(n2) as they require comparing all pairs of elements in the worst case. Binary search has a better time complexity of O(logn) as it halves the search space on each iteration.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Chapter 2 - Elementary Searching and Sorting Algorithms

The document covers elementary searching and sorting algorithms including sequential search, binary search, simple sort, bubble sort, selection sort, and insertion sort. It explains the algorithms, provides pseudocode for their implementations, and analyzes their time complexities. All of the sorting algorithms have a worst-case time complexity of O(n2) as they require comparing all pairs of elements in the worst case. Binary search has a better time complexity of O(logn) as it halves the search space on each iteration.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Data Structure and Algorithms

Instructor: Abdulkerim S. Endris [M.Sc.]


kerimacademy@gmail.com
Wollo University
Chapter Two: Elementary Searching
and Sorting Algorithms
This Chapter Covers:
Sequential search
Binary search
Simple sort
Bubble sort
Selection sort
Insertion sort
Why do we study sorting and searching
algorithms?
they are the most common & useful tasks in any
software development
they take more than 25% of running time of
computers task
Example:
Searching documents over the internet
Searching files and folders in a hard disk
Sorting students by their name, year and so on
Sorting file, search results by file name, date created and so
on
Deleting and recording data from a database
Simple searching Algorithm
Searching is a process of finding an element from a
collection of elements.
Searching algorithms to be considered:
Sequential search
Binary search
Sequential Searching
It is natural way of searching
The algorithm does not assume that the data is sorted.
Algorithm:
Search list from the beginning until key is found or end of list
reached.
Implementation:
int i;
bool found =false;
for(i=0;i<n;i++;)
if (DataElement[i]==key)
{
found=true;
break;
}
return (found);
Example

List index 0 1 2 3 4 5 6 7
Data C A E F N Q R K
Key=Q after six comparison the algorithm returns found
Key=X after 8 comparison the algorithm returns found false

What is the Big O of sequential searching algorithm?


For searching algorithm the dominant factor is comparison of
keys
How many comparisons are made in sequential searching?
The maximum number of comparison is n, i.e. O (n).
Binary Searching
Algorithm:
1. Divide the list into halves
2. see which half of the list the key belongs
3. repeat 1 and 2 until only one element remains
4. if the remaining element is equal to search key then
found =true else key not found.
Implementation:
int Top =n-1, Bottom=0;middle;
bool found=false;
while (Top>bottom)
{
Middle=(Top+Bottom)/2;
if( dataElement[middle]<key)
Bottom=middle+1;
else
Top=middle;
}
if (dataElement[top]==key)
found =true;
return found;
How many comparisons are made in
binary search algorithm?
n Max. Number of comparison
1 1
2 2
4 3
8 4
16 5
32 6
Then for n number of data items, logn+1 number of
comparison is needed; so, it is O(logn).
Simple (Elementary) Sorting
Algorithms

For searching, sorting is important


Take as an example a telephone directory
Efficiency of the algorithm should be assessed
Sorting algorithms commonly consist of two types of
operation: comparisons and data movements
A comparison is simply comparing one value in a list
with another, and a data movement (swapping) is an
assignment.
Sorting algorithms to be considered:
Simple sort, bubble sort, selection sort, insertion sort
Simple Sort Algorithm
In simple sort algorithm,
the first element is compared with the second, third, and all
subsequent elements.

If any of these other is less than the current first element
then the first element is swapped with that element.

The above step is repeated with the second, third and all
other subsequent elements.
Implementation:
void swap( dataType x, dataType y)
{
dataType temp;
temp=x;
x=y;
y=temp;
}
for (i=0;i<=n-2;i++)
for(j=i+1;j<=n-1;j++)
if(dataElement[i]>dataElement[j])
Swap(dataElement[i],dtataElement[j]);
Example

i j (pass) Data elements Remarks


0 1 2,4,3,1 The smallest element
2 2,4,3,1 is placed in its
proper position
3 1,4,3,2
1 2 1,3,4,2 The second smallest
3 1,2,4,3 element is placed
in its proper place.
2 3 1,2,3,4 All the data elements
are sorted at the n-
1 path
Algorithm Analysis
Analysis involves number of comparisons and
number of swaps.
Iteration Number of comparison number of swaps (compares)
1st pass n-1 n-1
2nd pass n-2 n-2
. . .
. . .
. . .
(n-1)th 1 1
Total n(n-1)/2 n(n-1)/2
Therefore Big O of simple sort algorithm is
n(n-1)/2+n(n-1)/2=O(n2)
Bubble Sort Algorithm
It a method which uses the interchanging of adjacent pair
of element in the array.
After each pass through the data one element (the largest or
smallest element) will be moved down to the end of the
array in its proper place.

Implementation:
for (int i = 0; i < n-1; i++)
for (int j = n-1; j > i; --j)
if (data[j] < data[j-1])
swap(data[j],data[j-1]);
Example data element= 5,2,3,8,1
Analysis
Passes number of comparison number of swaps
1 n-1 n-1
2 n-2 n-2
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n(n-1)/2
=O(n2)
Selection Sort Algorithm
It is in many ways similar to both simple and bubble
algorithm.
Rather than swapping the neighbors continuously as the
algorithm traverses the sub array to be sorted, as done in
the bubble sort case, this algorithm finds the minimum
element of the sub array and swaps it with the pivot
elements.
Implementation

for (i=0; i<=n-2;i++)


{
min_index=i;
for (j=(i+1); j<=n-1;j++)
if (dataElement[j] <= dataElement[min_index])
min_index=j;
swap (dataElement[i],dataElement[min_index]);
}
Example
Analysis:
Pass number of comparison number of swaps
1 n-1 1
2 n-2 1
. . .
. . .
. . .
n-1 1 1
total n(n-1)/2 n-1
It is in O(n2)
Insertion Sort Algorithm
As each element in the list is examined it is put into
its proper place among the elements that have been
examined ( and put in correct order).

When the last element is put into its proper position


the list is sorted and the algorithm is done.

It behaves in the worst case like the inefficient


bubble sort and selection sort algorithms.

But in many average case it performs better.


Implementation
for (i=1;i<=n-1;i++)
{
inserted =false;
j=i;
while ((j>=1) && (inserted == false))
{
If (dataElement[j]<dataElement[j-1])
Swap(dataElement[j],dataElement[j-1]);
else
inserted=true;
j--
}
}
Analysis
Pass number of comparison number of swaps
1 1 1
2 2 2
. . .
. . .
. . .
n-1 n-1 n-1
Total n(n-1)/2 n(n-1)/2
It is in O(n2)
Assignment - II
Write a C++ program that will implement all of the
sorting algorithms. The program should accept
different unsorted data items from user and sort
using all algorithms and should tell which algorithm
is efficient for that particular unsorted data items.
(Hint use how many comparison and swaps are done
so the one with the less number of swap and
comparison number is considered as an efficient
algorithm for that particular data items.)

Deadline: 05/04/2023

You might also like