DAA File
DAA File
1|Page
22U03020 Aditya Pandey
INDEX
S. Experiment Page Date of Date of Remark
No. Experiment Submission
1. Assignment 1: 3-4
Finding the output of given
functions.
2. Assignment 2: 3-8
Printing a given matrix in
(a) Row Major and (b)
Column Major
3. Assignment 3: 8-12
Implement stack using
queue.
Implement queue using
stack.
4. Assignment 4: 13-19
Program to implement
-Selection Sort
-Bubble Sort
-Insertion Sort
5. Assignment 5: 19-24
Implement merge sort
using recursion.
2|Page
22U03020 Aditya Pandey
Assignment-1
#include<bits/stdc++.h>
using namespace std;
int R()
{
static int a=5;
cout<<a<<endl;
return --a;
}
int main()
{
for(R();R();R())
{
R();
}
return 0;
}
Solution:
Output:
3|Page
22U03020 Aditya Pandey
(b)
#include<bits/stdc++.h>
using namespace std;
int R()
{
static int a=5;
cout<<a<<" ";
return a--;
}
int main()
{
for(R();R();R())
{
R();
}
return 0;
}
Solution:
Output:
4|Page
22U03020 Aditya Pandey
Assignment-2
Q1. Write a program taking a (nxn) matrix as input and printing in the following
formats:
(a) Row Major
Solution:
//Row Major
#include<bits/stdc++.h>
using namespace std;
int main()
{
//User input for Rows and Columns
int m,n;
cout<<"Enter the rows and column respectively: ";
cin>>m>>n;
int arr[m][n];
//Input for elemnts of the array
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
//Output of array in row major format
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Output:
5|Page
22U03020 Aditya Pandey
int main()
{
//User input for Rows and Columns
int m,n;
cout<<"Enter the rows and column respectively: ";
cin>>m>>n;
int arr[m][n];
//Input for elemnts of the array
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
//Output of array in column major format
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
cout<<arr[j][i]<<" ";
}
cout<<endl;
}
return 0;
}
Output:
6|Page
22U03020 Aditya Pandey
Q2. Write a program taking a 3x3 matrix as input and find the inverse of the matrix.
Solution:
#include <bits/stdc++.h>
using namespace std;
const int m=3;
const int n=3;
int main(){
int arr[m][n];
cout<<"Enter the matrix: ";
for(int i=0;i<3;i++){
7|Page
22U03020 Aditya Pandey
for(int j=0;j<3;j++){
cin>>arr[i][j];
}
}
int inv[m][n];
int det=cofac(arr,0,0)+cofac(arr,0,1)+cofac(arr,0,2);
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
if (det==0){
cout<<"Matrix is Singular, Inverse can't be found."<<endl;
}
else{
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
inv[i][j]=cofac(arr,i,j);
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
inv[i][j]=inv[i][j]/det;
}
}
cout<<"Inverse of Matrix"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<" "<<inv[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}
Output:
8|Page
22U03020 Aditya Pandey
Assignment-3
int deque(){
int x=-1;
while(!a.empty()){
b.push(a.top());
a.pop();
}
x=b.top();
b.pop();
while(!b.empty()){
a.push(b.top());
b.pop();
}
return x;
}
};
int main()
{
Queue q;
int n;
cout<<"Enter the number of elements for enqueue: ";
cin>>n;
for(int i=0;i<n;i++)
{
int m;
cout<<"Enter element: ";
cin>>m;
q.enqueue(m);
}
int k;
9|Page
22U03020 Aditya Pandey
return 0;
}
Output:
Queue use =2
Time complexity: -
Push = O(1)
Pop = O(n)
10 | P a g e
22U03020 Aditya Pandey
class Stack{
queue<int> a;
queue<int> b;
public:
int pop(){
int x=-1;
while(!a.empty()){
b.push(a.front());
a.pop();
if(a.size()==1){
x=a.front();
a.pop();
break;
}
}
while(!b.empty()){
a.push(b.front());
b.pop();
}
return x;
}
};
int main()
{
Stack q;
int n;
cout<<"Enter the number of elements for pushing in stack: ";
cin>>n;
for(int i=0;i<n;i++)
{
int m;
cout<<"Enter element: ";
cin>>m;
q.push(m);
11 | P a g e
22U03020 Aditya Pandey
}
int k;
cout<<"Enter number of elements for popping: ";
cin>>k;
for(int i=0;i<k;i++)
{
cout<<q.pop()<<endl;
}
return 0;
}
Output:
Stack use =2
Time complexity: -
Enqueue = O(1)
Dequeue = O(n)
12 | P a g e
22U03020 Aditya Pandey
Assignment-4
}
int main() {
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
for (int i=0; i<n; i++)
{
cin>>arr[i];
}
cout << "Before selection sort: " << "\n";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";
selection_sort(arr, n);
return 0;
13 | P a g e
22U03020 Aditya Pandey
Output:
Time Complexity:
Best Case: O(n2)
Average Case: O(n2)
Worst Case: O(n2)
14 | P a g e
22U03020 Aditya Pandey
int main()
{
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
for (int i=0; i<n; i++)
{
cin>>arr[i];
}
15 | P a g e
22U03020 Aditya Pandey
bubble_sort(arr, n);
return 0;
}
Output:
Time Complexity:
Best Case: O(n)
Average Case: O(n2)
Worst Case: O(n2)
16 | P a g e
22U03020 Aditya Pandey
int main()
{
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
for (int i=0; i<n; i++)
{
cin>>arr[i];
}
insertion_sort(arr, n);
return 0;
}
17 | P a g e
22U03020 Aditya Pandey
Output:
Time Complexity:
Best Case: O(n)
Average Case: O(n2)
Worst Case: O(n2)
18 | P a g e
22U03020 Aditya Pandey
Assignment-5
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1];
int rightArr[n2];
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int i = 0; i < n2; i++) {
rightArr[i] = arr[mid + 1 + i];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k++] = leftArr[i++];
} else {
arr[k++] = rightArr[j++];
}
}
while (i < n1) {
arr[k++] = leftArr[i++];
}
while (j < n2) {
arr[k++] = rightArr[j++];
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main(){
int n;
cout << "enter size of array " << endl;
cin >> n;
19 | P a g e
22U03020 Aditya Pandey
int a[n];
int i=0;
while(i<n){
cout << "enter element" << endl;
cin >> a[i];
i++;
}
mergeSort(a,0,n-1);
int j=0;
while(j<n){
cout << "the sorted element : " << a[j] << endl;
j++;
}
return 0;
}
Output:
20 | P a g e
22U03020 Aditya Pandey
Assignment-6
# include <bits/stdc++.h>
using namespace std;
int partition(int arr[], int l, int r)
{
int x = arr[r], i = l;
for (int j = l; j <= r - 1; j++) {
if (arr[j] <= x) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i], arr[r]);
return i;
}
int kthSmallest(int arr[], int l, int r, int k) {
if (k > 0 && k <= r - l + 1) {
int index = partition(arr, l, r);
if (index - l == k - 1)
return arr[index];
else if (index - l > k - 1)
return kthSmallest(arr, l, index - 1, k);
return kthSmallest(arr, index + 1, r, k - index + l - 1);
}
return INT_MAX;
}
int main()
{
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
for (int i=0; i<n; i++)
{
cin>>arr[i];
}
int k;
cout<<"Enter value of k: ";
21 | P a g e
22U03020 Aditya Pandey
cin>>k;
cout << "K-th smallest element is "
<< kthSmallest(arr, 0, n - 1, k);
return 0;
}
Output:
22 | P a g e
22U03020 Aditya Pandey
#include <iostream>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
int main() {
int arr[] = {10, 4, 12, 8, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
23 | P a g e
22U03020 Aditya Pandey
Output:
Original array: 12 11 13 5 6 7
Sorted array: 5 6 7 11 12 13
24 | P a g e