将C中的数组从低到高排序(不使用qsort)

将C中的数组从低到高排序(不使用qsort),c,arrays,algorithm,sorting,C,Arrays,Algorithm,Sorting,我有一个函数,它接受一个数字数组,并将它们从低到高排序。到目前为止,我有这个算法,但是输出不是我期望的。有人能解释一下吗。我不能使用任何C库函数 /* Sort "count" numbers stored in array numbers[] in non-decreasing order. There may be duplicate numbers in the array. You may use any sorting algorithm that you know

我有一个函数,它接受一个数字数组,并将它们从低到高排序。到目前为止,我有这个算法,但是输出不是我期望的。有人能解释一下吗。我不能使用任何C库函数

/* 
   Sort "count" numbers stored in array numbers[] in non-decreasing order.
   There may be duplicate numbers in the array.
   You may use any sorting algorithm that you know.
 */

void sort( double numbers[], int count )
{
    int i, j, k;
    //printf("%d", count);

    double temp;
    do{
        j = 0;  
        for (i = 0;i<=count;i++){
                if (numbers[i] > numbers[i+1]){//this was numbers[k], which was an error
                    j = 1;
                    temp = numbers[i];
                    numbers[i] = numbers[i+1];
                    numbers[i+1] = temp;
                }
            }
    } while (j == 1);
}
/*
按非降序对数组numbers[]中存储的“count”数字进行排序。
数组中可能有重复的数字。
您可以使用您知道的任何排序算法。
*/
无效排序(双数[],整数计数)
{
int i,j,k;
//printf(“%d”,计数);
双温;
做{
j=0;
对于(i=0;i数字[i+1]){//这是数字[k],这是一个错误
j=1;
温度=数字[i];
数字[i]=数字[i+1];
数字[i+1]=温度;
}
}
}而(j==1);
}

使用了
k
的值,但变量从未初始化或赋值。当包含
count
元素的数组具有最大索引
count-1

时,您的代码将尝试访问值
numbers[count]
,您尚未初始化
k

只要移动了一个数字,算法就会停止。你需要把它们都搬走

我认为您在while循环之外的
k
上缺少for循环,但是我不能完全确定您在这里试图做什么,所以我不能确定

为什么不能实现自己的qsort()函数?允许吗?尝试在线阅读一些排序算法。

如果(数字[i]>数字[k]){

应该是

for (i = 0; i < count-1;i++){
如果(数字[i]>数字[i+1]){

k
根本不使用

for (i = 0;i <= count;i++){

用于(i=0;i您正在尝试实现冒泡排序算法。

循环
中的条件我很高兴知道。。但是不要忘了阅读@stillstanding提供的wiki链接。您的实现可以优化。这就是为什么我提到了计数的事情-但是文本太混乱,没有显示完整的代码。。。
if (numbers[i] > numbers[i+1])