COUNTING SORT ALGORITHM IN C++
Structure
of the Problem Requirements
Counting
sort is an Algorithm for sorting a collection of objects according to keys that
are small integers. In counting sorting technique based on keys between a
specific range. It works by counting the number of objects having distinct key
values (kind of hashing). Then doing some arithmetic to calculate the position
of each object in the output sequence.
Source
Code
#include<stdio.h>
#include<ctype.h>
void quicksort(int [10],int,int);
int main(){
printf("\n\n\t ******** QUICK SORT ********** \n\n");
int x[40],size,i;
char st[40];
printf("Size
of Array :");
scanf("%d",&size);
fflush(stdin);
for (i=0;i<size;i++)
{
printf("\n Enter Number : ");
scanf("%c",&st[i]);
fflush(stdin);
x[i]=(int)st[i];
}
quicksort(x,0,size-1);
printf("\n\n\t\t Sorted Number : ");
for(i=0;i<size;i++)
printf(" %c",x[i]);
return 0;
}
void quicksort(int x[10],int
first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output
of the Program
No comments:
Post a Comment