Objectives of this lecture
How Bucket Sort Works
e.g. for the list: |
2 |
10 |
7 |
12 |
1 |
5 |
temp |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
-1 |
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
temp |
1 |
-1 |
2 |
-1 |
-1 |
5 |
-1 |
7 |
-1 |
-1 |
10 |
-1 |
12 |
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
final list: |
1 |
2 |
5 |
7 |
10 |
12 |
#include
<stdio.h>
#include
<stdlib.h>
#define
LIST_SIZE 6
void
BucketSort(int list[], int max);
main()
{
int
i, list[]={2,10,7,12,1,5};
BucketSort(list,13);
for (i=0; i<LIST_SIZE; i++)
printf("%d ",list[i]);
return 0;
}
void
BucketSort(int list[], int max)
{ int i, j=0, *temp;
temp=(int *)malloc(max*sizeof(int));
for(i=0; i<max; i++)
temp[i] = -1;
for (i=0; i<LIST_SIZE; i++)
temp[list[i]] = list[i];
for (i=0; i<max; i++)
if (temp[i] != -1)
list[j++]=temp[i];
free(temp);
}
Limitations:
Note: