Chapter 2
Chapter 2
Pseudo code
Loop through the array starting at the first element until the value of target matches one of the array
elements.
Time is proportional to the size of input (n) and we call this time complexity O(n).
Algorithm
1 / input “n” elements of the array and elements going to be searched “x”.
3/ if (array[i]==x)
flag=1
return i
4/ if (flag==0)
Return -1
6/ return index
7/ exit
Implementation:
if(flag==0)
{
index=-1;
}
else
{
index=i;
}
return index;
}
b) Binary Search
This searching algorithms works only on an ordered list.
The computational time for this algorithm is proportional to log2 n. Therefore the time complexity is
O(log n)
Algorithm
1 / input “n” elements of the array and the element going to be searched “x”;
4/ mid=(right+left)/2;
if(array[mid]==x)
if(array[mid]<x)
left=mid+1
otherwise
right= mid-1
6/ return index
Implementation:
else{
if(array[mid]<x)
left=mid+1;
else
right=mid-1;
}
}while(flag==0&&left<right);
if(flag==0)
index=-1;
else
index=mid;
return index;
}
Sorting is used to arrange names or numbers in meaning full ways.by default sorting is performed in
ascending order.
There are two types of sorting based on the type of memory the sorting technique takes place
1) Internal sorting
The following are simple sorting algorithms used to sort small-sized lists.
• Ordinary/linear sort
• Bubble Sort
• Selection Sort
• Insertion Sort
a) Ordinary/linear sort
Basic idea:
Hold the first element that is A[0] and compare with all elements of the array. After completing the first
pass the smallest will be found from the array and it can be placed in its right position. Let us consider
“A” is an array with n elements.
Algorithm
4/if(array[i]> array[j])
temp = array[i]
array[i]=array[j]
array[j]=temp
6/exit
Implementation of Ordinary or linear sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp;
int n=5;
int array[5];
for(i=0;i<n; i++){
for(j=i+1;j<n; j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}//swap adjacent elements
}//end of inner loop
getch( );
}//end of linear_sort
b) Bubble Sort
Bubble sort is the simplest algorithm to implement and the slowest algorithm on very large inputs.
Bubble sort:
Makes a number of passes through array:
First bubble the largest element putted in last position, by
iteratively comparing & swapping adjacent elements starting
with the start of the list
Next bubble next largest element putted to last-1 position
etc. until all apart from first element placed
Basic Idea:
Loop through array from i=0 to n and swap adjacent elements if they are out of order.
Algorithm
4/if(array[j]>array[j+1])
temp = array[j]
array[j]=array[j+1]
array[j+1]=temp
6/exit
Implementation of Bubble sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp;
int n=5;
int array[5];
for(i=0;i<n; i++){
for(j=0;j<n-1-i; j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}//swap adjacent elements
}//end of inner loop
getch( );
Basic Idea:
Scan array to find smallest element: swap it with element in first position
Scan remainder of array to find smallest element: swap it with element in second
position
etc. until all elements placed in correct position
Algorithm
min=array[i]
loc=i
4/if(array[j]<min)
min = array[j]
loc=j
5/if(loc!=i)
temp = array[i]
array[i] = array[loc]
array[loc]=temp
for(i=0;i<n; i++){
min=array[i];
loc=i;
for(j=i+1;j<n; j++)
{
if(array[j]<min)
{
min = array[j];
loc=j;
}
}//end of inner loop
if(loc != i)
{
temp=array[i];
array[i]=array[loc];
array[loc]=temp;
}
}//end of outer loop
getch( );
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. While playing cards, you pick up a card, start at the beginning of your
hand and find the place to insert the new card, insert it and move all the others up one place.
Basic Idea: Find the location for an element and move all others up, and insert the element.
First, consider first two elements: if they are out of order then swap them
Next consider, the third element: insert it into its correct location in the 2 elements preceding it
Next consider the fourth element: insert it into its correct location in the 3 elements preceding it
etc. up to last element.
Algorithm
temp=array[i]
pos=pos-1
3/ repeat through step “3” if ( temp < array[pos] and pos >=0)
Array[pos+1]=array[pos]
pos=pos-1
4/array[pos+1]=temp
6/exit
Implementation of insertion sorting in C++:
#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr();
int i,j,temp,pos;
int n=5;
int array[5];
for(i=1;i<n; i++){
temp=array[i];
pos=i-1;
for(;temp<array[pos] && pos>=0;)
{
array[pos+1]=array[pos];
pos=pos-1;
getch( );
int i, temp, j;
temp = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = temp;