blob: 521a78119ff2ecc875b59318390d2109454769b9 (
plain)
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
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 10
void swap(int * a, int * b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void generateRandom(int * arr, int n)
{
time_t sr;
srand(sr);
for (int i = 0; i < n; i++)
arr[i] = rand() % 100;
}
void bubbleSort(int * arr, int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1 - i; j++)
if (arr[j] > arr[j+1])
swap(&arr[j],&arr[j+1]);
}
int main(void)
{
int arr[SIZE];
generateRandom(arr,SIZE);
for (int i = 0; i < SIZE; i++)
printf("%3d",arr[i]);
putchar('\n');
bubbleSort(arr,SIZE);
for (int i = 0; i < SIZE; i++)
printf("%3d",arr[i]);
putchar('\n');
return 0;
}
|