C语言中的数组桶排序
我试图从txt文件中读取数字列表,然后用桶排序法对它们进行排序。 这是我的代码:C语言中的数组桶排序,c,arrays,sorting,C,Arrays,Sorting,我试图从txt文件中读取数字列表,然后用桶排序法对它们进行排序。 这是我的代码: void bucketSort(int array[],int *n) { int i, j; int count[*n]; for (i = 0; i < *n; i++) count[i] = 0; for (i = 0; i < *n; i++) (count[array[i]])++; for (i = 0, j
void bucketSort(int array[],int *n)
{
int i, j;
int count[*n];
for (i = 0; i < *n; i++)
count[i] = 0;
for (i = 0; i < *n; i++)
(count[array[i]])++;
for (i = 0, j = 0; i < *n; i++)
for(; count[i] > 0; (count[i])--)
array[j++] = i;
}
int main(int brArg,char *arg[])
{
FILE *ulaz;
ulaz = fopen(arg[1], "r");
int array[100];
int i=0,j,k,n;
while(fscanf(ulaz, "%d", &array[i])!=EOF)i++;
fclose(ulaz);
n=i;
for (j = 0; j<i; j++)
{
printf("Broj: %d\n", array[j]);
}
BucketSort(array,&n);
for (k = 0; k<i; k++)
printf("%d \n", array[i]);
return 0;
}
void bucketSort(int数组[],int*n)
{
int i,j;
整数计数[*n];
对于(i=0;i<*n;i++)
计数[i]=0;
对于(i=0;i<*n;i++)
(计数[数组[i]])+;
对于(i=0,j=0;i<*n;i++)
对于(;计数[i]>0;(计数[i])--)
数组[j++]=i;
}
int main(int brArg,char*arg[])
{
文件*ulaz;
ulaz=fopen(arg[1],“r”);
整数数组[100];
int i=0,j,k,n;
而(fscanf(ulaz,“%d”,&array[i])!=EOF)i++;
fclose(乌拉兹);
n=i;
对于(j=0;j,当您试图写入程序不拥有的内存位置时,代码显示出未定义的行为
for (i = 0; i < *n; i++)
(count[array[i]])++;
当i
为0时可以,因为它与count[2]
相同。当i
为1时,这与count[3]
相同。之后,当i
为4和5时,count[4]
和count[5]
在尝试写入无效内存位置时是错误的
此外,您的代码不会对值进行排序。当您试图写入程序不拥有的内存位置时,代码会显示未定义的行为
for (i = 0; i < *n; i++)
(count[array[i]])++;
当i
为0时可以,因为它与count[2]
相同。当i
为1时,这与count[3]
相同。之后,当i
为4和5时,count[4]
和count[5]
在尝试写入无效内存位置时是错误的
此外,您的代码不会对值进行排序。正如Cool Guy正确指出的,您在内存访问方面存在问题,但最重要的是,代码不会对任何内容进行排序。首先,您应该了解实际工作情况
一般而言:
- 将输入数据按某些条件在各个存储桶之间进行划分,以确保这些存储桶不会扰乱输入顺序
- 使用其他排序方法或使用bucket Sort对每个bucket进行递归排序
- 连接已排序的数据(这就是为什么第一个点具有不扰乱输入顺序的限制)
这是您的原始代码的一个示例,我尝试对其进行尽可能小的调整,以便于您理解。此代码将预定义的输入数组按范围划分为3个存储桶:
- [-无穷大][-1]->第一个桶
- [0;10]->第二个桶
- [11;无穷大]->第三个桶
然后对每个bucket执行快速排序并连接结果。我希望这有助于理解该算法的工作原理
#include <stdio.h>
#include <stdlib.h>
struct bucket
{
int count;
int* values;
};
int compareIntegers(const void* first, const void* second)
{
int a = *((int*)first), b = *((int*)second);
if (a == b)
{
return 0;
}
else if (a < b)
{
return -1;
}
else
{
return 1;
}
}
void bucketSort(int array[],int n)
{
struct bucket buckets[3];
int i, j, k;
for (i = 0; i < 3; i++)
{
buckets[i].count = 0;
buckets[i].values = (int*)malloc(sizeof(int) * n);
}
// Divide the unsorted elements among 3 buckets
// < 0 : first
// 0 - 10 : second
// > 10 : third
for (i = 0; i < n; i++)
{
if (array[i] < 0)
{
buckets[0].values[buckets[0].count++] = array[i];
}
else if (array[i] > 10)
{
buckets[2].values[buckets[2].count++] = array[i];
}
else
{
buckets[1].values[buckets[1].count++] = array[i];
}
}
for (k = 0, i = 0; i < 3; i++)
{
// Use Quicksort to sort each bucket individually
qsort(buckets[i].values, buckets[i].count, sizeof(int), &compareIntegers);
for (j = 0; j < buckets[i].count; j++)
{
array[k + j] = buckets[i].values[j];
}
k += buckets[i].count;
free(buckets[i].values);
}
}
int main(int brArg,char *arg[]) {
int array[100] = { -5, -9, 1000, 1, -10, 0, 2, 3, 5, 4, 1234, 7 };
int i = 12,j,k,n;
n=i;
for (j = 0; j<i; j++)
{
printf("Broj: %d\n", array[j]);
}
bucketSort(array, n);
for (k = 0; k<i; k++)
printf("%d \n", array[k]);
return 0;
}
#包括
#包括
结构桶
{
整数计数;
int*值;
};
整数比较整数(常量无效*第一,常量无效*第二)
{
int a=*((int*)第一,b=*((int*)第二);
如果(a==b)
{
返回0;
}
否则如果(a10:3
对于(i=0;i10)
{
bucket[2]。值[bucket[2]。count++]=数组[i];
}
其他的
{
bucket[1]。值[bucket[1]。count++]=数组[i];
}
}
对于(k=0,i=0;i<3;i++)
{
//使用快速排序分别对每个存储桶进行排序
qsort(bucket[i]。值,bucket[i]。计数,sizeof(int)和compareIntegers);
对于(j=0;j 对于(j=0;j,正如Cool Guy正确指出的那样,您在内存访问方面存在问题,但最重要的是,代码没有对任何内容进行排序。首先,您应该了解实际工作原理
一般而言:
- 将输入数据按某些条件在各个存储桶之间进行划分,以确保这些存储桶不会扰乱输入顺序
- 使用其他排序方法或使用bucket Sort对每个bucket进行递归排序
- 连接已排序的数据(这就是为什么第一个点具有不扰乱输入顺序的限制)
这是您的原始代码的一个示例,我尝试对其进行尽可能小的调整,以便于您理解。此代码将预定义的输入数组按范围划分为3个存储桶:
- [-无穷大][-1]->第一个桶
- [0;10]->第二个桶
- [11;无穷大]->第三个桶
然后对每个bucket执行快速排序并连接结果。我希望这有助于理解该算法的工作原理
#include <stdio.h>
#include <stdlib.h>
struct bucket
{
int count;
int* values;
};
int compareIntegers(const void* first, const void* second)
{
int a = *((int*)first), b = *((int*)second);
if (a == b)
{
return 0;
}
else if (a < b)
{
return -1;
}
else
{
return 1;
}
}
void bucketSort(int array[],int n)
{
struct bucket buckets[3];
int i, j, k;
for (i = 0; i < 3; i++)
{
buckets[i].count = 0;
buckets[i].values = (int*)malloc(sizeof(int) * n);
}
// Divide the unsorted elements among 3 buckets
// < 0 : first
// 0 - 10 : second
// > 10 : third
for (i = 0; i < n; i++)
{
if (array[i] < 0)
{
buckets[0].values[buckets[0].count++] = array[i];
}
else if (array[i] > 10)
{
buckets[2].values[buckets[2].count++] = array[i];
}
else
{
buckets[1].values[buckets[1].count++] = array[i];
}
}
for (k = 0, i = 0; i < 3; i++)
{
// Use Quicksort to sort each bucket individually
qsort(buckets[i].values, buckets[i].count, sizeof(int), &compareIntegers);
for (j = 0; j < buckets[i].count; j++)
{
array[k + j] = buckets[i].values[j];
}
k += buckets[i].count;
free(buckets[i].values);
}
}
int main(int brArg,char *arg[]) {
int array[100] = { -5, -9, 1000, 1, -10, 0, 2, 3, 5, 4, 1234, 7 };
int i = 12,j,k,n;
n=i;
for (j = 0; j<i; j++)
{
printf("Broj: %d\n", array[j]);
}
bucketSort(array, n);
for (k = 0; k<i; k++)
printf("%d \n", array[k]);
return 0;
}
#包括
#包括
结构桶
{
整数计数;
int*值;
};
整数比较整数(常量无效*第一,常量无效*第二)
{
int a=*((int*)第一,b=*((int*)第二);
如果(a==b)
{
返回0;
}
否则如果(a10:3
对于(i=0;i