Assignment01 21103085 Atul Kumar Gupta ADA

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

Assignment-1

Name:Atul Kumar Gupta


Sid:21103085

Part 1: Searching in a list.


1:-Search for 1, 81, 7, and 3 in the list by scanning the list linearly and analyzing the
searching complexity.
CODE:
void linearsearch(int arr[], int size, int target, vector<int>& output){
for(int i=0;i<size;i++){
if(arr[i]==target){
output.push_back(i);
}
}
if(output.size()==0){
output.push_back(-1);
}
return;
}

OUTPUT:

2:-Optimize the above searching algorithm to perform searching for the same elements in
O(log(n)) average time complexity by comparing search key to the middle of the list and
recursively decreasing search space size to one half.
CODE:
int binarysearch1(int arr[], int st, int en, int tar, int val){
if(st<=en){
int mid=(st+en)/2;
if(arr[mid]==tar){
val=mid;
return binarysearch1(arr,st,mid-1,tar,val);
}
else if(arr[mid]>tar){
return binarysearch1(arr,st,mid-1,tar,val);
}
else{
return binarysearch1(arr,mid+1,en,tar,val);
}
}
else{
return val;
}
}
int binarysearch2(int arr[], int st, int en, int tar, int val){
if(st<=en){
int mid=(st+en)/2;
if(arr[mid]==tar){
val=mid;
return binarysearch2(arr,mid+1,en,tar,val);
}
else if(arr[mid]>tar){
return binarysearch2(arr,st,mid-1,tar,val);
}
else{
return binarysearch2(arr,mid+1,en,tar,val);
}
}
else{
return val;
}
}

OUTPUT:

Part 2 : Sorting elements of list.


1:-Sort the list by selecting the smallest element in the list and placing it in the beginning,
and repeating the process with the leftover elements until the entire list is sorted in increasing
order. Also, output the number of comparisons and iterations required for sorting.

CODE:
void selectionsort(int arr[], int size){
int count=0;
int iter=0;
for(int i=0;i<size-1;i++){
int minel=arr[i];
int mindex=i;
for(int j=i+1;j<size;j++){
count++;
if(arr[j]<minel){
mindex=j;
minel=arr[j];
}
}
int temp=arr[i];
arr[i]=arr[mindex];
arr[mindex]=temp;
iter++;
}
cout<<"No. of iterations: "<<iter<<endl;
cout<<"No. of comparisons: "<<count<<endl;

OUPTUT:

2;-Perform bubble sort on the same list and analyze the change in time complexity and
number of comparisons required.
CODE:
void bubblesort(int v[], int size){
int count=0;
for(int i=0;i<size-1;i++){
for(int j=0;j<size-1-i;j++){
count++;
if(v[j]>v[j+1]){
int temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
}
cout<<"No. of comparisons: "<<count<<endl;
}

OUTPUT:

3:-Implement Insertion sort algorithm to sort the above list and analyze the change in time
complexity and number of comparisons required.
CODE:
void insertionsort(int v[], int size){
int count=0;
for(int i=1;i<size;i++){
int j=i-1;
count++;
while(j>=0 and v[j]>v[j+1]){
count++;
int temp = v[j];
v[j] = v[j+1];
v[j+1]=temp;
j--;
}
}
cout<<"No. of comparisons: "<<count<<endl;
}

OUTPUT:

4:-Given two sorted sub-halves of the list 7, 8, 18, 27, 56 and 1, 4, 9, 31, 81 implement the
process of merging two sorted lists in linear time.
CODE:
void merge(int arr1[], int arr2[], int mergearr[], int s1, int s2, int s3){
int k=0;
int i=0;
int j=0;
while(k<s3){
if(i<s1 and j<s2){
if(arr1[i]<=arr2[j]){
mergearr[k]=arr1[i];
i++;
}
else{
mergearr[k]=arr2[j];
j++;
}
}
else if(i<s1){
mergearr[k]=arr1[i];
i++;
}
else if(j<s2){
mergearr[k]=arr2[j];
j++;
}
k++;
}
}
OUTPUT:

5:-Use the above program to implement the complete merge-sort algorithm.


CODE:
void mergesort(int input[],int si,int ei){
if(si>=ei){
return;
}
else{
int mid=(si+ei)/2;
mergesort(input,si,mid);
mergesort(input,mid+1,ei);
int new_arr[ei+1-si];
int j=si;
int k=mid+1;
for(int i=0;i<=ei-si;i++){
if(j<=mid and k<=ei){
if(input[j]<input[k]){
new_arr[i]=input[j];
j+=1;
}
else{
new_arr[i]=input[k];
k+=1;
}
}
else{
if(j>mid){
new_arr[i]=input[k];
k+=1;
}
else{
new_arr[i]=input[j];
j+=1;
}
}
}
for(int i=0;i<=ei-si;i++){
input[i+si]=new_arr[i];
}
}
}
void mergeSort(int input[],int n){
mergesort(input,0,n-1);
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy