<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JIYUNALEX &#187; bucket sort</title>
	<atom:link href="http://www.jiyunalex.com/blog/tag/bucket-sort/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jiyunalex.com/blog</link>
	<description>不登高山，不知天之高也；不臨深谷，不知地之厚也；不聞先王遺言，不知學問之大也</description>
	<lastBuildDate>Tue, 31 Jan 2012 07:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>介绍一下Bucket Sort</title>
		<link>http://www.jiyunalex.com/blog/2009/05/12/%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8bbucket-sort/</link>
		<comments>http://www.jiyunalex.com/blog/2009/05/12/%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8bbucket-sort/#comments</comments>
		<pubDate>Tue, 12 May 2009 20:47:00 +0000</pubDate>
		<dc:creator>yukialex</dc:creator>
				<category><![CDATA[Alex's]]></category>
		<category><![CDATA[编程]]></category>
		<category><![CDATA[bucket sort]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">/alex/post/e4bb8be7bb8de4b880e4b88bBucket-Sort.aspx</guid>
		<description><![CDATA[排序算法是大家刚学编程的时候一定要学的算法，冒泡算法(bubble sort)，快速排序(quick sort)等等相信大家耳熟能详。我下面要介绍的bucket sort其实大家应该也学过，不过最近刚刚帮别人交了一份作业，勾起了不少回忆，所以顺便在这里发出来。下面的算法有几个限制：所排序的一定要是正整数，至于整数的大小和个数都是可以修改的。 [code:c#] #include #include #define MAX_INT 65536 #define MAX_NO_INPUT 1000 struct bucket{ int data; bucket* next; }; void addToBucket(struct bucket *head[],int value) { bucket* current; if (head[value] == NULL) { head[value] = (bucket*)malloc(sizeof(bucket)); head[value]-&#62;data = value; head[value]-&#62;next = NULL; } else { current = head[value]; while (current-&#62;next != NULL) current = current-&#62;next; [...]]]></description>
			<content:encoded><![CDATA[<p>排序算法是大家刚学编程的时候一定要学的算法，冒泡算法(bubble sort)，快速排序(quick sort)等等相信大家耳熟能详。我下面要介绍的bucket sort其实大家应该也学过，不过最近刚刚帮别人交了一份作业，勾起了不少回忆，所以顺便在这里发出来。下面的算法有几个限制：所排序的一定要是正整数，至于整数的大小和个数都是可以修改的。</p>
<p>[code:c#]</p>
<pre>#include
#include 

#define MAX_INT 65536
#define MAX_NO_INPUT 1000

struct bucket{
 int data;
 bucket* next;
};

void addToBucket(struct bucket *head[],int value)
{
 bucket* current;

 if (head[value] == NULL)
 {
  head[value] = (bucket*)malloc(sizeof(bucket));
  head[value]-&gt;data = value;
  head[value]-&gt;next = NULL;
 }
 else
 {
  current = head[value];
  while (current-&gt;next != NULL)
   current = current-&gt;next;
  current-&gt;next = (bucket*)malloc(sizeof(bucket));
  current-&gt;next-&gt;data = value;
  current-&gt;next-&gt;next = NULL;
 }
}
void printOut(bucket* head[],int size)
{
 int i=0;
 bucket *current;
 FILE *fp;

 fp = fopen("output.txt","w");

 for (i=size-1;i&gt;=0;i--)
 {
  if (head[i] == NULL)
   continue;
  current = head[i];
  while(current != NULL)
  {
   fprintf(fp,"%d\n",current-&gt;data);
   current = current-&gt;next;
  }
 }
 fclose(fp);
}
int main()
{
 bucket* head[MAX_INT] = {NULL};
 int unsorted[MAX_NO_INPUT];
 FILE *fp;
 int i=0;

 //read in a file
 if ((fp = fopen("input.txt","r"))==NULL)
 {
  printf("Cannot open file");
  exit(0);
 }

 i=0;
 while (fscanf(fp,"%i\n",&amp;unsorted[i])!=EOF &amp;&amp; i&lt; MAX_NO_INPUT)
  i++;
 fclose(fp);

 int length = i;
 i = 0;
 for (i=0;i

[/code]

以上的算法时间复杂度是O(n)。算是比较快速的排序算法，不过就跟所有算法一样，其实都是时间和空间上的取舍。它的空间复杂度相当大，尤其是在整数上限很大的情况下，更是占用了不少内存。所以，大家还是要按情况使用。</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jiyunalex.com/blog/2009/05/12/%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8bbucket-sort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

