Analysis Algo
Analysis Algo
#include <bits/stdc++.h>
int N = inputArray.size();
int M = 0;
M = max(M, inputArray[i]);
countArray[inputArray[i]]++;
vector<int> outputArray(N);
outputArray[countArray[inputArray[i]] - 1]
= inputArray[i];
countArray[inputArray[i]]--;
return outputArray;
int main()
return 0;
Q1. IMPLEMENT INSERTION SORT AND REPORT THE NUMBER OF COMPARISONS.
#include<iostream>
int i, key, j;
key = arr[i];
j = i - 1;
comparisons++;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main()
int comparisons = 0;
insertionSort(arr, n, comparisons);
return 0;
}
Q2. IMPLEMENT MERGE SORT AND REPORT THE NUMBER OF COMPARISONS.
#include<iostream>
int merge(int arr[], int temp[], int left, int mid, int right) {
int i, j, k;
int comparisons = 0;
i = left;
j = mid;
k = left;
comparisons++;
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
temp[k++] = arr[i++];
temp[k++] = arr[j++];
arr[i] = temp[i];
return comparisons;
return comparisons;
int main()
int temp[n];
return 0;
#include<iostream>
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
comparisons++;
}
largest = right;
comparisons++;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, i, comparisons);
swap(arr[0], arr[i]);
heapify(arr, i, 0, comparisons);
int main() {
int comparisons = 0;
heapSort(arr, n, comparisons);
return 0;
}
Q4. IMPLEMTN RANDOMIZED QUICK SORT AND THE REPORT THE NUMBER OF COMPARISONS.
#include<iostream>
int comparisons = 0;
int i = low + 1;
comparisons++;
swap(arr[i], arr[j]);
i += 1;
return i - 1;
swap(arr[random], arr[low]);
quickSort(arr, pi + 1, high);
int main() {
return 0;
#include<iostream>
maximum = array[i];
return maximum;
int output[size];
int count[max];
count[i] = 0;
array[i] = output[i];
int i;
int main() {
RadixSort(array, n);
printArray(array, n);
Q6 SEARCHING TECHNIQUE
#include <iostream>
if(arr[i] == x) {
int main() {
int x = 10;
(result == -1) ? cout << "Element is not present in array" : cout << "Element is present at index " << result;
return 0;
#include<iostream>
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
return -1;
int main() {
int x ;
cin>>x;
return 0;
1. FIBONACCI SERIES
#include<iostream>
int fibonacci(int n) {
if (n <= 1)
return n;
else
int main() {
int n ;
return 0;
(B) FACTORIAL
#include<iostream>
int factorial(int n) {
if(n > 1)
else
return 1;
int main() {
int num;
cin>> num;
cout << "Factorial of " << num << " is " << factorial(num);
return 0;
}
Q8. STACK AND QUEUE USING LINKED LIST AND ARRAY
#include <iostream>
#define MAX_size 10
class stackNode
public:
int data;
stackNode *next;
};
class stack
stackNode *top;
int size;
public:
stack()
top = NULL;
size = 0;
bool isfull();
bool isempty();
void push(int);
void pop();
void peek();
void display();
};
bool stack::isfull()
if (size == MAX_size)
return true;
else
return false;
bool stack::isempty()
return true;
else
return false;
if (isfull())
return;
else
newNode->data = num;
newNode->next = top;
top = newNode;
size++;
void stack::pop()
if (isempty())
return;
}
else
top = top->next;
delete (ptr);
size--;
void stack::peek()
void stack::display()
ptr = ptr->next;
int main()
stack s1;
int ch;
do
switch (ch)
case 1:
int num;
s1.push(num);
break;
case 2:
s1.pop();
break;
case 3:
s1.peek();
break;
case 4:
s1.display();
break;
case 5:
break;
default:
}
STACK USING ARRAY
#include <iostream>
#define MAX_STACK_SIZE 5
class Stack
int top;
public : Stack()
top = 0;
bool isFull()
if(top==MAX_STACK_SIZE-1)
return 1;
else
return 0;
bool isEmpty()
if(top==-1)
return 1;
else
return 0;
if(isFull())
return;
else
stk[++top]=num;
int pop()
if(isEmpty())
return -1;
else
return (stk[top--]);
void peek()
void display(int i)
cout<<stk[j]<<"\t";
cout<<endl;
}
};
int main() {
Stack s;
int i=0;
int num = 0;
do{
cin>>num;
cout<<endl;
switch(num)
++i;
int val;
cin>>val;
s.push(val);
break;
break;
s.peek();
break;
s.display(i);
break;
case 5: //exit
exit(0);
}
} while(num!=5);
return 0;
#include<iostream>
#define MAX_SIZE 10
class qNode{
qNode *next;
qNode(){next=NULL;}
};
class queue
int size;
void enqueue(int);
void dequeue();
bool isfull();
bool isempty();
void peek();
void display();
};
{
qNode *newNode= new qNode;
if(isfull())
cout<<"Queue is full."<<endl;
else
rear->next = newNode;
rear =newNode;
}size++;
void queue::dequeue()
qNode *ptr;
if (isempty())
cout<<"Queue is empty."<<endl;
else
ptr =front;
front = front->next;
cout<<"Deleted :"<<ptr->data;
cout<<endl;
delete ptr;
size--;
void queue::display()
{
cout<<ptr->data<<"->";
ptr=ptr->next;
cout<<rear->data;
cout<<endl;
int main()
queue q1;
int ch;
do
cout<<"5.Exit."<<endl;
cin>>ch;
switch(ch)
cin>>num;
q1.enqueue(num);
break;
case 2: q1.dequeue();
break;
case 3:q1.peek();
break;
case 4: q1.display();
break;
}while(ch!=5);
#include <iostream>
#define MAX_SIZE 10
class queue{
int front,rear,que[MAX_SIZE];
public :
queue(){front=rear=-1;}
return true;
else
return false;
return true;
else
return false;
if (isfull())
cout<<"Queue is full."<<endl;
else{
if(front ==-1)
front =0;
que[++rear]=num;
void dequeue()
int num;
if (isempty())
cout<<"Queue is empty."<<endl;
else
num = que[front];
if (front==rear)
front=-1;
else
front++;
cout<<"Deleted element:"<<num<<endl;
void display()
cout<<que[i]<<" ";
cout<<endl;
void peek()
cout<<"Front Element:"<<que[front]<<endl;
}};
int main()
{ queue q1;
int ch;
do {
cout<<"5) Exit"<<endl;
cin>>ch;
switch (ch) {
cin >> n;
q1.enqueue(n);
break;
case 2: q1.dequeue();
break;
case 3: q1.display();
break;
case 4: q1.peek();
break;
break;
} while(ch!=5);
return 0;
#include <iostream>
class Node
public:
int data;
Node *next;
Node() { next = 0; }
};
class SLL
Node *head;
public:
void addatbeg(int);
void addatend(int);
void delfrombeg();
void delfromend();
void delfromloc(int);
void display();
};
newNode->data = num;
newNode->next = head;
head = newNode;
if (head == NULL)
head = newNode;
else
ptr = ptr->next;
ptr->next = newNode;
newNode->data = num;
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;
void SLL::delfrombeg()
if (head == NULL)
else
head = head->next;
delete (ptr);
}
void SLL::delfromend()
if (head == NULL)
else
if (head->next == NULL)
delete (head);
head = NULL;
else
ptr = ptr->next;
delete (ptr->next);
ptr->next = NULL;
if (head == NULL)
Node *ptr2;
{
ptr2 = ptr1;
ptr1 = ptr1->next;
ptr2->next = ptr1->next;
delete (ptr1);
void SLL::display()
if (head == NULL)
else
ptr = ptr->next;
int main()
SLL singlinklist;
int ch;
do
switch (ch)
case 1:
int nb;
singlinklist.addatbeg(nb);
break;
case 2:
int ne;
singlinklist.addatend(ne);
break;
case 3:
int nl;
int idx;
singlinklist.addatloc(nl, idx);
break;
case 4:
singlinklist.delfrombeg();
break;
case 5:
singlinklist.delfromend();
break;
case 6:
int i;
cin >> i;
singlinklist.delfromloc(i);
break;
case 7:
singlinklist.display();
break;
case 8:
break;
default:
#include <iostream>
class dNode
public:
int data;
dNode *prev;
dNode *next;
};
class DoublyLinkedList
public:
dNode *head;
void addatbeg(int);
void addatend(int);
void delfrombeg();
void delfromend();
void display();
};
temp->data = num;
if (head == NULL)
temp->prev = NULL;
temp->next = NULL;
head = temp;
else
temp->prev = NULL;
temp->next = head;
head->prev = temp;
head = temp;
{
dNode *temp = new dNode();
temp->data = num;
if (head == NULL)
temp->prev = NULL;
temp->next = NULL;
head = temp;
else
ptr = ptr->next;
temp->next = NULL;
temp->prev = ptr;
ptr->next = temp;
void DoublyLinkedList::delfrombeg()
dNode *ptr;
if (head == NULL)
return;
if (head->next == NULL)
delete (head);
else
{
ptr = head;
head = head->next;
head->prev = NULL;
delete (ptr);
void DoublyLinkedList::delfromend()
dNode *ptr;
if (head == NULL)
return;
if (head->next == NULL)
delete (head);
return;
else
ptr = head;
ptr = ptr1;
ptr1 = ptr1->next;
delete (ptr1);
ptr->next = NULL;
}
void DoublyLinkedList::display()
ptr = ptr->next;
int main()
DoublyLinkedList l1;
int ch;
do
switch (ch)
case 1:
int nb;
l1.addatbeg(nb);
break;
case 2:
int ne;
l1.addatend(ne);
break;
case 3:
l1.delfrombeg();
break;
case 4:
l1.delfromend();
break;
case 5:
l1.display();
break;
case 6:
break;
default:
#include<iostream>
using namespace std;
struct Node {
int data;
Node *next;
};
newNode->data = data;
newNode->next = *head_ref;
if (*head_ref != NULL) {
temp = temp->next;
temp->next = newNode;
} else
newNode->next = newNode;
*head_ref = newNode;
if (*head_ref == NULL)
return;
if (curr->next == *head_ref) {
cout << "The node " << key << " is not in the list.\n";
return;
prev = curr;
curr = curr->next;
if (curr->next != *head_ref)
*head_ref = curr->next;
prev->next = curr->next;
delete curr;
if (head != NULL) {
do {
temp = temp->next;
int main() {
while(1) {
switch(choice) {
case 1:
addNode(&head, data);
break;
case 2:
deleteNode(&head, data);
break;
case 3:
displayList(head);
break;
case 4:
exit(0);
default:
return 0;
#include<iostream>
struct Node {
int data;
Node* left;
Node* right;
};
if (!newNode) {
return NULL;
newNode->data = data;
return newNode;
root = createNode(data);
return root;
else
return root;
if (temp == NULL)
return;
inorder(temp->left);
inorder(temp->right);
if (temp == NULL)
return;
preorder(temp->left);
preorder(temp->right);
if (temp == NULL)
return;
postorder(temp->left);
postorder(temp->right);
int main() {
while(1) {
switch(choice) {
case 1:
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default:
return 0;
}