You are on page 1of 6

LAB ASSIGNMENT 1

ANALYSIS AND
DESIGN OF
ALGORITHMS
(CS1401)

NAME-MISHANK KUMAR
Nalin Kumar Gupta
SID -21103107
21103027
Code
// NAME=MISHANK KUMAR
// SID=21103107
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void LinearSearch(int * arr , int n,int key){
bool check=0;
for(int i=0;i<n;i++){
if(arr[i]==key){
cout<<"The index of key "<<key<<" is "<<i<<endl;
check=1;
break;
}
}
if(check==0){
cout<<"The key "<<key<<" is not present in the list"<<endl;
}

}
void SelectionSort(int * arr, int n){
int comparision=0;
int iterations=0;
for(int i=0;i<n-1;i++){
int minindex=i;
iterations++;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[minindex]){
minindex=j;
comparision++;
}
else{
comparision++;
}
}
swap(arr[minindex],arr[i]);
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"The number of comparisions are "<<comparision<<endl;
cout<<"The number of iterations are "<<iterations;
cout<<endl;
}

void BinarySearch(int* arr,int n,int key){


int s=0;
int e=n-1;
int mid=s+(e-s)/2;
bool check=0;
while(s<=e){
if(arr[mid]==key){
cout<<"The index of key "<<key<<" is "<<mid<<endl;
check=1;
break;
}
else if(arr[mid]>key){
e=mid-1;
}
else{
s=mid+1;
}
mid=s+(e-s)/2;
}
if(check==0){
cout<<"The key "<<key<<" is not present in the list"<<endl;
}
}
void BubbleSort(int *arr,int n){
bool swapped=0;
int comparisions=0;
for(int i=1;i<n;i++){
for(int j=0;j<n-i;j++){
comparisions++;
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
swapped=1;
}
}
if(swapped==0){
break;
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"The number of comparions required is "<<comparisions<<endl;
cout<<"The time complexity for bubble sort is O(N^2)";
cout<<endl;
}
void InsertionSort(int *arr,int n){
int comparisions=0;
for(int i=1;i<n;i++){
int temp=arr[i];
int j=i-1;
for(;j>=0;j--){
if(arr[j]>temp){
comparisions++;
arr[j+1]=arr[j];
}
else{
comparisions++;
break;
}
}
arr[j+1]=temp;
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"The number of comparisions required is "<<comparisions;
cout<<endl;
}
void Merge(int arr[],int s,int e){
int mid=s+(e-s)/2;
int len1=mid-s+1;
int len2=e-mid;
int *arr1=new int[len1];
int *arr2=new int[len2];
int k=s;
for(int i=0;i<len1;i++){
arr1[i]=arr[k++];
}
k=mid+1;
for(int i=0;i<len2;i++){
arr2[i]=arr[k++];
}
int index1=0;
int index2=0;
k=s;
while(index1<len1 && index2<len2){
if(arr1[index1]<arr2[index2]){
arr[k++]=arr1[index1++];
}
else{
arr[k++]=arr2[index2++];
}
}
while(index1<len1){
arr[k++]=arr1[index1++];
}
while(index2<len2){
arr[k++]=arr2[index2++];
}

}
void MergeSort(int arr[],int s,int e){
if(s>=e){
return ;
}
int mid=s+(e-s)/2;
MergeSort(arr,s,mid);
MergeSort(arr,mid+1,e);
Merge(arr,s,e);
}
int main(){
cout<<"Name-MISHANK KUMAR"<<endl;
cout<<"SID_21103107"<<endl;
cout<<endl;
cout<<"Part 1"<<endl;
int arr[11]={1,4,7,8,9,18,27,31,56,81};
cout<<"Q1. "<<"Search for 1,81,7,3 in the list"<<endl;
LinearSearch(arr,11,1);
LinearSearch(arr,11,81);
LinearSearch(arr,11,7);
LinearSearch(arr,11,3);
cout<<"The time complexity for searching the key in the code is O(n)"<<endl;
cout<<endl;

cout<<"Q2. "<<"Optimising the above code to get the complexity in O(log


n)"<<endl;
BinarySearch(arr,11,1);
BinarySearch(arr,11,81);
BinarySearch(arr,11,7);
BinarySearch(arr,11,3);
cout<<endl;
cout<<"Part2"<<endl;

int arr1[10]={18,56,27,7,8,4,9,31,81,1};
cout<<"Q1. "<<"Selection Sort "<<endl;
SelectionSort(arr1,10);
cout<<endl;
cout<<"Q2. Bubble Sort"<<endl;
BubbleSort(arr1,10);
cout<<endl;
cout<<"Q3. Insertion Sort"<<endl;
InsertionSort(arr1,10);
cout<<endl;
cout<<"Q4 and Q5 MergeSort"<<endl;
MergeSort(arr1,0,9);
for(int i=0;i<10;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;

}
OUTPUT

You might also like