1

我正在尝试编写一个代码让用户编写自己的数字并决定他是否希望它们按升序或降序排序,并用冒泡排序对它们进行排序。这是我目前能写的(也就是明显的入口);

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int n, a, number;
  printf("Enter your numbers. Write -1 to stop. \n");
  do {
    scanf("%d", &a);
  } while(a != -1);
  printf("Enter 1 if you want them to be in ascending order. Enter 2 if you want  descending order\n");
  scanf("%d", &a);
  if(a = 1)
    do {
      system("PAUSE");
      return 0;
    }

我的问题是,我真的不知道如何合并冒泡排序。在所有示例中,我都可以找到事先设置好的数组。我想我应该从一个 for 结构开始,但我不知道。

编辑:

多亏了帮助,我才走到了这一步,它有点“有效”,直到我写 1 或 2 然后它崩溃了。有什么建议么?

#include <stdio.h>
#include <stdlib.h>

int main()
{
int myarray[100],index,a,b,swap,turn;
index=0;
printf("Enter your numbers. Write -1 to stop. \n");
do{
            scanf("%d", &myarray[index]);
            index++;
            }while(myarray[index-1] != -1);
printf("Enter 1 if you want them to be in ascending order. Enter 2 if you want   descending order\n");
scanf("%d",&b);
if(b == 1) {
   for(turn=1; turn <= myarray[100] -1; turn++)

   for(index = 0; index <=  myarray[100]; index++)
   {
   if (myarray[index] > myarray[index+1]){ 
    swap  = myarray[index];
    myarray[index]   = myarray[index+1];
    myarray[index+1] = swap; }
    }
}
else {
   for(turn=1; turn <= myarray[100] -1; turn++)

   for(index = 0; index <=  myarray[100]; index++)
   {
   if (myarray[index] < myarray[index+1]){ 
    swap  = myarray[index];
    myarray[index]   = myarray[index+1];
    myarray[index+1] = swap; }
    }
}   
system("PAUSE");
return 0;
} 
4

4 回答 4

2

您将输入存储到单个变量a中,每次读取更多输入时都会覆盖该变量。您应该存储每个输入,以便您的程序知道提供的所有输入,而不仅仅是提供的最后一个输入。

数组是一组连续排列的相同类型的变量,并使用单个名称和索引进行访问。

int arr[10];

在这个例子arr中,组成了 10 个连续int的 s。您使用 访问int数组中的第一个,使用 访问arr[0]最后一个arr[9]。要将您的输入输入到数组中,您可以将其存储aarr. 您可以通过计算用户到目前为止输入的数字来维护正确的索引。该计数将用作输入数组的索引。不允许用户超出声明中定义的数组边界,否则当您尝试存储超出与数组关联的最后一个位置的数据时,您将调用未定义的行为(发生这种情况时,称为缓冲区溢出)。

将输入读入数组后,可以将数组传递给冒泡排序函数。

假设您有一个这样的输入例程:

#define MAX_ARR 10
int a;
int entered = 0;
int arr[MAX_ARR];
while (entered < MAX_ARR) {
    if (scanf("%d", &a) != 1) break;
    if (a == -1) break;
    arr[entered] = a;
    ++entered;
}
if (entered == MAX_ARR) {
    printf("No more room in the array (max is %d)\n", MAX_ARR);
}

我们检查了scanf是否返回了预期的返回值。我们已经根据停止值检查了输入,并确保用户输入的数据不能超过数组可以容纳的数据。

输入数组的元素个数是entered。因此,要遍历数组,循环看起来像这样:

int i;
for (i = 0; i < entered; ++i) {
    printf("arr[%d] = %d\n", i, arr[i]);
}

冒泡排序的一个非常简单的版本就是不断循环遍历数组,直到您不必再进行任何交换。每当两个连续的元素未按所需顺序时,您就可以交换。对于上升的情况:

int j, swaps, unsorted = entered;
do {
    swaps = 0;
    for (j = 1; j < unsorted; ++j) {
        /* ... if arr[j-1] and arr[j] need to swap then:
                   swap them, and
                   increment swaps ... */
    }
} while (swaps > 0);

您知道数组最后一个位置的元素将在一次完整的冒泡循环结束时处于其排序位置,因此unsorted在每次完整通过后可以减少数量。

于 2012-07-21T16:40:58.333 回答
2

您是正确的,因为您需要将这些数字存储在某种数据结构中,例如数组或向量。向量是一个不错的选择,因为您不知道用户将输入多少个数字。这是您可以应用于代码的草图:

#include <vector>

int main()
{
  // ...
  std::vector<int> userInts;
  // ... get input 
  userInts.push_back(a); // add int to the end of the list

  bubbleSort(userInts);
  // ...
}

编辑:我没有意识到这被标记为 C 而不是 C++。只需std::vector用一些代码替换调用即可在 C(或您自己的向量实现)中动态分配数组。或者,如果您知道只有N个整数将被输入,然后声明int userInts[N],循环输入,将其插入到数组中,然后排序。

EDITx2:请参阅下面的@user315052 的答案,以使用如上所述的固定长度数组执行此操作。

于 2012-07-21T16:41:36.080 回答
1

对于第一个版本,有一个固定大小的数组说

int myarray[100];
//Accept the integers 

index=0;
do {
    scanf("%d", &myarray[index]);
    index++;
}    while(myarray[index-1]!= -1);

现在你有了数组和元素总数的计数 - (index-1)

您可以将排序算法应用于数组。

于 2012-07-21T16:45:43.283 回答
0
#include <stdio.h>
#include <stdlib.h>

typedef enum _order {
    Ascending=1, Descending
} order;

void swap(int *x, int *y){
    int wk;
    wk=*x;*x=*y;*y=wk;
}

int needSwap(int x, int y, order dir){
    if(dir == Ascending)
        return x > y;
    if(dir == Descending)
        return x < y;
    return 0;
}

void bubbleSort(int *array, int top, int end, order dir){
    int i, j, swaped;
    for(i = top; i < end; ++i){
        swaped = 0;
        for(j = top + 1; j <= end - i; ++j)
            if(needSwap(array[j-1], array[j], dir)){
                swap(&array[j-1], &array[j]);
                swaped = 1;
            }
        if(swaped == 0)break;
    }
}

int main(){
    int myarray[100], index, order;
    index=0;
    printf("Enter your numbers. Write -1 to stop. \n");
    do{
        scanf("%d", &myarray[index++]);
    }while(myarray[index-1] != -1 && index < 100);
    --index;//Correction to point to the final value
    printf("Enter 1 if you want them to be in ascending order.\n"
           "Enter 2 if you want descending order\n");
    scanf("%d",&order);
    bubbleSort(myarray, 0, index-1, order);
    {//result print
        int i;
        for(i=0;i<index;++i)
            printf("%d ", myarray[i]);
        printf("\n");
    }
    system("PAUSE");
    return 0;
}
于 2012-07-22T02:02:30.530 回答