Home > Back-end >  C randomly generated within the 100 number, into the array and the bubble sort, intermittent error (
C randomly generated within the 100 number, into the array and the bubble sort, intermittent error (

Time:10-05

Compile the code below, after the operation, can sometimes sorting, sometimes can't sorting, probability 50-50, shouldn't this also the mood?



#include
#include
#include

# define MAXSIZE 255


Bubble sort/* */
Void the BubbleSort (int a [], int n) {
Int I, j, temp.
Int flag=0;
for(i=1; iFor (j=1; jIf (a [j] & gt; + 1 a [j]) {
Temp=a, [j].
A [j]=a, a + 1 bonus to [j].
A=\ [j + 1];
flag=1;
}
If (flag==0) return;
}
}
}

Int main () {
Srand (time (NULL));
Int length=10;
Int I, J;
Int k=1;
Int a [MAXSIZE];
for(i=1; i<=length; I++) {
A [I]=rand () % 101;
}
Printf (" before ordering: ");
For (I=1; i<=length; I++) {
Printf (" % d \ t ", a [I]);
}
Putchar (" \ n ");
Printf (" sorted: ");
A, the BubbleSort (length);
For (I=1; i<=length; I++) {
Printf (" % d \ t ", a [I]);
}
Putchar (" \ n ");
return 0;
}

CodePudding user response:

This should see face






Or back into the oven, you all have a good look at the bubbling, books or
The role of the flag you have a good research study

CodePudding user response:

In the BubbleSort, this flag do you use the ~ ~

If (flag==0) return; Had the delete

CodePudding user response:

Indeed, the flag is completely useless, don't know what to do
  • Related