Professional Documents
Culture Documents
Q1). WAP to insert new element at given index number in the array.
/*************************************************************
/*************************************************************
#include<iostream>
int main()
cin>>to;
cin>>arr[i];
cin>>element;
cin>>position;
arr[i] = arr[i-1];
arr[i] = element;
to++;
cout<<arr[i]<<" ";
cout<<endl;
return 0;
Q2) WAP to implement the linear search. Use function concept, if element is found then
return index number of element otherwise return -1;
/*************************************************************
/*************************************************************
#include<iostream>
if (arr[i] == x)
return i;
return -1;
int main()
int x =46;
(result == -1)
return 0;
/*************************************************************
#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?
/*************************************************************
/*************************************************************
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the Size of the Array : ";
cin>>n;
int m = arr[0];
34
for(int i = 0 ; i < n ; i++)
{
if (arr[i] > m )
{
m = arr[i];
}
}
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.
/***********************************************************
/*************************************************************
#include <stdio.h>
#include <stdlib.h>
int main()
scanf("%d", &num);
int t;
37
t = arr[i];
arr[i] = arr[num-1-i];
arr[num-1-i] = t;
return 0;
/*************************************************************
/*************************************************************
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char s[1000];
fr[s[i] - '0']++;
return 0;
/*************************************************************
/*************************************************************
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
39
int sum = 0;
sum += marks[i];
return sum;
int main() {
int number_of_students;
char gender;
int sum;
scanf("%d", &number_of_students);
printf("%d", sum);
free(marks);
return 0;
40
}
/*************************************************************
/*************************************************************
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int N;
int d;
int A[N];
scanf("%d", &A[i]);
{
41
printf("%d ", A[(i + d) % N]);
puts("");
return 0;
42
LAB-5
Q1) Write a program to implement binary search algorithm. Assume user will enter the
sorted array.
/*************************************************************
/*************************************************************
#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;
}
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.
/*************************************************************
/*************************************************************
#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;
}
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.
/*************************************************************
/*************************************************************
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, m;
int arr[n][m];
int maxi = n*m;
int trp_row = 1;
}
}
}
}
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.
/*************************************************************
/*************************************************************
47
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the Array size : ";
cin>>n;
int m = arr[0];
for(int i = 0 ; i < n ; i++)
{
if (m < arr[i])
{
m = arr[i];
}
}
int k = 0;
for(int i = 0 ; i < m ; i++)
48
{
if (temp[k] < temp[i])
{
k = i;
}
}
}
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).
/*************************************************************
/*************************************************************
#include<iostream>
int* Rdm(int n)
for(int x=0;x<n;x++)
p[x]=rand()%100;
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)
{
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.
/*************************************************************
/*************************************************************
#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;
}
Q4) WAP to implement the insertion sort and show the output of each pass.
/*************************************************************
/*************************************************************
#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;
}
Q5) WAP to implement the quick sort and show the output of each pass.
/*************************************************************
/*************************************************************
#include <iostream>
#include "LAB6_Q1.h"
using namespace std;
int *rnd(int);
int n = 10;
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.
/*************************************************************
/*************************************************************
#include<bits/stdc++.h>
using namespace std;
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++];
}
}
// 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);
58
59