求大神解答,原始数组没有问题,但为什么排序后出现了出乎意料的结果
求教各位,我的程序代码如下,是在Visual Studio中写的;
已经定义好的数组在排序之前是没有问题的,但排序过后就会出问题,Release时而有问题,Debug是每次必出问题;
求大神指点
程序代码:
#include<stdio.h> #include<time.h> #include<stdlib.h> void maopao(int a[]); void exchange(int *a, int *b); #define LENGTH 100 int main() { int a[LENGTH],counter=0; srand(time(NULL)); while (1) { for (counter = 0;counter < LENGTH; counter++) { a[counter] = rand() % 100000; } maopao(a); printf("\nIt's over\nPress ENTER to run again\n"); getchar(); } return 0; } void exchange(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } void maopao(int a[]) { int i,j,output; printf("Before:\n\n"); for (output = LENGTH - 1;output >= 0;output--) { printf("\tNo.%d\t=%d\n ", output + 1, a[output]); } printf("\n\n\n"); for (i=0;i<=LENGTH-1;i++) { for (j = 0;j <= LENGTH-i;j++) { if (a[j] > a[j + 1]) exchange(&a[j], &a[j + 1]); } } printf("After:\n\n"); for (output = LENGTH-1;output>=0;output--) { printf("\tNo.%d\t=%d\n ",output+1, a[output]); } }