Professional Documents
Culture Documents
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 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;
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