我正在使用Turbo C,并且对我的代码进行了一些查询。我只是困惑...程序首先要求一个数字列表(你不应该输入超过20)。当用户键入数字时,它们被放置在数组list[]
中。一旦用户通过键入0 *(不在列表中)*终止列表,程序将调用sort()
函数,该函数对列表中的值进行排序。在最后一部分,其中有/*I AM NOW CONFUSED WITH THIS PART*/
的评论,是我需要你的帮助的部分...请帮助我。排序数组问题
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
请告诉我什么是错的... – aer 2011-05-25 03:53:42
这只是看起来像一个[冒泡排序(http://en.wikipedia.org/wiki/Bubble_sort),虽然它没有做正确的交换。 – chrisaycock 2011-05-25 03:55:06
什么问题? – bdares 2011-05-25 03:57:08