//Quick Sort Algorithm
#include<iostream>
using namespace std;
#include<conio.h>
//Function for partitioning the array
int Partition(int low,int high,int arr[])
{
int i,high_var=high,low_var=low,pivot,temp;
pivot=arr[low];
while(low<high)
{
while(arr[low_var]<pivot)
{
low_var++;
}
while(arr[high_var]>pivot)
{
high_var--;
}
if(low_var<high_var)
{
temp=arr[low_var];
arr[low_var]=arr[high_var];
arr[high_var]=temp;
}
else
{
temp=pivot;
pivot=arr[low_var];
arr[low_var]=temp;
break;
}
}
return high_var;
}
void Quick_sort(int low,int high,int arr[])
{
int Piv_index,i;
if(low<high)
{
Piv_index=Partition(low,high,arr);
Quick_sort(low,Piv_index-1,arr);
Quick_sort(Piv_index+1,high,arr);
}
}
int main()
{
int a[100],n,low,high,i;
cout<<"Enter number of elements:";
cin>>n;
//a=new int[n];
cout<<"enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
//for(i=0;i<n;i++)
//a[i]=rand()%100;
cout<<"\nInitial Order of elements:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
high=n-1;
low=0;
Quick_sort(low,high,a);
cout<<"\nFinal Array After Sorting:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
_getch();
return 0;
}
//c++ code
#include<iostream>
using namespace std;
#include<conio.h>
#include<process.h>
template<class T>
class Queue
{
private:
int front,rear;
T *queue;
int maxsize;
public:
Queue(int maxqueuesize)
{
front=0;
rear=-1;
maxsize=maxqueuesize;
queue=new T[maxsize];
}
~Queue()
{
delete[] queue;
}
int isempty();
int isfull();
void insertion();
void deletion();
void _front();
void _rear();
void display();
};
template<class T>
int Queue<T>::isempty()
{
if(front==0&&rear==-1)
return 1;
else
return 0;
}
template<class T>
int Queue<T>::isfull()
{
if(rear==maxsize-1)
return 1;
else
return 0;
}
template<class T>
void Queue<T>::_front()
{
if(isempty())
cout<<"\n queue is empty.............";
else
cout<<"\n front element of the queue is:"<<queue[front];
}
template<class T>
void Queue<T>::_rear()
{
if(isempty())
cout<<"\n queue is empty.............";
else
cout<<"\n rear element of the queue is:"<<queue[rear];
}
template<class T>
void Queue<T>::insertion()
{
T ele;
if(isfull())
cout<<"\n queue is full........";
else
{
cout<<"\n enter the element do u want to insert :";
cin>>ele;
queue[++rear]=ele;
}
}
template<class T>
void Queue<T>::deletion()
{
if(isempty())
cout<<"\n queue is empty........";
else
cout<<"\n the deleted element of the queue is :"<<queue[front++];
}
template<class T>
void Queue<T>::display()
{
if(isempty())
cout<<"\n queue is empty.........";
else
{
cout<<"\n queue elements are :";
for(int i=front;i<=rear;i++)
{
cout<<"\t"<<queue[i];
}
}
}
int main()
{
int ch;
Queue<int> q(10);
do
{
cout<<"\n 1.insertion \n 2.deletion \n 3.front \n 4.rear \n 5.display \n 6.exit \n";
cout<<"enter ur choice:";
cin>>ch;
switch(ch)
{
case 1:
q.insertion();
break;
case 2:
q.deletion();
break;
case 3:
q._front();
break;
case 4:
q._rear();
break;
case 5:
q.display();
break;
case 6:
_exit(0);
}
}while(ch<=6);
_getch();
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
template<class T>
class evaluation
{
private:
T postfix[20];
int stack[20],top;
public:
evaluation()
{
top=-1;
}
void getpostfixexpr();
void calculate();
void display();
};
template<class T>
void evaluation<T>::getpostfixexpr()
{
cout<<"\n enter the postfix expression :";
cin>>postfix;
}
template<class T>
void evaluation<T>::display()
{
cout<<"\n the calculate value is :"<<stack[top];
}
template<class T>
void evaluation<T>::calculate()
{
int l=strlen(postfix);
int k=l;
for(int i=0;i<k;i++)
{
int x;
if((postfix[i]>='a'&&postfix[i]<='z')||(postfix[i]>='A'&&postfix[i]<='Z'))
{
cout<<"\n enter the value of "<<postfix[i]<<":";
cin>>x;
stack[++top]=x;
}
else
{
int a=stack[top];
int b=stack[top-1];
switch(postfix[i])
{
case '+':
stack[top-1]=b+a;
top--;
break;
case '-':
stack[top-1]=b-a;
top--;
break;
case '*':
stack[top-1]=b*a;
top--;
break;
case '/':
stack[top-1]=b/a;
top--;
break;
}
}
}
}
int main()
{
evaluation<char> e;
e.getpostfixexpr();
e.calculate();
e.display();
_getch();
return 0;
}
#include<iostream>
using namespace std;
#include<conio.h>
template<class T>
void m_sort(T numbers[],T temp[],int left,int right);
template<class T>
void mergesort(T numbers[],T temp[],int array_size)
{
m_sort<T>(numbers,temp,0,array_size-1);
}
template<class T>
//void merge(T numbers[],T temp[],int left,int mid,int right);
//template<class T>
void m_sort(T numbers[],T temp[],int left,int right)
{
int mid;
if(right>left)
{
mid=(right+left)/2;
m_sort(numbers,temp,left,mid);
m_sort(numbers,temp,mid+1,right);
merge(numbers,temp,left,mid+1,right);
}
}
template<class T>
void merge(T numbers[],T temp[],int left,int mid,int right)
{
int i,left_end,num_elements,temp_pos;
left_end=mid-1;
temp_pos=left;
num_elements=right-left+1;
while((left<=left_end)&&(mid<=right))
{
if(numbers[left]<=numbers[mid])//(kept -numbers instead )
{
temp[temp_pos]=numbers[left];
temp_pos++;
left++;
}
else
{
temp[temp_pos]=numbers[mid];
temp_pos++;
mid++;
}
}
while(left<=left_end)
{
temp[temp_pos]=numbers[left];
left++;
temp_pos++;
}
while(mid<=right)
{
temp[temp_pos]=numbers[mid];
mid++;
temp_pos++;
}
for(i=0;i<=num_elements;i++)
{
numbers[right]=temp[right];
right--;
}
}
int main()
{
int a[20],b[20],n;
cout<<"\n enter the value of n:";
cin>>n;
cout<<"\nenter n elements:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
mergesort(a,b,n);
cout<<"\n sorted elements are :";
for(int i=0;i<n;i++)
{
cout<<"\t"<<a[i]<<"\t";
}
_getch();
return 0;
}
//insertion sort
#include<iostream>
using namespace std;
#include<conio.h>
template<class T>
void insertionsort(T a[],int n)
{
for(int i=1;i<n;i++)
{
T temp=a[i];
int j;
for(j=i-1;j>=0&&temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}
int main()
{
int a[50],i,n;
cout<<"enter number of elements do u want to sort :";
cin>>n;
cout<<"enter elements to sort:";
for(i=0;i<n;i++)
{
cin>>a[i];
}
insertionsort(a,n);
cout<<"after sorting the elements are....";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
_getch();
return 0;
}