1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
/* Bubble Sort Example*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
int ary[SIZE];
void main(void);
void outputarray(void);
void bubblesort(void);
void outputarray(void)
{
int i;
for (i=0; i<=SIZE-1; i++)
{
if (i % 10 == 0) printf("\n");
printf("%3d ", ary[i]);
}
printf("\n");
}
void bubblesort(void)
{
int j, temp, stop=0;
while (!stop)
{
stop = 1;
for (j=0; j<SIZE-1; j++)
if (ary[j] > ary[j+1])
{
temp = ary[j];
ary[j] = ary[j+1];
ary[j+1] = temp;
stop = 0;
}
}
}
void main(void)
{
int i;
char ans='Y';
// randomize(); // for Borland C++
srand( (unsigned)time( NULL ) ); // for Visual C++
while (ans =='Y')
{
for (i=1; i<=SIZE; i++)
ary[i-1] = rand() / 100;
outputarray();
bubblesort();
outputarray();
printf("Continue (Y/N)? ");
scanf(" %c", &ans);
}
} |
Partager