尝试在自声明数组列表上实现一个简单的冒泡排序函数

Trying to implement a simple bubble sort function on a list of self declared arrays

新手尝试创建一个数组,然后使用我在代码底部创建的名为 "sort" 的函数对它们进行排序,使用冒泡排序..

我很确定打印部分没问题,只是我遇到问题的功能,有人可以帮助新手吗?..

#include <stdio.h>
#include <cs50.h>

void sort(int arrayRack[], int NumberOfArrayElements);

int main(void)

{
//variable declarations
int NumberOfArrayElements = 0;


//Get number of arrays
printf("number of arrays you'd like?\n");
NumberOfArrayElements = GetInt();

//variable array declaration
int arrayRack[NumberOfArrayElements];


//Get number for each int
for(int i = 0; i < NumberOfArrayElements; i++)
    {
        printf("give me an int for the %i th array\n", i + 1);
        arrayRack[i] = GetInt();
    }

//print what is currently in the arrayRack
for(int j = 0; j < NumberOfArrayElements; j++)
    printf("{%i}<-- number in %i-th array (unsorted)\n", arrayRack[j], j + 1);



for(int i = 0; i < NumberOfArrayElements; i++)
    {
        sort(&arrayRack[i], NumberOfArrayElements);
        printf("{%i}<-- number in %i-th array (sorted)\n", arrayRack[i], i + 1);

    }
    return 0;
}


//this is the funciton, like sort(haystack, size);
void sort(int arrayRack[], int NumberOfArrayElements)
{
//declare variable
int swap = 0;
        for(int k = 0; k < NumberOfArrayElements; k++)
        {
            //if [1] is higher, swap with [2]
            if(arrayRack[k] > arrayRack[k + 1])
            {
                swap = arrayRack[k];
                arrayRack[k] = arrayRack[k + 1];
                arrayRack[k + 1] = swap;
            }
        }

}

您传递了指向元素 &arrayRack[i] 的指针,但在 sort 的参数中您已将第一个参数声明为数组,因此这不匹配。你可以简单地写:

主线:

sort(arrayRack, NumberOfArrayElements);
for(int i = 0; i < NumberOfArrayElements; i++)
    {
        printf("{%i}<-- number in %i-th array (sorted)\n", arrayRack[i], i + 1);

    }

在排序函数中:

void sort(int arrayRack[], int NumberOfArrayElements)
{
//declare variable
int swap = 0;
        for(int k = 0; k < NumberOfArrayElements-1; k++)
        {

           for(int j=0; j<NumberOfArrayElements-1-k;j++)

            if(arrayRack[j] > arrayRack[j + 1])
            {
                swap = arrayRack[j];
                arrayRack[j] = arrayRack[j + 1];
                arrayRack[j + 1] = swap;
            }
        }

}

我还建议 scanf("%d",&NumberOfArrayElements); 而不是 NumberOfArrayElements = GetInt();scanf("%d",&arrayRack[i]); 而不是 arrayRack[i] = GetInt();

示例:

 number of arrays you'd like?
    5
    give me an int for the 1 th array
    5 
    give me an int for the 2 th array
    7
    give me an int for the 3 th array
    1
    give me an int for the 4 th array
    8
    give me an int for the 5 th array
    3
    {5}<-- number in 1-th array (unsorted)
    {7}<-- number in 2-th array (unsorted)
    {1}<-- number in 3-th array (unsorted)
    {8}<-- number in 4-th array (unsorted)
    {3}<-- number in 5-th array (unsorted)
    {1}<-- number in 1-th array (sorted)
    {3}<-- number in 2-th array (sorted)
    {5}<-- number in 3-th array (sorted)
    {7}<-- number in 4-th array (sorted)
    {8}<-- number in 5-th array (sorted)