Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

ALGORITHMS

Stack 
1. Stack Operations (PUSH, POP, PEEK)
2. Infix to Postfix Expression

3. Evaluation of Postfix Expression


4. Stack as Linked List
#include<iostream>
using namespace std;
class node
{
int info;
node *next;
public:
void push();
void pop();
void disp();
};
node *top;
void node::push(int val)
{

node *node1 = new node();


node1->info = val;
node1->next = NULL;
if(front ==NULL)
{
top = node1;
cout<<"\n Node :"<<val<<" Inserted Successfully as a First Node :";
}
else
{
node1->next = top;
top= node1;
cout<<"\n Node :"<<val<<" Inserted Successfully at Last :";
}
}
void node:: pop()
{
if(top== NULL)
{
cout<<"\n Stack is Underflow :";
return;
}
else
{
node *move;
move = top;
top = top->next;
cout<<"\n Deleted Node is :"<<move->info;
delete(move);
}
}

void node::disp()
{
node *move;
if(top==NULL)
{
cout<<"\n No Nodes to show :";
}
else
{
move = top;
while(move!=NULL)
{
cout<<"\t"<<move->info<<"-->";
move = move->next;
}
}
}

int main()
{
node s;
int ch;
do
{
cout<<"\n 1. Push 2. Pop 3. Disp 4. Exit ";
cin>>ch;
switch(ch)
{
case 1: int val;
cout<<"\n Enter the value to Push :";
cin>>val;
s.push(val);
break;
case 2: s.pop();
break;
case 3: s.disp();
break;

default:
cout<<"\n Enter only Choice 1 to 4 :";
}
}while(ch!=4);
}
Queue 
1. Linear Queue
Algorithm Enqueue
{
if (r == max)
print("Queue Overflow");
else {
if (f == - 1)
f = 0;
r++;
q[r]  item;
}
}
Algorithm Dequeue
{
if(f == -1)
Print(“Underflow”);
else {
if(f == r)
f = r = -1;
else
f++;
}
}
2. Circular Queue
Algorithm Enqueue
{
if (f = 0 && r== max || f = r +1)
print("Queue Overflow");
else {
if (r == max)
r = -1
r++;
q[r]  item;
if (f == - 1)
f = 0;
}
}
Algorithm Dequeue
{
if(f == -1)
Print(“Underflow”);
else {
if(f == r)
f = r = -1;
else {
if (f == max)
f = 0;
else
f++;
}
}
}

3. Queue as Linked List


#include<iostream>
using namespace std;
class node
{
int info;
node *next;
public:

void Enqueue(int);
void Dequeue();
void disp();
};

node *front=NULL;
node *rear = NULL;

void node::Enqueue(int val)


{

node *node1 = new node();


node1->info = val;
node1->next = NULL;
if(front ==NULL)
{
front = node1;
rear = node1;
cout<<"\n Node :"<<val<<" Inserted Successfully as a First Node :";
}
else
{
rear->next = node1;
rear= node1;
cout<<"\n Node :"<<val<<" Inserted Successfully at Last :";
}
}

void node:: Dequeue()


{
if(front== NULL)
{
cout<<"\n Stack is Underflow :";
return;
}
else
{
node *move;
move = front;
front = front->next;
cout<<"\n Deleted Node is :"<<move->info;
delete(move);
}
}

void node::disp()
{
node *move;
if(front==NULL)
{
cout<<"\n No Nodes to show :";
}
else
{
move = front;
while(move!=NULL)
{
cout<<"\t"<<move->info<<"-->";
move = move->next;
}
}
}

int main()
{
node s;
int ch;
do
{
cout<<"\n 1. Enqueue 2. Dequeue 3. Disp 4. Exit ";
cin>>ch;
switch(ch)
{
case 1: int val;
cout<<"\n Enter the value to Push :";
cin>>val;
s.Enqueue(val);
break;
case 2: s.Dequeue();
break;
case 3: s.disp();
break;

default:
cout<<"\n Enter only Choice 1 to 4 :";
}
} while(ch!=4);
}

Priority Queue 
1. Insert
Algorith insert(a,n)
{
i  n;
item  a[n];
while(i>1 && (a[i/2]  item))
{
a[i]  a[i/2];
i  i/2;
}
a[i]  item;
}
2. Adjust
Algoriothm Adjust(a,i,n)
{
j  2i;
item  a[i];
while (j <= n) do
{
if((j<n) and (a[j] < a[j+1]) then // For min_heap ( a[j] > a[j+1] )
j  j+1;
if(item > a[j]) then // For min_heap ( item < a[j] )
break;
a[j/2]  a[j];
j  2j;
}
a[j/2]  item;
}
3. DelMax
Algorithm DelMax(a,n.x)
{
if(n == 0) then
{
While (“Heap is empty”)
Return false;
}
x  a[1];
a[i]  a[n];
adjust (a,1,n-1);
return true;
}
4. Heapify
Algorithm Heapify(a,n)
{
for in/2 to 1 step -1 do
adjust(a,i,n);
}
5. Heap Sort
Algorithm Heap Sort(a,n)
{
Heapify(a,n);
for in to 2 step -1 do
{
t  a[i];
a[i]  a[1];
a[1]  t;
adjust(a,i,i-1);
}
}

Searching 
1. Linear Search
Algorithm linear search (a,n,val)
{
pos = -1;
for in to 1 step +1 do
{
if(a[i] == val)
return pos;
}
return pos;
}

2. Binary Search
Algorithm binary search (low,high,val)
{
while (low <= high) then
{
mid  (low + high)/2;
if(a[mid] == val)
Return mid;
else if(a[mid] < val)
low  mid + 1;
else
high  mid -1;
}
}

Sort 
1. Bubble Sort
Algorithm Bubble sort(a,n)
{
last = n;
for i1 to n -1 step +1 do
{
int exch=0;
for j1 to last - 1 step +1 do
{
if (arr[j] > arr[j + 1]) // for descending order (arr[j] < arr[j+1])
{
swap(arr[j],arr[j+1]);
exch = exch +1;
}
}
if (exch == 0)
return;
else
last = last - 1;
}
}
2. Insertion Sort
Algorithm Insertion sort (a,n)
{
for i2 to n do step +1
{
key  arr[i];
ptr  i - 1;
while (ptr >= 0 and key < arr[ptr]) do // for descending order (key > arr[ptr])
{
arr[ptr + 1]  arr[ptr];
ptr--;
}
arr[ptr + 1] = key;
}
}
3. Selection Sort
Algorithm Selection sort
{
for i1 to n -1 do step +1
{
min_index  i;
for ji+1 to n do step +1
{
if (a[min_index] > a[j]) then
min_index  j;
if(min_index != i) then
swap(a[min_index],a[j])
}
}
}
4. Merge Sort
Algorithm Merge Sort(low, high)
{
if (low < high)
{
mid  (low + high) / 2;
mergeSort (low, mid);
mergeSort (mid + 1, high);
merge (low, mid, high);
}
}
Algorithm Merge(low,mid,high)
{
i  low;
j  mid + 1;
k  low;
while (i <= mid && j <= high) {
if (a[i] <= a[j]) // for descending order (if (a[i] >= a[j]))
{
c[k++] = a[i++];
}
else {
c[k++] = a[j++];
}
}
if (i > mid) {
while (j <= high)
c[k++] = a[j++];
} else {
while (i <= mid)
c[k++] = a[i++];
}
for (k = low; k <= high; k++)
a[k] = c[k];
}
5. Quick Sort
Algorithm Quick Sort (low, high)
{
if (low < high)
{
j  partition(arr, low, high);
quickSort(arr, low, j-1);
quickSort(arr, j + 1, high);
}
}
Algorithm partition (low, high)
{
pivot  arr[low];
left  low + 1;
right  high;
while(true)
{
while ([left] <= pivot) // for descending order ( while ([left] >= pivot) )
left++;
while ([right] > pivot) // while ([right] < pivot)
right--;
if (left > right)
break;
swap(arr[left], arr[right]);
}
swap(arr[low], arr[right]);
return right;
}

You might also like