0% found this document useful (0 votes)
43 views31 pages

Arpit Ds Lab 03-06

The document discusses four questions related to arrays and searching algorithms. Question 1 discusses inserting a new element into an array at a given index. Question 2 implements linear search using a function. Question 3 deletes an element from an array using search and shifting elements. Question 4 checks for duplicate elements in an array.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views31 pages

Arpit Ds Lab 03-06

The document discusses four questions related to arrays and searching algorithms. Question 1 discusses inserting a new element into an array at a given index. Question 2 implements linear search using a function. Question 3 deletes an element from an array using search and shifting elements. Question 4 checks for duplicate elements in an array.

Uploaded by

Hemant Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

LAB-3

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

//This program is developed by Arpit Awasthi (221B086)

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

6)
#include<iostream>

08
using namespace std;

int main()

1B
{

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

22
cout<<"Enter the Size of array: ";
i(
cin>>to;

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


th

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


as

cin>>arr[i];
w

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


tA

cin>>element;

cout<<"which position ? ";


pi

cin>>position;
Ar

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;

6)
}

Time Complexity for Worst Case :O(n)

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

using namespace std;


as

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


w

{
tA

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

if (arr[i] == x)
pi

return i;
Ar

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;

6)
return 0;

08
}

Time Complexity for Worst Case :O(n)

1B
Time Complexity for Best Case :O(1)

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

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

//This program is developed by Arpit Awasthi (221B086)


as

/*************************************************************
#include <iostream>
w

using namespace std;


tA

int linear_search(int *a,int x,int i)


{
if (i==5)
pi

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

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


as

{
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

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


cin>>n;
pi

int * arr = new int[n];


Ar

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

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

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


int a = arr[i];
pi

check[a]++;
}
Ar

//Checking for duplicates


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

6)
}

08
if(k)

1B
cout<<"NO duplicates found";
else

22
cout<<"Duplicates Found";
i(
return 0;
}
th

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(1)
as

{
w
tA
pi
Ar
6)
08
1B
22
i(
th

36
as
w

LAB-4
tA

Q1) Write a program to reverse the elements of an array.


pi

/***********************************************************
Ar

//This program is developed by Arpit Awasthi (221B086)

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

#include <stdio.h>

#include <stdlib.h>
int main()

int num, *arr, i;

scanf("%d", &num);

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

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

scanf("%d", arr + 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

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


Ar

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 Arpit Awasthi (221B086)

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

6)
#include <stdio.h>

08
#include <string.h>

#include <math.h>

1B
#include <stdlib.h>

int main() { 22
i(
th

char s[1000];

int fr[10] = {0};


as

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

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


tA

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

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

}
Ar

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

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>

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


th
as

39
int sum = 0;
w

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

sum += marks[i];
tA

}
pi

return sum;
Ar

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++) {

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

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


pi

times.
Ar

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

//This program is developed by Arpit Awasthi (221B086)

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

#include <stdio.h>

#include <string.h>
#include <math.h>

#include <stdlib.h>

int main() {

6)
int N;

08
int d;

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

1B
int A[N];

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

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

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


as

{
w

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

puts("");
pi

return 0;
Ar

Time Complexity for Worst Case :O(n)


Time Complexity for Best Case :O(n)
6)
08
1B
22
i(
th
as
w
tA

42

LAB-5
pi

Q1) Write a program to implement binary search algorithm. Assume user will enter the
sorted array.
Ar

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

//This program is developed by Arpit Awasthi (221B086)

/*************************************************************
#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

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


cin>>arr[i];
n = sizeof(arr) / sizeof(arr[0]);
pi

43
int key;
Ar

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 Arpit Awasthi (221B086)

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

return maximum(arr, mid + 1, e);


}
else{
w

return maximum(arr, s, mid - 1);


tA

}
}
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;
}

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 Arpit Awasthi (221B086)

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

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


cin>>m;
as

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

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


tA

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


{
pi

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


{
Ar

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++;
}
}

6)
cout<<endl;
}

08
if(count1 > (maxi/2))
{

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

trp_size = (n*m) - count1 ;

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 i= 0 ; i < n ; i++)


tA

{
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++;

}
}
}

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


{
for(int j = 0 ; j < 3 ; j++)
{
cout<<trp[i][j]<<" ";
}

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

Time Complexity for Worst Case :O(nm)


tA

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
pi

coming three times.


Ar

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

//This program is developed by Arpit Awasthi (221B086)

/*************************************************************
47
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the Array size : ";
cin>>n;

int *arr = new int[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

int *temp = new int[m];

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


w

{
temp[i] = 0;
tA

}
pi

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


Ar

{
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";

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

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

//This program is developed by Arpit Awasthi (221B086)

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;

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 Arpit Awasthi (221B086)

/*************************************************************
#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
}

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


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

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

//This program is developed by Arpit Awasthi (221B086)


tA

/*************************************************************
#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<<"\nSorted Array : "<<endl;


for(int x=0;x<size;x++)
Ar

{
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

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


as

for(int x=0;x<size;x++)
{
cout<<a[x]<<" ";
}
w

return 0;
}
tA

Time Complexity for Worst Case :O(n^2)


Time Complexity for Best Case :O(n)
pi

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

pass.

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

//This program is developed by Arpit Awasthi (221B086)

/*************************************************************
#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]);

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

quickSort(arr, pivotIndex + 1, high);


cout << "Pass: ";
55
i(
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
th

}
cout << endl;
}
as

int main() {
w

int *arr = rnd(10);


int n = 10;
tA

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.
/*************************************************************

//This program is developed by Arpit Awasthi (221B086)

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

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

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

if (first[index1] < second[index2]) {


arr[index++] = first[index1++]; }
Ar

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

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

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

6)
08
1B
22
i(
58
59
th
as
w
tA
pi
Ar

You might also like