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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4407696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:21:40  来源:igfitidea点击:

Sorting an Array in C from low to high (without using qsort)

carraysalgorithmsorting

提问by smooth_smoothie

I have a functions, which takes an Array of numbers, and sort them from low to high. So far, I have this algorithm, however the output isn't what I'm expecting. Can someone shed some light on it. I cannot use any C library functions.

我有一个函数,它接受一个数字数组,并将它们从低到高排序。到目前为止,我有这个算法,但是输出不是我所期望的。有人可以解释一下吗。我不能使用任何 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);
}

回答by bcosca

You are trying to implement the bubble sort algorithm. Read this to understand what your code is missing.

您正在尝试实现冒泡排序算法。阅读本文以了解您的代码缺少什么

回答by codaddict

The condition in forloop i<=countis incorrect.

for循环中的条件i<=count不正确。

Valid index in the array are 0to count-1.
Since you are accessing value at index i+1in the loop:

数组中的有效索引是0to count-1
由于您正在i+1循环中访问索引处的值:

if (numbers[i] > numbers[i+1])

ican take the value from 0to count-2, so change the condition to i<=count-2or i<count-1

i可以从0to取值count-2,因此将条件更改为 i<=count-2i<count-1

回答by Jim Lewis

The value of kis used, but the variable is never initialized or assigned to. At some point your code will try to access the value numbers[count], when an array containing countelements has maximum index count-1

使用了 的值k,但从未初始化或分配变量。在某些时候numbers[count],当包含count元素的数组具有最大索引时,您的代码将尝试访问该值count-1

回答by AlastairG

You haven't initalised k.

你还没有初始化k

The algorithm will stop as soon as it has moved just one number. You need to move all of them.

算法只要移动了一个数字就会停止。您需要移动所有这些。

I think you are missing a for loop on koutside the while loop, but as I'm not entirely sure what you're trying to do here I can't be sure.

我认为您k在 while 循环之外缺少 for 循环,但由于我不完全确定您在这里尝试做什么,我无法确定。

Why can't you implement your own qsort() function? Is that allowed? Try reading up on a few sorting algorithms online.

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

回答by Eiko

if (numbers[i] > numbers[k]){

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

should probably be

应该是

if (numbers[i] > numbers[i+1]){

如果(数字[i] > 数字[i+1]){

kisn't used at all.

k根本没有使用。

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

should probably be

应该是

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

as there are only elements from 0 to count-1, and then you are comparing to the next one. The name for jis crap. Make it a boolean called didSwap. And then rethink about your codition, maybe it's just the other way around...

因为只有从 0 到 count-1 的元素,然后您要与下一个进行比较。的名字j是废话。将其设为名为 didSwap 的布尔值。然后重新考虑你的条件,也许它只是相反......