#include<stdio.h>
#include<stdlib.h>
#define max 100
void print(int arr[max],int len)
{
int i;
for(i=0;i<len;i++)
printf("%d\t",arr[i]);
printf("\n");
}
void counting_sort(int source_array[max],int destination_array[max],int upper_bound,int src_array_length)
{
int temp_buffer[max],i;
for(i=0;i<=upper_bound;i++)
{
temp_buffer[i]=0;
}
for(i=0;i<src_array_length;i++)
{
temp_buffer[source_array[i]]=temp_buffer[source_array[i]]+1;
}
for(i=1;i<=upper_bound;i++)
{
temp_buffer[i]=temp_buffer[i]+temp_buffer[i-1];
}
for(i=src_array_length-1;i>=0;i--)
{
destination_array[temp_buffer[source_array[i]]-1]=source_array[i];
temp_buffer[source_array[i]]=temp_buffer[source_array[i]]-1;
}
}
void main()
{
int arr[]={2,7,3,10,9,13,8,0,3},d_arr[max];
counting_sort(arr,d_arr,13,9);
print(arr,9);
print(d_arr,9);
system("pause");
}
0 comments:
Post a Comment