0% found this document useful (0 votes)
24 views24 pages

DAA File

Uploaded by

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

DAA File

Uploaded by

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

22U03020 Aditya Pandey

INDIAN INSTITUTE OF INFORMATION


TECHNOLOGY, BHOPAL (IIIT-BHOPAL)
Department of Information Technology

DESIGN AND ANALYSIS OF ALGORITHM


(IT - 215)
IIIrd - Semester

Submitted to: Submitted By:


Dr. Dharmendra Dangi Aditya Pandey
Senior Professor, IT 22U03020

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

Finding the inverse of a


given matrix.

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

Q. Find the output of the given functions.


(a)

#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

(b) Column Major


Solution:
//Column 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 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 cofac(int arr[m][n], int a, int b) {


int x;
int sign = (a + b) % 2 == 0 ? 1 : -1;

int cofac_Matrix[m - 1][n - 1];


int cofac_Row = 0, cofac_Col = 0;

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


for (int j = 0; j < n; j++) {
if (a != i && b != j) {
cofac_Matrix[cofac_Row][cofac_Col++] = arr[i][j];
if (cofac_Col == n - 1) {
cofac_Col = 0;
cofac_Row++;
}
}
}
}

x = sign * (cofac_Matrix[0][0] * cofac_Matrix[1][1] - cofac_Matrix[1][0]


* cofac_Matrix[0][1]);
return x;
}

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

Q1. Write a program to implement stack using queue.


Solution:
#include <bits/stdc++.h>
using namespace std;
class Queue{
stack<int> a;
stack<int> b;
public:

void enqueue(int x){


a.push(x);
}

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

cout<<"Enter number of elements for dequeue: ";


cin>>k;
for(int i=0;i<k;i++)
{
cout<<q.deque()<<endl;
}

return 0;
}

Output:

Queue use =2

Time complexity: -

Push = O(1)

Pop = O(n)

10 | P a g e
22U03020 Aditya Pandey

Q2. Write a program to implement queue using stack.


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

class Stack{

queue<int> a;
queue<int> b;
public:

void push(int x){


a.push(x);
}

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

Q. Write a program to implement the following:


(a) Selection Sort
Solution:
#include<bits/stdc++.h>
using namespace std;

void selection_sort(int arr[], int n) {


// selection sort
for (int i=0; i<n-1; i++) {
int mini = i;
for (int j=i+1; j<n; j++) {
if (arr[j]<arr[mini]) {
mini = j;
}
}
int temp=arr[mini];
arr[mini]=arr[i];
arr[i]=temp;
}

cout << "After selection sort: " << "\n";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";

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

Space Complexity: O(1)

14 | P a g e
22U03020 Aditya Pandey

(b) Bubble Sort


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

void bubble_sort(int arr[], int n) {


// bubble sort
for (int i = n - 1; i >= 0; i--) {
int didSwap = 0;
for (int j = 0; j <= i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
didSwap = 1;
}
}
if (didSwap == 0) {
break;
}
}

cout << "After Using bubble sort: " << "\n";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}

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 Using Bubble Sort: " << endl;


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

15 | P a g e
22U03020 Aditya Pandey

cout << endl;

bubble_sort(arr, n);
return 0;
}

Output:

Time Complexity:
Best Case: O(n)
Average Case: O(n2)
Worst Case: O(n2)

Space Complexity: O(1)

16 | P a g e
22U03020 Aditya Pandey

(c) Insertion Sort


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

void insertion_sort(int arr[], int n) {


for (int i = 0; i <= n - 1; i++) {
int j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
int temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
j--;
}
}

cout << "After Using insertion sort: " << "\n";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}

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 Using insertion Sort: " << endl;


for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;

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)

Space Complexity: O(1)

18 | P a g e
22U03020 Aditya Pandey

Assignment-5

Q. Write a program to implement the merge sort using recursion.


Solution:

#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:

Time Complexity: O(nlogn)


Space Complexity: O(n)

20 | P a g e
22U03020 Aditya Pandey

Assignment-6

Q. Write a program to implement the following:


(a) Quick Search
Solution:

# 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:

Time Complexity: O(n)

22 | P a g e
22U03020 Aditya Pandey

(b) Quick Sort


Solution:

#include <iostream>
using namespace std;
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);
quickSort(arr, pivotIndex + 1, high);
}
}

int main() {
int arr[] = {10, 4, 12, 8, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";


for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout<< endl;

quickSort(arr, 0, n - 1);

cout << "Sorted array: ";


for (int i = 0; i < n; ++i) {
cout << arr[i] << " ";
}
cout << endl;

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

Time Complexity: O(n*logn)

24 | P a g e

You might also like