Professional Documents
Culture Documents
Chapter 3
Chapter 3
Chapter 3
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.
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)
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
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}
There are many, many different types of sorting algorithms, but the primary ones are:
Basic Idea:
–Loop through array from i=0 to n and swap adjacent elements
if they are out of order.
Pseudo code
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
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
Original array:
6354927
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.
4 7 6 5 2
7 4 6 5 2
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
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
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
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. ji
4. while (j >0) and (A[j] > x)
5. A[j + 1] A[j]
6. jj-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
of two parts: the first part a0, ..., ai-1 is already sorted, the second
• The worst case occurs when in every step the proper position
42