You are on page 1of 30

LAB-3

Q1). WAP to insert new element at given index number in the array.
/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include<iostream>

using namespace std;

int main()

int arr[40], i, element, position, to;

cout<<"Enter the Size of array: ";

cin>>to;

cout<<"Enter "<<to<<" elements of array: ";

for(i=0; i<to; i++)

cin>>arr[i];

cout<<"\nEnter the element to Insert: ";

cin>>element;

cout<<"which position ? ";

cin>>position;

for(i=to; i>=position; i--)

arr[i] = arr[i-1];

arr[i] = element;

to++;

cout<<"\nNew Array is:\n";


30
for(i=0; i<to; i++)

cout<<arr[i]<<" ";

cout<<endl;

return 0;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(1)

Q2) WAP to implement the linear search. Use function concept, if element is found then
return index number of element otherwise return -1;
/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include<iostream>

using namespace std;

int search(int arr[], int n, int x)

for (int i = 0; i < n; i++)

if (arr[i] == x)

return i;

return -1;

int main()

int arr[] = {3,4,46,78,80};

int x =46;

int n = sizeof(arr) / sizeof(arr[0]);


31
int result = search(arr, n, x);

(result == -1)

? cout << "Element not present in an array"

: cout << "Element present at index number" << result;

return 0;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(1)

Q3) WAP to delete an element from an array, use search algorithm to


find the index number of given number; if element to be deleted is not
found then print “Error: element not found”.
/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include <iostream>
using namespace std;
int linear_search(int *a,int x,int i)
{
if (i==5)
return -1;
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];
}
int x;
cout<<"Enter number to be searched:"<<endl;
cin>>x;
int c = linear_search(a,x,0);
int flag=0;
if(c==-1)
cout<<"Error: element not found."<<endl;
else
{
for (int i=0;i<5;i++)
{
if(i==c)
flag=1;
if(flag==1)
{
a[i]=a[i+1];
}

}
cout<<"New array:"<<endl;
for(int i=0;i<4;i++)
cout<<a[i]<<" ";

}
return 0;
}

33
Time Complexity for Worst Case :O(n)
Time Complexity for Best Case :O(n)

Q4) WAP for checking whether there are any duplicated elements in the array or not?
/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cout<<"Enter the Size of the Array : ";
cin>>n;

int * arr = new int[n];

cout<<"Enter the Elements : "<<endl;


for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}

int m = arr[0];

// Finding the largest Element

34
for(int i = 0 ; i < n ; i++)
{
if (arr[i] > m )
{
m = arr[i];
}
}

int * check = new int[m];

for (int i = 0 ; i < m ; i++)


{
check[i] = 0;
}

for (int i = 0 ; i < n ; i++)


{
int a = arr[i];
check[a]++;
}

//Checking for duplicates


int k = 1;
for(int i = 0 ; i < m ; i++)
{
if(check[i] > 1)
{
k = 0;
35
break;
}
}

if(k)
cout<<"NO duplicates found";
else
cout<<"Duplicates Found";

return 0;
}
Time Complexity for Worst Case :O(n)
Time Complexity for Best Case :O(1)
{

36
LAB-4
Q1) Write a program to reverse the elements of an array.

/***********************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include <stdio.h>

#include <stdlib.h>

int main()

int num, *arr, i;

scanf("%d", &num);

arr = (int*) malloc(num * sizeof(int));

for(i = 0; i < num; i++) {

scanf("%d", arr + i);

int t;

for(i = 0; i < num/2; i++) {

37
t = arr[i];

arr[i] = arr[num-1-i];

arr[num-1-i] = t;

for(i = 0; i < num; i++)

printf("%d ", *(arr + i));

return 0;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)

Q2) Write a C program to print the frequency of the digits in given


alphanumeric string.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int main() {

char s[1000];

int fr[10] = {0};


38
scanf("%[^\n]", s);

for (int i = 0; i < strlen(s); i++) {

if (s[i] >= '0' && s[i] <= '9') {

fr[s[i] - '0']++;

for (int i = 0; i < 10; i++) {

printf("%d ", fr[i]);

return 0;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)

Q3) Write C program to complete “Students Marks Sum’ as mentioned below:

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int marks_summation(int* marks, int number_of_students, char gender) {

39
int sum = 0;

for(int i = (gender == 'b' ? 0 : gender == 'g' ? 1 : -1); i < number_of_students; i+=2) {

sum += marks[i];

return sum;

int main() {

int number_of_students;

char gender;

int sum;

scanf("%d", &number_of_students);

int *marks = (int *) malloc(number_of_students * sizeof (int));

for (int student = 0; student < number_of_students; student++) {

scanf("%d", (marks + student));

scanf(" %c", &gender);

sum = marks_summation(marks, number_of_students, gender);

printf("%d", sum);

free(marks);

return 0;

40
}

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)

Q4) Write a C/C++ program to left rotate an array of integers by d


times.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

int main() {

int N;

int d;

scanf("%d %d", &N, &d);

int A[N];

for (int i = 0; i < N; i++)

scanf("%d", &A[i]);

for (int i = 0; i < N; i++)

{
41
printf("%d ", A[(i + d) % N]);

puts("");

return 0;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)

42
LAB-5
Q1) Write a program to implement binary search algorithm. Assume user will enter the
sorted array.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#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)
{
return mid;
}
else if(arr[mid] < key)
{
s = mid+1;
}
else if(arr[mid] > key)
{
e = mid-1;
}

mid=((e-s)/2 )+s;
}
return -1;
}
int main(){
int n;
cin>>n;
int arr[n],i;
for(i = 0; i < n; i++)
cin>>arr[i];
n = sizeof(arr) / sizeof(arr[0]);

43
int key;
cin>>key;
int f;
f = binarysearch(arr, n, key);
cout<<f;
return 0;
}

Time Complexity for Worst Case :O(logn)


Time Complexity for Best Case :O(1)

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.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include <iostream>
using namespace std;
int maximum(int arr[], int s, int e)
{
if (s == e){
return arr[e];
}
int mid = s + (e - s) / 2;
if (arr[mid] > arr[mid - 1] and arr[mid] > arr[mid + 1]){
return arr[mid];
}
else if (arr[mid] < arr[mid + 1]){
return maximum(arr, mid + 1, e);
}
else{
return maximum(arr, s, mid - 1);
}
}
int main()
{
int arr[] = { 1, 4, 7, 8, 9, 5, 4 };

44
int n = sizeof(arr) / sizeof(arr[0]);
cout << "The maximum element is "
<< maximum(arr, 0, n - 1);
return 0;
}

Time Complexity for Worst Case :O(logn)


Time Complexity for Best Case :O(1)

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.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n, m;

cout<<"Enter the no of rows : ";


cin>>n;
cout<<"Enter the no of cols : ";
cin>>m;

int arr[n][m];
int maxi = n*m;

cout<<"Enter the Elements : "<<endl;

for(int i= 0 ; i < n ; i++)


{
for(int j = 0 ; j < m ; j++)
{
cin>>arr[i][j];
45
}
}
int count1 = 0;

for(int i= 0 ; i < n ; i++)


{
for(int j = 0 ; j < m ; j++)
{
if (arr[i][j] == 0)
{
count1++;
}
}
cout<<endl;
}

if(count1 > (maxi/2))


{
cout<<"It is a sparse matrix an it's triplet form is below : "<<endl;

int trp_size = (n*m) - count1 ;

int trp[trp_size + 1][3];

// Initializing the first row


trp[0][0] = n;
trp[0][1] = m;
trp[0][2] = trp_size;

int trp_row = 1;

for(int i= 0 ; i < n ; i++)


{
for(int j = 0 ; j < m ; j++)
{
if (arr[i][j] != 0)
{
trp[trp_row][0] = i+1;
trp[trp_row][1] = j+1;
trp[trp_row][2] = arr[i][j];
46
trp_row++;

}
}
}

for(int i= 0 ; i < trp_size + 1 ; i++)


{
for(int j = 0 ; j < 3 ; j++)
{
cout<<trp[i][j]<<" ";
}
cout<<endl;
}
int arr_bytes = n*m*4;
int trp_bytes = trp_size*3*4;

if(arr_bytes > trp_bytes)


{

cout<<"No of bytes saved : "<<arr_bytes - trp_bytes<<endl;


}
else
{
cout<<"No of bytes saved : "<< trp_bytes - arr_bytes<<endl;
}

}
Time Complexity for Worst Case :O(nm)
Time Complexity for Best Case :O(nm)

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
coming three times.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
47
#include <bits/stdc++.h>
using namespace std;

int main()
{
int n;
cout<<"Enter the Array size : ";
cin>>n;

int *arr = new int[n];

cout<<"Enter the Elements of the array : ";

for(int i = 0 ; i < n ; i++)


{
cin>>arr[i];
}

int m = arr[0];
for(int i = 0 ; i < n ; i++)
{
if (m < arr[i])
{
m = arr[i];
}
}

int *temp = new int[m];

for(int i = 0 ; i < m ; i++)


{
temp[i] = 0;
}

for(int i = 0 ; i < n ; i++)


{
int a = arr[i];
temp[a]++;
}

int k = 0;
for(int i = 0 ; i < m ; i++)
48
{
if (temp[k] < temp[i])
{
k = i;
}
}

cout<<k<<" Appears maximum no of times";

}
Time Complexity for Worst Case :O(n)
Time Complexity for Best Case :O(n)

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).

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************

#include<iostream>

using namespace std;

int* Rdm(int n)

int *p=new int[n];

for(int x=0;x<n;x++)

p[x]=rand()%100;

return p;

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)

Q2) WAP to implement the bubble sort and show the output of each pass.
50
/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include<iostream>
#include"Lab6_Q1.h"
void Pass(int arr[],int n)
{
for(int x=0;x<n;x++)
{
cout<<arr[x]<<" ";
}
}
int main()
{
int size;
cout<<"Enter size of Array : ";
cin>>size;
int *arr=Rdm(size);
cout<<"\nUnsorted array : "<<endl;
for(int x=0;x<size;x++)
{
cout<<arr[x]<<" ";
}
cout<<endl;
cout<<"Bubble sort : "<<endl;
for(int x=0;x<size-1;x++)
{
for(int y=0;y<size-1-x;y++)
{
if(a[y]>a[y+1])
{
int m=[y];
a[y]=a[y+1];
a[y+1]=m;
}
}
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.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include<iostream>
#include"Lab6_q1.h"
void Pass(int arr[],int n)
{
for(int x=0;x<n;x++)
{
cout<<arr[x]<<" ";
}
}
int main()
{
int size;
cout<<"Enter size of 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 : "<<endl;
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)<<" : ";
showPass(a,size);
cout<<endl;
}

cout<<"\nSorted Array : "<<endl;


for(int x=0;x<size;x++)
{
cout<<ar[x]<<" ";
}
return 0;
}
Time Complexity for Worst Case :O(n^2)
Time Complexity for Best Case :O(n^2)

Q4) WAP to implement the insertion sort and show the output of each pass.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include<iostream>
#include"Lab6_Q1.h"
void Pass(int arr[],int n)
{
for(int x=0;x<n;x++)
{
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 : ";
cin>>size;

53
int *a=Rdm(size);
cout<<"\nUnsorted Array : "<<endl;
for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
cout<<endl;
cout<<"Insertion Sort : "<<endl;

int k,y;
for(int x=1;x<size;x++)
{
k=a[x];
y=x-1;
while(y>=0 && a[y]>key)
{
a[y+1]=a[y];
y=y-1;
}
a[y+1]=key;
cout<<"\nPass "<<(x+1)<<" : ";
showPass(a,size);
cout<<endl;
}

cout<<"\nSorted Array : "<<endl;


for(int x=0;x<size;x++)
{
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++)
{
if(a[index]>a[y])
{
index=y;
}
}
if(index!=x)
54
{
int t=a[index];
a[index]=a[x];
a[x]=t;
}
cout<<"\nPass "<<(x+1)<<" : ";
showPass(a,size);
cout<<endl;
}

cout<<"\nSorted Array : "<<endl;


for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
return 0;
}
Time Complexity for Worst Case :O(n^2)
Time Complexity for Best Case :O(n)

Q5) WAP to implement the quick sort and show the output of each pass.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include <iostream>
#include "LAB6_Q1.h"
using namespace std;
int *rnd(int);
int n = 10;

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
55
quickSort(arr, pivotIndex + 1, high);
cout << "Pass: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}

int main() {
int *arr = rnd(10);
int n = 10;
cout<<"Array:"<<endl;
for (int k = 0; k < n; k++) {
cout << arr[k] << " ";
}
cout<<endl;
quickSort(arr, 0, n-1);
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.

/*************************************************************

//This program is developed by NISHANT KUMAR (221B253)

/*************************************************************
#include<bits/stdc++.h>
using namespace std;

void mergesorted(int * arr , int s , int e)


{
int mid = s + (e-s)/2;
// Creating two parts
int len1 = mid + 1 -s;
int len2 = e - mid;

int *first = new int[len1];


int *second = new int[len2];

int index = s;
for (int i = 0 ; i < len1 ; i++)
{
first[i] = arr[index++];
}
56
index = mid + 1;
for(int i = 0 ; i < len2 ; i++)
{
second[i] = arr[index++];
}
index = s;
int index1 = 0;
int index2 = 0;
while(index1 < len1 && index2 < len2)
{
if (first[index1] < second[index2])
{
arr[index++] = first[index1++];
}
else
{
arr[index++] = second[index2++];
}
}

while(index1 < len1)


{
arr[index++] = first[index1++];
}
while(index2 < len2)
{
arr[index++] = second[index2++];
}
}

void mergesort(int * arr , int s , int e)


{
// base case
if (s >= e)
{
return ;
}

int mid = s + (e-s)/2;

// Left Part
mergesort(arr , s , mid);

// Right Part
mergesort(arr , mid+1 , e);

57
// Merge the Sorted array

mergesorted(arr , s , e);

int main()
{
int arr[5] = {2 , 3 , 0 ,9 , 1};

// Merge sort
mergesort(arr , 0 , 4);

// print

for(int i = 0 ; i < 5 ; i++)


{
cout<<arr[i]<<" ";
}
}
Time Complexity for Worst Case :O(nlogn)
Time Complexity for Best Case :O(nlogn)

58
59

You might also like