Friday 6 February 2015

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

COUNTING SORT ALGORITHM IN C

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment