Data Structure and Algorithms
Instructor: Abduselam Ali
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 halve
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 16
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
is placed in its
proper position
2 2,4,3,1
3 1,4,3,2
1 2 1,3,4,2 The second smallest
element is placed
in its proper place.
3 1,2,4,3
2 3 1,2,3,4 All the data elements
are sorted at the n-
1 path
13
footer
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.)

Chapter 3 - Elementary Searching and Sorting Algorithms.ppt

  • 1.
    Data Structure andAlgorithms Instructor: Abduselam Ali
  • 2.
    Chapter Two: ElementarySearching and Sorting Algorithms This Chapter Covers: Sequential search Binary search Simple sort Bubble sort Selection sort Insertion sort
  • 3.
    Why do westudy 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
  • 4.
    Simple searching Algorithm Searchingis a process of finding an element from a collection of elements. Searching algorithms to be considered: Sequential search Binary search
  • 5.
    Sequential Searching It isnatural 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);
  • 6.
    Example List index 01 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).
  • 7.
    Binary Searching Algorithm: 1. Dividethe list into halve 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.
  • 8.
    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;
  • 9.
    How many comparisonsare made in binary search algorithm? n Max. Number of comparison 1 1 2 2 4 3 8 4 16 5 32 16 Then for n number of data items, logn+1 number of comparison is needed; so, it is O(logn).
  • 10.
    Simple (Elementary) Sorting Algorithms Forsearching, 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
  • 11.
    Simple Sort Algorithm Insimple 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.
  • 12.
    Implementation: void swap( dataTypex, 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]);
  • 13.
    Example i j (pass)Data elements Remarks 0 1 2,4,3,1 The smallest element is placed in its proper position 2 2,4,3,1 3 1,4,3,2 1 2 1,3,4,2 The second smallest element is placed in its proper place. 3 1,2,4,3 2 3 1,2,3,4 All the data elements are sorted at the n- 1 path 13 footer
  • 14.
    Algorithm Analysis Analysis involvesnumber 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 )
  • 15.
    Bubble Sort Algorithm Ita 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]);
  • 16.
  • 17.
    Analysis Passes number ofcomparison 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 )
  • 18.
    Selection Sort Algorithm Itis 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.
  • 19.
    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]); }
  • 20.
  • 21.
    Analysis: Pass number ofcomparison 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 )
  • 22.
    Insertion Sort Algorithm Aseach 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.
  • 23.
    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-- } }
  • 24.
    Analysis Pass number ofcomparison 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 )
  • 25.
    Assignment - II Writea 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.)