2011-05-25 59 views
1

我正在使用Turbo C,并且对我的代码进行了一些查询。我只是困惑...程序首先要求一个数字列表(你不应该输入超过20)。当用户键入数字时,它们被放置在数组list[]中。一旦用户通过键入0 *(不在列表中)*终止列表,程序将调用sort()函数,该函数对列表中的值进行排序。在最后一部分,其中有/*I AM NOW CONFUSED WITH THIS PART*/的评论,是我需要你的帮助的部分...请帮助我。排序数组问题

enter image description here

 File Edit Run Compile Project Options Debug Break/watch 
    ╒════════════════════════════════════ Edit ════════════════════════════════════╕ 
    │  Line 1  Col 43 Insert Indent Tab Fill Unindent * C:NONAME.C   │ 
    │                    │ 
    │ #define MAXSIZE 20       /* size of buffter */   │ 
    │ void sort(int[], int);      /* prototype */    | 
    │                    | 
    │ main()                  | 
    │ {                   | 
    │  static int list[MAXSIZE];     /* buffer for numbers */  | 
    │  int size = 0;        /* size 0 before input */  | 
    │  int dex;         /* index of array */   | 
    │  do          /* get list of numbers */  | 
    │  {                  | 
    │   printf("Type number: ");            | 
    │   scanf("%d", &list[size]);           | 
    │  }                  | 
    │  while(list[size++] != 0);     /* exit loop on 0 */   | 
    │                    | 
    │  sort(list,--size);      /* sort nubmers */    | 
    │  for(dex=0; dex<size; dex++)    /* print sorted list */  | 
    │   printf("%d\n", list[dex]);           | 
    │                    | 
    │  getche();                | 
    │ }                   | 
    │                    | 
    │ void sort(int list[], int size)            | 
    │ {                   | 
    │  int out, in, temp;      /* I AM NOW CONFUSED */  | 
    │                    | 
    │  for(out=0; out<size-1; out++)    /* IN THIS PART! */   | 
    │   for(in=out; in<size; in++)           | 
    │    if(list[out] > list[in])          | 
    │    {                | 
    │     temp=list[in];            | 
    |     list[in]=list[out];           | 
    │     list[out]=temp;            | 
    │    }                | 
    │ }                   | 
    │                    | 
    │                    | 
    ├─────────────────────────────────── Watch ────────────────────────────────────┤ 
    │                    │ 
    └──────────────────────────────────────────────────────────────────────────────┘ 
    F1-Help F5-Zoom F6-Switch F7-Trace F8-Step F9-Make F10-Menu NUM 
+2

请告诉我什么是错的... – aer 2011-05-25 03:53:42

+0

这只是看起来像一个[冒泡排序(http://en.wikipedia.org/wiki/Bubble_sort),虽然它没有做正确的交换。 – chrisaycock 2011-05-25 03:55:06

+1

什么问题? – bdares 2011-05-25 03:57:08

回答

2

这只是意味着数组元素进行排序的代码,但它永远不会因为目前的形式工作:

temp=list[in]; 
list[out]=temp; 

将覆盖list[out]list[in]而没有保存list[out]原始内容。

交换两个变量的最佳方式是这样的:

temp = array[index1]; 
array[index1] = array[index2]; 
array[index2] = temp; 

而且,请你相信什么神的爱,不要做this :-)

所以,如果你的问题是如何排序数据,那么下面的伪代码应该有所帮助。我会提供C代码,但是,如果这是家庭作业,你应该自己做一些工作a

def sort (arr[], sz): 
    swapped = true      # Force loop entry. 
    while swapped:      # Loop until a pass had no swaps. 
     swapped = false 
     for idx goes from 1 to sz-1:  # For all but the first element. 
      if arr[idx-1] > arr[idx]: # If order is wrong. 
       swapped = true   # More passes will be needed. 
       temp = arr[idx-1]  # Swap 
       arr[idx-1] = arr[idx] # the 
       arr[idx] = temp   #  elements. 

这是一个气泡排序变化,一旦列表排序(好,一次传递后没有交换)就退出。无论如何,一些天真的变体只会持续大约n2次。


一个如果您想在一个评论,它的作业指示,我很乐意提供的C代码。请注意(如果您打算对我说谎),您的教育工作者几乎肯定能够看到该代码,您可能会在此情况下失败(或因公然抄袭而被开除)。


而且,由于你说这不是功课,这里说明一个完整的C程序:

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

#define FALSE (1==0) 
#define TRUE (1==1) 

static void sort (int arr[], int sz) { 
    int idx, temp, swapped; 

    swapped = TRUE;      // Force loop entry. 
    while (swapped) {      // Loop until a pass had no swaps. 
     swapped = FALSE; 
     for (idx = 1; idx < sz; idx++) { // For all but the first element. 
      if (arr[idx-1] > arr[idx]) { // If order is wrong. 
       swapped = TRUE;   // More passes will be needed. 
       temp = arr[idx-1];   // Swap 
       arr[idx-1] = arr[idx];  // the 
       arr[idx] = temp;   //  elements. 
      } 
     } 
    } 
} 

 

int main (int argc, char *argv[]) { 
    int sz, i, *vals; 

    sz = argc - 1; 
    if (sz < 1) 
     return 0; 
    if ((vals = malloc (sz * sizeof (int))) == NULL) { 
     printf ("ERROR: Cannot allocate memory.\n"); 
     return 1; 
    } 

    for (i = 0; i < sz; i++) 
     vals[i] = atoi (argv[i+1]); 

    printf ("Numbers before:"); 
    for (i = 0; i < sz; i++) 
     printf (" %d", vals[i]); 
    printf ("\n"); 

    sort (vals, sz); 

    printf ("Numbers after :"); 
    for (i = 0; i < sz; i++) 
     printf (" %d", vals[i]); 
    printf ("\n"); 

    free (vals); 
    return 0; 
} 

与运行此:

$ ./testprog 3 1 4 1 5 9 2 6 5 3 5 8 9 

给出y ou输出:

Numbers before: 3 1 4 1 5 9 2 6 5 3 5 8 9 
Numbers after : 1 1 2 3 3 4 5 5 5 6 8 9 9 
+0

@paxdiablo不,它不是作业...我们还没有达到开学日期... – aer 2011-05-25 05:30:20

+0

@paxdiablo ...我修改了我的代码,它确实有效。但我还有一些问题。在'for(out = 0; out aer 2011-05-26 08:28:33

+0

@paxdiable ...我把这个'-1',因为我认为,它应该排除用户输入0来终止程序...但我已经在函数调用'sort(list, - size);'中减小了'size'的大小。 – aer 2011-05-26 08:35:43

1

sort()最内环路的值交换是不完全的。它永远不会为list[in]分配任何东西。

0

我相信以下是你正在做的事情。基本上,你试图在每个内部循环迭代中找到最小元素。例如,如果从数组[5,4,3,2,1]开始,在第一次内循环迭代之后,数组看起来像下面那样:

[5,4,3,2, 1]:在经过= 0

[4,5,3,2,1]:在后= 1

[3,5,4,2,1]:在后= 2

[2,5,4,3,1]:后= 3

[1,5,4,3,2]:在后= 4

现在1是在开始。最终,数组将按[1,2,3,4,5]排序。

void sort(int list[], int size) { 
    int out, in, temp;  
    for(out=0; out<size; out++) {  | 
     for(in=out; in<size; in++)           
      if(list[out] > list[in])           
      {                
       tmp = list[out] 
       list[out]=list[in]; 
       list[in] = tmp                       
      }  
    }               
} 
+0

最后输入的0如何终止程序? – aer 2011-05-25 05:38:35

+0

@aerohn你可以调用sort(list,size - 1)进行排序,这将解决你的问题。 – CEGRD 2011-05-31 09:58:59