an improved counting sort for handling negative integers, also take an easy performance compare
2012-06-29 09:02
423 查看
#include<stdio.h>
[code]#include<stdlib.h>
#defineMAX_LENGTH9999
voidprint_array(constinta[],intsize)
{
inti=0;
for(i=0;i<size;i++)
{
printf("%d,",a[i]);
}
printf("\n");
}
/*
assumea[i]>0
*/
voidcounting_sort(constinta[],intb[],intsize,intmaxvalue)
{
inti=0;
for(i=0;i<size;i++)
{
b[i]=0;
}
inttemp[maxvalue+1];
for(i=0;i<=maxvalue;i++)
{
temp[i]=0;
}
for(i=0;i<size;i++)
{
temp[a[i]]+=1;
}
for(i=1;i<=maxvalue;i++)
{
temp[i]=temp[i]+temp[i-1];
}
for(i=size-1;i>=0;i--)
{
b[temp[a[i]]-1]=a[i];
temp[a[i]]=temp[a[i]]-1;
}
}
/*
handlea[i]<0
*/
voidcounting_sort2(constinta[],intb[],intsize,intmaxvalue)
{
inti=0;
intcopyofa[size];
intmin=0;
for(i=0;i<size;i++)
{
b[i]=0;
if(min>a[i])
{
min=a[i];
}
}
intnewmaxvalue=maxvalue-min;
for(i=0;i<size;i++)
{
copyofa[i]=a[i]-min;
}
inttemp[newmaxvalue+1];
for(i=0;i<=newmaxvalue;i++)
{
temp[i]=0;
}
for(i=0;i<size;i++)
{
temp[copyofa[i]]+=1;
}
for(i=1;i<=newmaxvalue;i++)
{
temp[i]=temp[i]+temp[i-1];
}
for(i=size-1;i>=0;i--)
{
b[temp[copyofa[i]]-1]=copyofa[i];
temp[copyofa[i]]=temp[copyofa[i]]-1;
}
for(i=0;i<size;i++)
{
b[i]=b[i]+min;
}
}
voidbubble_sort(constinta[],intb[],intsize)
{
inti=0;
intj=0;
inttemp;
for(i=0;i<size;i++)
{
b[i]=a[i];
}
for(i=0;i<size;i++)
{
for(j=i;j<size;j++)
{
if(b[j]<b[i])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
}
[/code]
thesecondcountingmethodisimprovedforhandingnegativeintegers.
thefirstisO(n+k),thesecondistwice:O(2n+2k).
writeatestprogramtocomparethesethreesubroutines,
inta[MAX_LENGTH];
[code]intb[MAX_LENGTH];
inti=0;
for(i=0;i<MAX_LENGTH;i++)
{
a[i]=rand();
b[i]=0;
}
for(i=0;i<1000;i++)
{
bubble_sort(a,b,MAX_LENGTH);
counting_sort2(a,b,MAX_LENGTH,RAND_MAX);
counting_sort(a,b,MAX_LENGTH,RAND_MAX);
}
[/code]
theresultasfollow:
wecanseeit’sveryobviousthataO(n2)algorithmisheavyslowerthanO(n+k).tosort9999randomintegersitwillcost195.5mseachtimesortingusebubblebutonly0.17msbyusingcountingmethod(0.35forthesecondcoutningsort,becauseitsrunningtimeisO(2n+2k))
相关文章推荐
- Given an array of positive and negative integers find the first subarray with zero sum
- How do I sort an integer array while also keeping identical elements apart from each other?
- An easy way to customize a scrollbar for a control
- git pull 或者执行一些其他git操作 Auto packing the repository for optimum performance. You may also run "git gc
- git操作提示 Auto packing the repository for optimum performance. You may also run "git gc" manually. See
- Sort an Array Mixed With Integers and Strings - Ruby
- An adaptive weighting parameter selection for improved integrated active contour model
- An improved BKW for LWE
- Given an array of integers, sort the array according to frequency of elements
- HDU 4729 An Easy Problem for Elfness(主席树)(2013 ACM/ICPC Asia Regional Chengdu Online)
- HDU 4729 An Easy Problem for Elfness 主席树
- 【主席树】 HDOJ 4729 An Easy Problem for Elfness
- one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
- An easy-to-use URL file downloader class for .NET
- HowTo compile native C codes to a library for iOS development in Xcode – take Mosquitto as an exampl
- HDU 4729 An Easy Problem for Elfness(树上主席树+LCA+二分)
- An Easy Problem for Elfness
- How to sort an array of hashes into hashes with multiple values for a key?