Arpit Ds Lab 03-06
Arpit Ds Lab 03-06
Q1). WAP to insert new element at given index number in the array.
/*************************************************************
/*************************************************************
6)
#include<iostream>
08
using namespace std;
int main()
1B
{
22
cout<<"Enter the Size of array: ";
i(
cin>>to;
cin>>arr[i];
w
cin>>element;
cin>>position;
Ar
arr[i] = arr[i-1];
arr[i] = element;
to++;
cout<<arr[i]<<" ";
cout<<endl;
return 0;
6)
}
08
Time Complexity for Best Case :O(1)
Q2) WAP to implement the linear search. Use function concept, if element is found then
1B
return index number of element otherwise return -1;
/*************************************************************
22
//This program is developed by Arpit Awasthi (221B086)
/*************************************************************
i(
#include<iostream>
th
{
tA
if (arr[i] == x)
pi
return i;
Ar
return -1;
int main()
int x =46;
int n = sizeof(arr) / sizeof(arr[0]);
31
int result = search(arr, n, x);
(result == -1)
6)
return 0;
08
}
1B
Time Complexity for Best Case :O(1)
22
find the index number of given number; if element to be deleted is not
found then print “Error: element not found”.
i(
/*************************************************************
th
/*************************************************************
#include <iostream>
w
return -1;
Ar
if (x==*(a+i))
return i;
return linear_search(a,x,i+1);
}
int main()
{
int a[5]={0};
cout<<"Enter 5 numbers:"<<endl;
32
for(int i=0;i<5;i++)
{
cin>>a[i];
6)
}
int x;
08
cout<<"Enter number to be searched:"<<endl;
cin>>x;
1B
int c = linear_search(a,x,0);
int flag=0;
if(c==-1)
22
cout<<"Error: element not found."<<endl;
i(
else
{
th
{
if(i==c)
w
flag=1;
tA
if(flag==1)
{
pi
a[i]=a[i+1];
}
Ar
}
cout<<"New array:"<<endl;
for(int i=0;i<4;i++)
cout<<a[i]<<" ";
}
return 0;
}
33
Time Complexity for Worst Case :O(n)
6)
Time Complexity for Best Case :O(n)
08
Q4) WAP for checking whether there are any duplicated elements in the array or
not? /*************************************************************
1B
//This program is developed by Arpit Awasthi (221B086)
22
/*************************************************************
#include <bits/stdc++.h>
i(
using namespace std;
th
as
int main()
{
w
int n;
tA
34
for(int i = 0 ; i < n ; i++)
6)
{
08
if (arr[i] > m )
{
1B
m = arr[i];
}
}
22
i(
int * check = new int[m];
for (int i = 0 ; i < m ; i++) {
th
check[i] = 0;
as
}
w
tA
check[a]++;
}
Ar
6)
}
08
if(k)
1B
cout<<"NO duplicates found";
else
22
cout<<"Duplicates Found";
i(
return 0;
}
th
{
w
tA
pi
Ar
6)
08
1B
22
i(
th
36
as
w
LAB-4
tA
/***********************************************************
Ar
/*************************************************************
#include <stdio.h>
#include <stdlib.h>
int main()
scanf("%d", &num);
6)
arr = (int*) malloc(num * sizeof(int));
08
for(i = 0; i < num; i++) {
1B
}
int t; 22
i(
for(i = 0; i < num/2; i++) {
th
37
t = arr[i];
as
arr[i] = arr[num-1-i];
w
arr[num-1-i] = t;
tA
}
pi
return 0;
/*************************************************************
/*************************************************************
6)
#include <stdio.h>
08
#include <string.h>
#include <math.h>
1B
#include <stdlib.h>
int main() { 22
i(
th
char s[1000];
38
scanf("%[^\n]", s);
w
fr[s[i] - '0']++;
pi
}
Ar
}
return 0;
6)
below: /*************************************************************
08
//This program is developed by Arpit Awasthi (221B086)
/*************************************************************
1B
#include <stdio.h>
#include <string.h>
22
#include <math.h>
i(
#include <stdlib.h>
39
int sum = 0;
w
sum += marks[i];
tA
}
pi
return sum;
Ar
int main() {
int number_of_students;
char gender;
int sum;
scanf("%d", &number_of_students);
6)
scanf("%d", (marks + student));
08
}
1B
scanf(" %c", &gender);
22
sum = marks_summation(marks, number_of_students, gender);
printf("%d", sum);
i(
free(marks);
th
as
return 0;
40
w
}
tA
times.
Ar
/*************************************************************
/*************************************************************
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
6)
int N;
08
int d;
1B
int A[N];
22
for (int i = 0; i < N; i++)
{
i(
scanf("%d", &A[i]);
th
{
w
41
printf("%d ", A[(i + d) % N]); }
tA
puts("");
pi
return 0;
Ar
42
LAB-5
pi
Q1) Write a program to implement binary search algorithm. Assume user will enter the
sorted array.
Ar
/*************************************************************
/*************************************************************
#include<iostream>
using namespace std;
int binarysearch(int arr[], int n, int key){
int s = 0;
int e = n-1;
int mid = ((e-s)/2)+s;
while(s<=e){
if(arr[mid] == key)
{
6)
return mid;
}
else if(arr[mid] < key)
08
{
s = mid+1;
1B
}
else if(arr[mid] > key)
{
e = mid-1;
} 22
i(
mid=((e-s)/2 )+s;
}
th
return -1;
}
as
int main(){
int n;
cin>>n;
w
int arr[n],i;
tA
43
int key;
Ar
cin>>key;
int f;
f = binarysearch(arr, n, key);
cout<<f;
return 0;
}
Q2) Write a function which accepts an array of integers along with the size of it. The
numbers are arranged in the list in increasing order until a particular index and after that it
is arranged in decreasing order. This function should find and return the index position at
which the increasing list starts decreasing. Call this function from main function.
/*************************************************************
6)
/*************************************************************
08
#include <iostream>
using namespace std;
1B
int maximum(int arr[], int s, int e)
{
if (s == e){
}
22
return arr[e];
int mid = s + (e - s) / 2;
i(
if (arr[mid] > arr[mid - 1] and arr[mid] > arr[mid + 1]){
return arr[mid];
th
}
else if (arr[mid] < arr[mid + 1]){
as
}
}
int main()
pi
{
int arr[] = { 1, 4, 7, 8, 9, 5, 4 };
Ar
44
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The maximum element is "
<< maximum(arr, 0, n - 1);
return 0;
}
Q3) Write a program to check whether given Matrix is sparse or not. We say a matrix as
sparse when more than 50% of total elements are zero. If matrix is sparse then represent it
in triplet form with the help of array data structure. Also print the number of bytes that are
saved or wasted when you represent input matrix in the triplet form.
/*************************************************************
6)
/*************************************************************
08
#include <bits/stdc++.h>
using namespace std;
1B
int main()
{
int n, m;
22
i(
cout<<"Enter the no of rows : ";
cin>>n;
th
int arr[n][m];
int maxi = n*m;
w
cin>>arr[i][j];
45
}
}
int count1 = 0;
6)
cout<<endl;
}
08
if(count1 > (maxi/2))
{
1B
cout<<"It is a sparse matrix an it's triplet form is below : "<<endl; int
22
int trp[trp_size + 1][3];
i(
// Initializing the first row
trp[0][0] = n;
th
trp[0][1] = m;
trp[0][2] = trp_size;
as
int trp_row = 1;
w
{
for(int j = 0 ; j < m ; j++)
{
pi
if (arr[i][j] != 0)
{
Ar
trp[trp_row][0] = i+1;
trp[trp_row][1] = j+1;
trp[trp_row][2] = arr[i][j];
46
trp_row++;
}
}
}
6)
cout<<endl;
}
int arr_bytes = n*m*4;
08
int trp_bytes = trp_size*3*4;
1B
if(arr_bytes > trp_bytes)
{
}
else
22
cout<<"No of bytes saved : "<<arr_bytes - trp_bytes<<endl;
i(
{
cout<<"No of bytes saved : "<< trp_bytes - arr_bytes<<endl;
th
}
as
}
w
Q4) Write a time efficient program for finding the element which appears maximum number
of times in the array. Sample input: 2, 4, 5, 6, 8, 9, 10, 13, 2, 3, 2 Sample output: 2 [as 2 is
pi
/*************************************************************
/*************************************************************
47
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the Array size : ";
cin>>n;
6)
cout<<"Enter the Elements of the array : ";
08
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
1B
}
int m = arr[0];
{ 22
for(int i = 0 ; i < n ; i++)
if (m < arr[i])
i(
{
m = arr[i];
th
}
}
as
{
temp[i] = 0;
tA
}
pi
{
int a = arr[i];
temp[a]++;
}
int k = 0;
for(int i = 0 ; i < m ; i++)
48
{
if (temp[k] < temp[i])
{
k = i;
}
}
6)
Time Complexity for Worst Case :O(n)
Time Complexity for Best Case :O(n)
08
1B
22
i(
th
as
w
tA
pi
Ar
49
LAB-6
Q1) WAP to implement a function Rdm(n) which returns an array of random
numbers{between 0 to 99}, where n is the size of array. (Hint: use dynamic memory
allocation concept).
/*************************************************************
6)
/*************************************************************
08
#include<iostream>
1B
using namespace std;
int* Rdm(int n)
{
22
i(
int *p=new int[n];
th
as
for(int x=0;x<n;x++)
{
w
tA
p[x]=rand()%100;
pi
}
Ar
return p;
Q2) WAP to implement the bubble sort and show the output of each pass.
50
/*************************************************************
/*************************************************************
#include<iostream>
#include"Lab6_Q1.h"
void Pass(int arr[],int n)
6)
{
for(int x=0;x<n;x++)
{
08
cout<<arr[x]<<" ";
}
}
1B
int main()
{
int size;
cin>>size;
22
cout<<"Enter size of Array : ";
int *arr=Rdm(size);
cout<<"\nUnsorted array : "<<endl;
i(
for(int x=0;x<size;x++)
{
th
cout<<arr[x]<<" ";
}
as
cout<<endl;
cout<<"Bubble sort : "<<endl;
for(int x=0;x<size-1;x++)
w
{
for(int y=0;y<size-1-x;y++)
tA
{
if(a[y]>a[y+1])
{
int m=[y];
pi
a[y]=a[y+1];
a[y+1]=m;
Ar
}
}
cout<<"\nPass "<<(x+1)<<" : ";
showPass(a,size);
cout<<endl;
}
cout<<"\nSorted Array : "<<endl;
for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
}
51
Time Complexity for Worst Case :O(n)
Time Complexity for Best Case :O(n^2)
Q3) WAP to implement the selection sort and show the output of each
pass.
6)
/*************************************************************
08
//This program is developed by Arpit Awasthi (221B086)
/*************************************************************
1B
#include<iostream>
#include"Lab6_q1.h"
void Pass(int arr[],int n)
{
22
for(int x=0;x<n;x++)
{
cout<<arr[x]<<" ";
i(
}
}
th
int main()
{
as
int size;
cout<<"Enter size of Array : ";
cin>>size;
w
int *a=Rdm(size);
cout<<"\nUnsorted Array : "<<endl;
tA
for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
pi
cout<<endl;
cout<<"Selection Sort : "<<endl;
Ar
for(int x=0;x<size-1;x++)
{
int index=x;
for(int y=x+1;y<size;y++)
{
if(a[index]>a[y])
{
index=y;
}
}
if(index!=x)
{
int m=a[index];
a[index]=a[x];
a[x]=m;
52
}
cout<<"\nPass "<<(x+1)<<" : ";
6)
showPass(a,size);
cout<<endl;
08
}
1B
{
cout<<ar[x]<<" ";
}
return 0;
} 22
Time Complexity for Worst Case :O(n^2)
i(
Time Complexity for Best Case :O(n^2)
Q4) WAP to implement the insertion sort and show the output of each
th
pass.
as
/*************************************************************
w
/*************************************************************
#include<iostream>
#include"Lab6_Q1.h"
void Pass(int arr[],int n)
pi
{
for(int x=0;x<n;x++)
Ar
{
cout<<arr[x]<<" ";
}
}
int main()
{
int size;
cout<<"Enter size of#include<iostream>
#include"Lab6_q1.h"
void showPass(int arr[],int n)
{
for(int x=0;x<n;x++)
{
cout<<arr[x]<<" ";
}
}
int main()
{
int size;
cout<<"Enter size of Array : ";
6)
cin>>size;
53
08
int *a=Rdm(size);
cout<<"\nUnsorted Array : "<<endl;
for(int x=0;x<size;x++)
{
1B
cout<<a[x]<<" ";
}
cout<<endl;
int k,y;
22
cout<<"Insertion Sort : "<<endl;
for(int x=1;x<size;x++)
i(
{
k=a[x];
th
y=x-1;
while(y>=0 && a[y]>key)
{
as
a[y+1]=a[y];
y=y-1;
}
w
a[y+1]=key;
cout<<"\nPass "<<(x+1)<<" : ";
tA
showPass(a,size);
cout<<endl;
}
pi
{
cout<<a[x]<<" ";
}
return 0;
} Array : ";
cin>>size;
int *a=Rdm(size);
cout<<"\nUnsorted Array : "<<endl;
for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
cout<<endl;
cout<<"Selection Sort mechanism : "<<endl;
for(int x=0;x<size-1;x++)
{
int index=x;
for(int y=x+1;y<size;y++)
{
6)
if(a[index]>a[y])
{
index=y;
08
}
}
if(index!=x)
1B
54
{
int t=a[index];
a[index]=a[x];
a[x]=t;
} 22
cout<<"\nPass "<<(x+1)<<" : ";
i(
showPass(a,size);
cout<<endl;
}
th
for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
w
return 0;
}
tA
Q5) WAP to implement the quick sort and show the output of each
Ar
pass.
/*************************************************************
/*************************************************************
#include <iostream>
#include "LAB6_Q1.h"
using namespace std;
int *rnd(int);
int n = 10;
6)
}
}
08
swap(arr[i + 1], arr[high]);
return i + 1;
}
1B
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
22
quickSort(arr, low, pivotIndex - 1);
}
cout << endl;
}
as
int main() {
w
cout<<"Array:"<<endl;
for (int k = 0; k < n; k++) {
cout << arr[k] << " ";
pi
}
cout<<endl;
quickSort(arr, 0, n-1);
Ar
return 0;
}
Time Complexity for Worst Case :O(n^2)
Time Complexity for Best Case :O(nlogn)
Q6) WAP to implement the merge sort and show the output of each
pass.
/*************************************************************
/*************************************************************
#include<bits/stdc++.h>
using namespace std;
6)
{
int mid = s + (e-s)/2;
// Creating two parts
08
int len1 = mid + 1 -s;
int len2 = e - mid;
1B
int *first = new int[len1];
int *second = new int[len2];
22
int index = s;
for (int i = 0 ; i < len1 ; i++)
{
i(
first[i] = arr[index++];
}
th
56
index = mid + 1;
for(int i = 0 ; i < len2 ; i++)
as
{
second[i] = arr[index++];
}
w
index = s;
int index1 = 0;
tA
int index2 = 0;
while(index1 < len1 && index2 < len2)
{
pi
else
{
arr[index++] = second[index2++]; }
}
6)
return ;
}
08
int mid = s + (e-s)/2;
1B
// Left Part
mergesort(arr , s , mid);
// Right Part
22
mergesort(arr , mid+1 , e);
i(
57
// Merge the Sorted array
th
mergesorted(arr , s , e);
as
}
w
tA
int main()
{
int arr[5] = {2 , 3 , 0 ,9 , 1};
pi
// Merge sort
mergesort(arr , 0 , 4);
Ar
6)
08
1B
22
i(
58
59
th
as
w
tA
pi
Ar