Chapter 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Chapter Three

Simple Searching and Sorting Algorithms

This Chapter covers:


Sequential Search
Binary Search
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. 2


Simple Searching Algorithms
Searching is a process of checking and finding an element from
a list of elements or determining that the item is not in the list.
Let “A” be a collection of data elements, i.e., A is a linear array
of say n elements. If we want to find the presence of an element
“data” in A, then we have to search for it. The search is
successful if data does appear in A and unsuccessful if
otherwise.
There are several types of searching techniques; one has some
advantage(s) over other. Following are the two important
simple searching techniques:
I. Linear/Sequential Searching
II. Binary Searching
3
Linear or Sequential Searching
In linear search, each element of an array is read one by one
sequentially and it is compared with the desired element.
A search will be unsuccessful if all the elements are read and
the desired element is not found.

Algorithm for Linear Search


• Let A be an array of n elements, A[1],A[2],A[3], ...... A[n].
“data” is the element to be searched.
• Then this algorithm will find the location “loc” of data in A.
Set loc = – 1, if the search is unsuccessful.

4
Cont…
1. Input an array A of n elements and “data” to be searched
and initialize loc = – 1.
2. Initialize i = 0; and repeat through step 3
if (i <n) by incrementing i by one.
3. If (data = A[i])
(a) loc = i
(b) GOTO step 4
4. If (loc > 0)
(a) Display “data is found and searching is successful”
5. Else
(a) Display “data is not found and searching is unsuccessful”
6. Exit 5
Example
Data 10 5 8 11 34 16 20 25
List 0 1 2 3 4 5 6 7
index(loc)

Key=16 after six comparison the algorithm returns “found”.


Key=50 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).6
C++ code
#include <iostream>
int LinearSearch(int list[ ], int key);
int LinearSearch(int list[ ], int key)
using namespace std; {
int n=7;
int main()
int index=-1;
{ for(int i=0; i < n; i++)
int list[n], int k;
{
cout<<"enter the key you want to search from the array";
cin>>k; if(list[i]==key)
cout<<"enter the array list";
for(int j=0;j<n;j++)
{
{ index=i;
cin>>list[j];
}
}
int i = LinearSearch(list, k); }
if(i==-1) cout << "the search item is not found" << endl;
else cout << "The value is found at index position " <<
return index;
i << endl; }
return 0;
}

7
Binary Search
Binary search is an extremely efficient algorithm when it
is compared to linear search.
Binary search technique searches “data” in minimum
possible comparisons.
Suppose the given array is a sorted one, otherwise first
we have to sort the array elements.
Then apply the following conditions to search a “data”.

8
Cont…
1. Find the middle element of the array (i.e., n/2 is the middle
element if the array or the sub-array contains n elements).
2. Compare the middle element with the data to be searched, then
there are following three cases.
(a) If the middle element is a desired element, then search is
successful.
(b) If mid is greater than desired data, then search only the first
half of the array, i.e., the elements which come to the left side of
the middle element.
(c) If mid is less than the desired data, then search only the
second half of the array, i.e., the elements which come to the
right side of the middle element.
Repeat the same steps until an element are found or exhaust the
search area.
9
Algorithm for binary search
Let A be an array of n elements A[1],A[2],A[3],...... A[n]. “Data” is
an element to be searched.
“mid” denotes the middle location of a segment (or array or sub-
array) of the element of A.
 LB and UB is the lower and upper bound of the array which is
under consideration.

10
Cont…
1.Input an array A of n elements and “data” to be searched
2.LB = 0, UB = n-1; mid = int ((LB+UB)/2)
3.Repeat step 4 and 5 while (LB <= UB) and (A[mid] ! = data)
4.If (data < A[mid])
(a) UB = mid–1
5.Else
(a) LB = mid + 1
6.Mid = int ((LB + UB)/2)
7.If (A[mid]== data)
(a) Display “the data found”
8.Else
(a) Display “the data is not found”
9.Exit
11
C++ code for Binary search Algorithm
int BinarySearch(int list[ ], int key)
{ #include <iostream>
int found=0,index=0; int BinarySearch(int list[ ], int key);
int n=5;
int top=n-1,bottom=0,middle;
using namespace std;
do int main()
{ {
middle=(top + bottom)/2; int list[n];
if(key==list[middle]) int k;
found=1; cout<<"enter the key you want to search from
else the array";
{ cin>>k;
cout<<"enter the array list";
if(key < list[middle])
for(int j=0;j<n;j++)
top=middle-1;
{
else cin>>list[j];
bottom=middle+1; }
} int i = BinarySearch(list, k);
}while(found==0 && top>=bottom); if(i==-1)
if(found==0) cout << "the search item is not found" << endl
index=-1; else
else cout << "The value is found at index position " << i << end
index=middle; return 0;
}
return index;
}
12
Example
Suppose we have an array of 7 elements

Following steps are generated if we binary search a data = 45 from


the above array.
Step 1:

LB = 0; UB = 6
mid = (0 + 6)/2 = 3
A[mid] = A[3] = 30
13
Cont…
Step 2:
Since (A[3] < data) - i.e., 30 < 45 - reinitialize the variable LB,
UB and mid

LB = 4 UB = 6
mid = (4 + 6)/2 = 5
A[mid] = A[5] = 45
Step 4:
Since (A[5] == data) - i.e., 45 == 45 - searching is successful.

14
C++ code int BinarySearch(int list[ ], int key)
{
#include <iostream>
int found=0,index=0;
int BinarySearch(int list[ ], int key);
int n=5;
int top=n-1,bottom=0,middle;
using namespace std; do{
int main() middle=(top + bottom)/2;
{ if(key==list[middle])
int list[n]; found=1;
int k; else {
cout<<"enter the key you want to search from the array"; if(key < list[middle])
cin>>k; top=middle-1;
cout<<"enter the array list";
else
for(int j=0;j<n;j++)
{
bottom=middle+1;
cin>>list[j]; }
} }
int i = BinarySearch(list, k); while(found==0 && top>=bottom);
if(i==-1) if(found==0)
cout << "the search item is not found" index=-1;
<< endl; else
else
index=middle;
cout << "The value is found at index position " << i << endl;
return index;
return 0; }
}
15
What is Sorting?

Sorting: is a process of reordering a list of items in either
increasing or decreasing order.

A= {3162134590}

A= {0112334569}

Why Sort and Examples


• Sorting Books in Library
• Sorting Individuals by Height
• Sorting Movies in Best-seller
• Sorting Numbers (Sequential)
Types of Sorting Algorithms

There are many, many different types of sorting algorithms, but the primary ones are:

1. Bubble Sort 4. Merge Sort 7. Quick Sort


2. Selection Sort 5. Shell Sort 8. Radix Sort
3. Insertion Sort 6. Heap Sort 9. Swap Sort
Bubble Sort
Bubble sort is the simplest algorithm to implement
and the slowest algorithm on very large inputs.
In bubble sort, each element is compared with its
adjacent element.
If the first element is larger than the second one, then
the positions of the elements are interchanged,
otherwise it is not changed.
Then next element is compared with its adjacent
element and the same process is repeated for all the
elements in the array until we get a sorted array.
18
Bubble Sort

• The simplest algorithm to implement


• The slowest algorithm on very large inputs.

Basic Idea:
–Loop through array from i=0 to n and swap adjacent elements
if they are out of order.
Pseudo code

Set flag = false


1. Traverse the array and compare pairs of two
elements
1.1 If E1  E2
OK
1.2 If E1 > E2 then
Switch(E1, E2) and set flag = true
2. If flag = true goto 1.
Implementation
void bubble_sort(list[])
{
int i,j,temp;
for(i=0;i<n; i++){
for(j=0; j<n-1; j++){
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j +1];
list[j +1]=temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort
Example-Bubble Sort

Original array:

1 23 2 56 9 8 10 100

1 1 23 2 56 9 8 10 100
2 1 23 2 56 9 8 10 100
3 1 2 23 56 9 8 10 100
4 1 2 23 56 9 8 10 100
5 1 2 23 9 56 8 10 100
6 1 2 23 9 8 56 10 100
7 1 2 23 9 8 10 56 100
1 2 23 9 8 10 56 100

---- finish the first traversal ----


---- start again ----
8 1 2 23 9 8 10 56 100
9 1 2 23 9 8 10 56 100
10 1 2 23 9 8 10 56 100
11 1 2 9 23 8 10 56 100
12 1 2 9 8 23 10 56 100
13 1 2 9 8 10 23 56 100
14 1 2 9 8 10 23 56 100

1 2 9 8 10 23 56 100
How many comparisons?
---- finish the second traversal ----
----
………………….
start again ----
T(n)= O(n2)
Selection
Selection sort algorithm finds the smallest element of the array
and interchanges it with the element in the first position of the
array.
Then it finds the second smallest element from the remaining
elements in the array and places it in the second position of the
array and so on.
Example: Sort the array A[5]= {5,2,3,8,1} in ascending order using
selection sort.

23
Selection Sort

How does it work:


–first find the smallest in the array and exchange it
with the element in the first position,
–then find the second smallest element and exchange
it with the element in the second position, and
–continue in this way until the entire array is sorted.
Cont’d

• Selection sort is:


–The simplest sorting techniques.
–a good algorithm to sort a small number of elements
–an incremental algorithm – induction method
• Selection sort is Inefficient for large lists.

Incremental algorithms  process the input elements one-by-


one and maintain the solution for the elements processed so far.
Selection Sort Algorithm
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in non decreasing order.
1. for i  0 to n - 1
2. ki
3. for j  i + 1 to n {Find the i th smallest element.}
4. if A[j] < A[k] then k  j
5. end for
6. if k  i then interchange A[i] and A[k]
7. end for
Implementation
void selection_sort(int list[]){
int i,j, smallest;
for(i=0;i<n;i++){
smallest=i;
for(j=i+1;j<n;j++){
if(list[j]<list[smallest])
smallest=j;
}//end of inner loop
if (smallest!=i){
temp=list[smallest];
list[smallest]=list[i];
list[i]=temp;}
} //end of outer loop
}//end of selection_sort
Example

Original array:
6354927

1. 1st pass -> 2 3 5 4 9 6 7 (2 and 6 were swapped)

2. 2nd pass -> 2 3 4 5 9 6 7 (4 and 5 were swapped)

3. 3rd pass -> 2 3 4 5 6 9 7 (6 and 9 were swapped)

4. 4th pass -> 2 3 4 5 6 7 9 (7 and 9 were swapped)

5. 5th pass -> 2 3 4 5 6 7 9 (no swap)

6. 6th pass -> 2 3 4 5 6 7 9 (no swap)


Analysis of Algorithms
• Algorithm analysis: quantify the performance of the algorithm,
i.e., the amount of time and space taken in terms of n.
• T(n) is the total number of accesses made from the beginning of
selection sort until the end.

T(n) =O(n2)
Insertion Sort
 The insertion sort works just like its name suggests - it
inserts each item into its proper place in the final list.
 It's the most instinctive(natural) type of sorting
algorithm.
The process involved in insertion sort is as follows:
 The left most value can be said to be sorted relative to
itself.
 Check to see if the second value is smaller than the
first one. If it is, swap these two values.
 Next, we need to insert the third value in to the
relatively sorted portion so that after insertion, the
portion will still be relatively sorted.
 Do the same for the remaining items in the list.
30
Insertion Sort
• Insertion sort keeps making the left side of the array sorted until
the whole array is sorted.
• A[i] is inserted in its proper position in the ith iteration in the sorted subarray
A[0 .. i-1]
• In the ith step, the elements from index i-1 down to 0 are scanned, each time
comparing A[i] with the element at the correct position.
• In each iteration an element is shifted one position up to a higher index.

• The process of comparison and shifting continues until:

– Either an element ≤ A[i] is found or

– When all the sorted sequence so far is scanned.

• Then A[i] is inserted in its proper position.


Example: sorting numbered cards

4 7 6 5 2

• Given some numbered cards.


• Our aim is to put them in increasing order.
Insertion sort working principle

7 4 6 5 2

1st time one


item is sorted

7 4 6 5 2

7 6 5 2 4

7 6 5 2 4

4 7 6 5 2
Insertion sort working principle

4 7 6 5 2

left one item is


sorted

4 7 6 5 2

4 7 5 2 6

4 7 5 2 6

4 6 7 5 2
Insertion sort working principle

4 6 7 5 2

left one item is


sorted

4 6 7 5 2

4 6 7 2 5

4 6 7 2 5

4 6 7 2 5

4 5 6 7 2

4 5 6 7 2
Insertion sort working principle

4 5 6 7 2

left one item is


sorted
4 5 6 7 2

4 5 6 7 2
4 5 6 7 2
4 5 6 7 2

4 5 6 7 2

4 5 6 7 2

2
4 4 5 6 7 All item are
sorted
Algorithm: INSERTION SORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in non decreasing order.
1. for i  1 to n
2. x  A[i]
3. ji
4. while (j >0) and (A[j] > x)
5. A[j + 1]  A[j]
6. jj-1
7. end while
8. A[j + 1]  x
9. end for
Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++){
temp=list[i];
for(int j=i; j>0 && temp<list[j-1];j--)
{ // work backwards through the array finding where temp
should go
list[j]=list[j-1];
list[j-1]=temp;
} //end of inner loop
} //end of outer loop
} //end of insertion_sort
Example 2:
An insertion sort of an array of five integers
Analysis

• Let a0, ..., an-1 be the sequence to be sorted. At the beginning

and after each iteration of the algorithm the sequence consists

of two parts: the first part a0, ..., ai-1 is already sorted, the second

part ai, ..., an-1 is still unsorted (i in 0, ..., n).

• The worst case occurs when in every step the proper position

for the element that is inserted is found at the beginning of the

sorted part of the sequence.


Cont’d

• Empirically it’s known that Insertion sort is over twice as


fast as the bubble sort and is just as easy to implement as
the selection sort.
• In short, there really isn't any reason to use the selection
sort - use the insertion sort instead.
Question?

42

You might also like