Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Data Structure and Algorithm

Chapter Two
Simple Sorting and Searching
Algorithms
Simple Searching Algorithms
• Searching is a process of looking for a specific element in a list of
items or determining that the item is not in the list.

• Many Search Methods Exist choose one that meets time/space


requirements and problem size!

• There are two simple searching algorithms

– Sequential or Linear search: for unsorted small arrays

– Binary search: for sorted large arrays:


Linear (Sequential) Search
• Also called Sequential Search Linear Search is needed if items are
in a random order. Sets of Steps for Linear search

1. start with the first position within the array (index 0)

2. compare target value with value at that position if equal: finish by


returning item's position within array

3. otherwise repeat (loop) until end of array

4. if no item was found in the loop, return \not found",

Example

• Array numlist contains: 17, 23, 5, 11, 2, 29,3 Searching for the

value 11, linear search examines 17, 23, 5, and 11 Searching for

the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3
Linear Search Algorithm
set found to false;
set position to -1;
set index to 0
while index < number of elements and found is false
if list[index] is equal to search value
found = true
position = index
end if
add 1 to index
end while
return position
Implementation of Linear Search Algorithm
Int searchList (int list[], int numElems, int value)
{ int index = 0;
Int position = -1;
// To record position of search value
Bool found = false;
// flag to indicate if value was found
While(index < numElems && !found) {
If( list[index] == value) // if the value is found
{ found = true; // set the flag
Position = index; // record the value’s subscript
} index ++;
// go to the next element
} return position;
// return the position or -1
}
Example that Implements Linear Search
int main()
{
int i, value,found=-1, list[]={65,20,10,55,32,12,50,99};
cout<<"enter the number to be searched\n":
cin>> value;
for(int i=0; i<8; i++)
{
if(value==list[i])
{
found= i+1;
break; }
}
if (found == -1)
{
cout<<"thw element "<<value<<" is not found";
}
else
{
cout<< " the position of "<<value<<"->"<<found;
}}
Efficiency of Linear Search

Advantage
• It is simple.
• It is easy to understand
• Easy to implement Best Case
• Does not require the array to be in item found at the beginning: O(1)
order Worst Case
item found at the end: O(n)
Disadvantage Average Case
• its inefficiency Arithmetic average: n+1/2
• Ineffcient (slow): for array of N
elements, examines N/2 elements on
average for value in array,
• N elements for value not in array
Binary Search

• This searching algorithm works only on an ordered list.


• The basic idea is:
– Locate midpoint of array to search
– Determine if target is in lower half or upper half of an array.
• If in lower half, make this half the array to search
• If in the upper half, make this half the array to search
– Loop back to step 1 until the size of the array to search is one,
and this element does not match, in which case return –1.
• The computational time for this algorithm is proportional to log2 n.
Therefore the time complexity is O(log n)
Binary Search Algorithm
Set first index to 0.
Set last index to the last subscript in the array.
Set found to false.
Set position to -1.
While found is not true and first is less than or equal to last
Set middle to the subscript half-way between array[first] and
array[last].
If array[middle] equals the desired value
Set found to true.
Set position to middle.
Else If array[middle] is greater than the desired value
Set last to middle - 1.
Else Set first to middle + 1.
End If.
End While.
Return position.
Example that Implements Binary Search
int main() {
int i, value,beg, end, mid, found=-1;
int list[]={10,12,20,32,50,55,65,80,99};
cout<<"enter the number to be searched\n";
cin>> value;
beg= 0; end = 9-1;
while(beg<=end){
mid =(beg+end)/2;
if(value== list[mid]){
found =mid + 1;
break; }
else if(value>=list[mid]){
beg=mid+1; }
else
{ end =mid-1; }
}
if ( found == -1){
cout<<"the element "<<value<<" is not found"; }
else
{
cout<< " the position of "<<value<<"->"<<found; }
}
Efficiency of Binary Search

Advantage Best Case


• Much more efficient item found in the middle of the array:
than linear search. O(1)
• For array of N Worst Case
elements, performs at Item found at very end or not at all
most log2N How often can you half the array?
comparisons
• Disadvantage
Average Case
• Requires that array
Average = Worst Case = O(log n)
elements be sorted
Simple Sorting Algorithms

• Sorting is one of the most important operations performed by


computers.

• Sorting is a process of reordering a list of items in either increasing


or decreasing order.

• The following are simple sorting algorithms used to sort small-sized


lists.

– Insertion Sort

– Selection Sort

– Bubble Sort
Insertion Sort
• The insertion sort works just like its name suggests - it inserts each
item into its proper place in the final list.

• The simplest implementation of this requires two list structures -


the source list and the list into which sorted items are inserted.

• To save memory, most implementations use an in-place sort that


works by moving the current item past the already sorted items and
repeatedly swapping it with the preceding item until it is in place.

• It's the most instinctive type of sorting algorithm. The approach is


the same approach that you use for sorting a set of cards in your
hand.
Insertion Sort
Basic Idea:
• Find the location for an element and move all others up, and insert
the element.
The process involved in insertion sort is as follows:
1. The left most value can be said to be sorted relative to itself.
Thus, we don’t need to do anything.
2. Check to see if the second value is smaller than the first one. If it
is, swap these two values. The first two values are now relatively
sorted.
3. 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.
4. Remove the third value first. Slide the second value to make
room for insertion. Insert the value in the appropriate position.
5. Now the first three are relatively sorted.
6. Do the same for the remaining items in the list.
Contd.
Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++) Analysis
{ How many comparisons?
1+2+3+…+(n-1)= O(n2)
temp=list[i];
How many swaps?
for(int j=i; j>0 && temp<list[j-1];j--) 1+2+3+…+(n-1)= O(n2)
{ // work backwards through the array How much space?
// finding where temp should go In-place algorithm
list[j]=list[j-1];
list[j-1]=temp;
}//end of inner loop
}//end of outer loop
}//end of insertion sort
Selection Sort

Basic Idea:
Loop through the array from i=0 to n-1.

Select the smallest element in the array from i to n

Swap this value with value at position i.


Implementation: Contd.
void selection_sort(int list[])
{
int i,j, smallest;
for(i=0;i<n;i++){
smallest=i; Analysis
for(j=i+1;j<n;j++){
How many comparisons?
if(list[j]<list[smallest])
(n-1)+(n-2)+…+1= O(n2)
smallest=j;
How many swaps?
}//end of inner loop
n=O(n)
temp=list[smallest];
How much space?
list[smallest]=list[i];
In-place algorithm
list[i]=temp;
} //end of outer loop
}//end of selection_sort
Bubble Sort

• Bubble sort is the simplest algorithm to implement and 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.
Implementation:
void bubble_sort(list[])
{ Analysis of Bubble Sort
int i,j,temp;
How many comparisons?
for(i=0;i<n; i++){
(n-1)+(n-2)+…+1= O(n2)
for(j=n-1;j>i; j--){
How many swaps?
if(list[j]<list[j-1]){
(n-1)+(n-2)+…+1= O(n2)
temp=list[j];
Space?
list[j]=list[j-1];
In-place algorithm.
list[j-1]=temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort

You might also like