Lab Manual Print
Lab Manual Print
OUTPUT –
II. Given an already sorted array of positive integers, design an algorithm
and implement it using a program to find whether given key element is
present in the array or not. Also, find total number of comparisons for each
input case. (Time Complexity = O(nlogn), where n is the size of input).
SOURCE CODE –
#include<iostream>
using namespace std;
int binary_search(int arr[],int left,int right,int key,int &comparisons){
while(left<=right){
comparisons++;
int mid=left+(right-left)/2;
if(arr[mid] == key){
return mid; //key present at mid
}
if(arr[mid<key]){
left=mid+1;
}else{
right=mid-1;
}} return -1; //key not present
}
int main(){
int n,key,comparisons=0;
cout<<"Enter the size of an array :";
cin>>n;
int arr[n];
cout<<"Enter the elements in an array :";
for(int i=0;i<n;++i){
cin>>arr[i];
}
cout<<"Enter the key element to search :";
cin>>key;
int result=binary_search(arr,0,n-1,key,comparisons);
if(result!= -1){
cout<<"Element found at :"<<result<<endl;
}else{
cout<<"Element not found !"<<endl;
}
cout<<"Total number of comparisons are :"<<comparisons<<endl;
return 0;
}
OUTPUT –
III. Given an already sorted array of positive integers, design an algorithm
and implement it using a program to find whether a given key element is
present in the sorted array or not. For an array arr[n], search at the
indexes arr[0], arr[2], arr[4],.....,arr[2k ] and so on. Once the interval
(arr[2k] < key < arr[2k+1] ) is found, perform a linear search operation
from the index 2k to find the element key. (Complexity < O(n), where n is
the number of elements need to be scanned for searching): Jump Search
SOURCE CODE –
#include<iostream>
#include<cmath>
using namespace std;
void jumpSearch(int arr[],int n,int key,int &comparisons){
int step=sqrt(n);
int prev=0;
comparisons=0;
while(arr[min(step,n)-1]<key){
comparisons++;
prev=step;
step += sqrt(n);
if(prev>=n){
cout<<"Not Present :"<<comparisons<<endl;
return;
}}
for(int i=prev;i<min(step,n);i++){
comparisons++;
if(arr[i] == key){
cout<<"Present :"<<comparisons<<endl;
return;
}}
cout<<"Not Present :"<<comparisons<<endl;
}
int main(){
int T;
cout<<"Enter the number of test cases: ";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
cout<<"Enter the elements in the array: ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
int key;
cout<<"Enter the key to be searched: ";
cin>>key;
int comparisons = 0;
jumpSearch(arr,n,key,comparisons);
}
return 0;
}
OUTPUT -
WEEK 02
I. Given a sorted array of positive integers containing few duplicate
elements, design an algorithm and implement it using a program to find
whether the given key element is present in the array or not. If present,
then also find the number of copies of given key. (Time Complexity = O(log
n)) .
SOURCE CODE –
#include<iostream>
using namespace std;
int first_occurrence(int arr[],int n,int key){
int low=0,high=n-1,result=-1;
while(low<=high){
int mid=low+(high-low)/2;
if(arr[mid]==key){
result=mid;
high=mid-1;
}else if(arr[mid]>key){
high=mid-1;
}else{
low=mid+1;
} } return result;
}
int last_occurrence(int arr[],int n,int key){
int low=0,high=n-1,result=-1;
while(low<=high){
int mid=low+(high-low)/2;
if(arr[mid]==key){
result=mid;
low=mid+1;
}else if(arr[mid]>key){
high=mid-1;
}else{
low=mid+1;
} } return result;
}
int count_occurrence(int arr[],int n,int key){
int first=first_occurrence(arr,n,key);
if(first==-1){
return 0;
}
int last=last_occurrence(arr,n,key);
return last-first+1;
}
int main(){
int T;
cout<<"Enter the number of test cases :";
cin>>T;
while(T--){
int n,key;
cout<<"Enter the size of an array :";
cin>>n;
int arr[n];
cout<<"Enter the elements of an array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Enter key to be searched :";
cin>>key;
int count=count_occurrence(arr,n,key);
if(count>0){
cout<<key<<" - "<<count<<endl;
}else{
cout<<"Key not present !"<<endl;
}}
return 0;
}
OUTPUT –
II. Given a sorted array of positive integers, design an algorithm and
implement it using a program to find three indices i, j, k such that arr[i] +
arr[j] = arr[k].
SOURCE CODE –
#include<iostream>
using namespace std;
void findIndice(int arr[],int n){
for(int k=n-1;k>=2;k--){
int i=0;
int j=k-1;
while(i<j){
if(arr[i] + arr[j] == arr[k]){
cout<<i<<" "<<j<<" "<<k<<endl;
return;
}else if(arr[i] + arr[j] < arr[k]){
i++;
}else{
j--;
}}}
cout<<"No sequence found !"<<endl;
}
int main(){
int T;
cout<<"Enter the number of test cases: ";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of an array: ";
cin>>n;
int arr[n];
cout<<"Enter the elements of the array: ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
findIndice(arr, n);
}
return 0;
}
OUTPUT –
III. Given an array of nonnegative integers, design an algorithm and a
program to count the number of pairs of integers such that their difference
is equal to a given key, K.
SOURCE CODE –
#include<iostream>
using namespace std;
int countPairWithDiff(int arr[],int n,int k){
int count=0;
for(int i=0;i<n;++i){
for(int j=i+1;j<n;++j){
if(arr[i] - arr[j] == k){
++count;
}}} return count;
}
int main(){
int T;
cout<<"Enter the number of test cases :";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of an array :";
cin>>n;
int arr[n];
cout<<"Enter the elements of the array: ";
for(int i=0;i<n;++i){
cin>>arr[i];
}
int key;
cout<<"Enter key to be searched :";
cin>>key;
int result=countPairWithDiff(arr,n,key);
cout<<"Number of pairs with difference "<<key<<" is: "<<result<<endl;
}
return 0;
}
OUTPUT –
WEEK 03
I. Given an unsorted array of integers, design an algorithm and a program
to sort the array using insertion sort. Your program should be able to find
number of comparisons and shifts (shifts - total number of times the array
elements are shifted from their place) required for sorting the array.
SOURCE CODE –
#include<iostream>
using namespace std;
void insertionSort(int arr[],int n,int &comparisons,int &shifts){
comparisons=0;
shifts=0;
for(int i=1;i<n;i++){
int key=arr[i];
int j=i-1;
while(j>=0 && arr[j] > key){
comparisons++;
arr[j+1]=arr[j];
shifts++;
j--;
}
arr[j+1]=key;
shifts++;
if(j>=0)comparisons++;
}}
int main(){
int T;
cout<<"Enter the number of test cases :";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of an array :";
cin>>n;
int arr[n];
cout<<"Enter the elements of an array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
int comparisons,shifts;
insertionSort(arr,n,comparisons,shifts);
cout<<"Sorted array: \n ";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" "<<endl;
}
cout<<"Number of comparisons: "<<comparisons<<endl;
cout<<"Number of shifts: "<<shifts<<endl;
}
return 0;
}
OUTPUT –
II. Given an unsorted array of integers, design an algorithm and
implement a program to sort this array using selection sort. Your program
should also find number of comparisons and number of swaps required.
SOURCE CODE –
#include<iostream>
using namespace std;
void selectionSort(int arr[],int n,int &comparisons,int &swaps){
comparisons=0;
swaps=0;
for(int i=0;i<n-1;i++){
int minIndex=i;
for(int j=i+1;j<n;j++){
comparisons++;
if(arr[j]<arr[minIndex]){
minIndex=j;
}}
if(minIndex!=i){
swap(arr[i],arr[minIndex]);
swaps++;
}}}
int main(){
int T;
cout<<"Enter the number of test cases :";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of an array :";
cin>>n;
int arr[n];
cout<<"Enter the elements of an array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
int comparisons,swaps;
selectionSort(arr,n,comparisons,swaps);
cout<<"Sorted array:\n";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" "<<endl;
}
cout<<"Number of comparisons: "<<comparisons<<endl;
cout<<"Number of swaps: "<<swaps<<endl;
}
return 0;
}
OUTPUT –
III. Given an unsorted array of positive integers, design an algorithm and
implement it using a program to find whether there are any duplicate
elements in the array or not. (use sorting) (Time Complexity = O(n log n)).
SOURCE CODE -
#include<iostream>
using namespace std;
void merge(int arr[],int lb,int mid,int ub){
int i,j,k;
int b[50];
i=lb;
j=mid+1;
k=lb;
while(i<=mid && j<=ub){
if(arr[i]<=arr[j]){
b[k]=arr[i];
i++;
}else{
b[k]=arr[j];
j++;
}
k++;
}
if(i>mid){
while(j<=ub){
b[k]=arr[j];
j++;
k++;
}
}else{
while(i<=mid){
b[k]=arr[i];
i++;
k++;
}}
for(k=lb;k<=ub;k++){
arr[k]=b[k];
}}
void mergeSort(int arr[],int lb,int ub){
if(lb<ub){
int mid=(lb+ub)/2;
mergeSort(arr,lb,mid);
mergeSort(arr,mid+1,ub);
merge(arr,lb,mid,ub);
}}
int check_duplicates(int arr[],int n){
for(int i=1;i<n;i++){
if(arr[i] == arr[i-1]){
return 1;
}} return 0;
}
int main(){
int T;
cout<<"Enter the test cases :";
cin>>T;
while(T--){
int n;
cout<<"Enter the size of an array :";
cin>>n;
int arr[50];
cout<<"Enter the elements in an array :";
for(int i=0;i<n;i++){
cin>>arr[i];
}
mergeSort(arr,0,n - 1);
if(check_duplicates(arr,n)){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}}
return 0;
}
OUTPUT –