#CS50 Bubble排序代码示例未运行



我面临的情况是,我认为我的代码应该可以工作,但cs50控制台无法按预期运行代码。你能告诉我你是否发现了我的方法中的缺陷(如果有的话(吗??

每当我想运行代码时,控制台都会保持提示,当我尝试debug50选项时,它不会在控制台上打印现在排序的数组。

#include <stdio.h>
void bubble_sort(int array[], int n);
void swap(int first, int second);
void print_array(int array[], int n);

const  int SIZE = 6;
int main(void)
{
int  array[] = {2, 1, 3, 6, 4, 5};
int n = SIZE;
bubble_sort(array, SIZE);
printf("Sorted array: n");
print_array(array, SIZE);
}

void swap(int first, int second)
{
int container = first;
first = second;
second = container;
}

void bubble_sort(int array[], int n)
{
int counter = 1;
do
{
counter = 0;
for (int i = 0; i < n - 1; i++)
{
if (array[i] > array[i + 1])
{
swap(array[i], array[i + 1]);
counter += 1;
}
}
}
while (counter > 0);
}

void print_array(int array[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%i ", array[i]);
}
}

提前感谢!!!

这是冒泡排序的伪代码,如本课程第3讲所述:

Repeat n–1 times
For i from 0 to n–2
If i'th and i+1'th elements out of order
Swap them

尝试实现这个伪代码。

相关内容

最新更新