CodePudding user response:
# include & lt; Stdio. H>
#include
Void bubble_sort (int * arr, int len);
Int main ()
{
Int n, * arr, I;
The scanf (" % d ", & amp; N);
Arr=(int *) malloc (sizeof (int) * (n));
if (! Arr)
return -1;
For (I=0; iThe scanf (" % d ", & amp; Arr [I]);
Bubble_sort (arr, n);
For (I=0; iPrintf (" % d ", arr [I]);
Putchar (10);
/*
For (I=1; iPrintf (" % d ", arr [I]);
Putchar (10);
For (I=2; iPrintf (" % d ", arr [I]);
Putchar (10);
*/
return 0;
}
Void bubble_sort (int * arr, int len)
{
int i, j;
Int TMP.
For (I=0; iFor (j=0; J & lt; Len - 1; J++) {
If (arr [j] & gt; Arr [m + 1]) {
TMP=arr [j];
Arr=arr [j] [j + 1);
Arr=[m + 1] TMP;
}
}
}
}
For your reference ~
CodePudding user response:
Plan a, the use of variable-length array, need support for C99 compiler
# include "stdio.h"
# include "stdlib. H"
# include "time. H"
Int main (void)
{
Int num=0, I=0;
Printf (" input array length: ");
The scanf (" % d ", & amp; Num);
Int arr (num);
Srand (time (0));
For (I=0; i{
Arr [I]=rand () % 100;
Printf (" % d ", arr [I]);
}
return 0;
}
Scheme 2, application memory, C90 can also
# include "stdio.h"
# include "stdlib. H"
# include "time. H"
Int main (void)
{
Int num=0, I=0;
Printf (" input array length: ");
The scanf (" % d ", & amp; Num);
Int * arr=calloc (num, sizeof (int));.
Srand (time (0));
For (I=0; i{
Arr [I]=rand () % 100;
Printf (" % d ", arr [I]);
}
return 0;
}