Our team came with the idea to solve the problem statement of students.So we solve a problem called as Quick Sort which is the part of Data structure."Quick Sort is basically used to sort an array of n elements.In this process we consider a key element in the initial stage and we imposed some instruction so that the new array create will place the key element to such a location so that at the left side of key elements all the elements has less value than key element and on the right side of the key element all the elements has high value than the key element.The process continues until the array will be sort.Key element is the leftmost index of the array." In this blog we create a program to resolve the problem of students with the help of recursive functions.We use a Random function in our program so that we don't need to give input through keyboard that is compiler itself insert the inputs from buffer memory.So here you are guys.....Hope you like it..
If you like the program then please like our channel by clicking the link below and if you had any problem then you can ask our team anytime.
programmerix@gmail.com
Facebook_Page youtube
<PROGRAM/CODE>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int part(int[],int,int);
void input(int a[],int n)
{
int i;
//cout<<"\nEnter array elements:"<<endl;
randomize();
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
cout<<"\nArray is \n";
for(i=0;i<n;i++)
{
cout<<" "<<a[i];
}
}
void quick(int a[],int low,int high)
{
int j;
if(low<high)
{
j=part(a,low,high);
quick(a,low,j-1);
quick(a,j+1,high);
}
}
int part(int a[],int low,int high)
{
int v,i,j,temp;
v=a[low];
i=low;
j=high+1;
do
{
do
{
i++;
}while(a[i]<v);
do
{
j--;
}while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[low]=a[j];
a[j]=v;
return(j);
}
void display(int a[],int n)
{
int i;
cout<<"\nArray after sorting: \n\t\t ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
void main()
{
int a[50],n,i,low,high;
clrscr();
cout<<"How many elements \n";
cin>>n;
input(a,n);
low=0;
high=n-1;
quick(a,low,high);
display(a,n);
getch();
}
Comments
Post a Comment