-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuble_sort_algo.c
executable file
·66 lines (59 loc) · 1.29 KB
/
buble_sort_algo.c
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
/*
simple bubble sort algo
*/
main()
{
#define RAND_LEN 5
/* vars */
char esc;
int rand_number[RAND_LEN] = {0};
time_t timer;
int temp_i, temp_j, buffer, did_swap = 0;
/* code */
puts("random number befor buble_sort");
// random numbers Always will be different on execution because "seed"
srand(time(&timer));
// [1-rand_len] rand numbers
for (int i = 0; i < RAND_LEN; ++i)
{
rand_number[i] = (rand()%10)+1;
printf("%d\n", rand_number[i]);
}
//sort
//to compare i value with other - do i <= j?
for (int i = 0; i < RAND_LEN; ++i)
{
printf("%s\n", "outer counter");
for (int j = i+1; j < RAND_LEN; ++j)
{
if (rand_number[i] > rand_number[j])
{
buffer = rand_number[i];
rand_number[i] = rand_number[j];
rand_number[j] = buffer;
did_swap = 1;
//partial sorted sdtout
printf("%s\n", "partial sorted sdtout");
for (int i = 0; i < RAND_LEN; ++i)
{
printf("%d\n", rand_number[i]);
}
}
}
}
//sorted stdo
putchar('\n');
for (int i = 0; i < RAND_LEN; ++i)
{
printf("%d\n", rand_number[i]);
}
//exit poit
printf("\n\nexit point\n");
scanf(" %c", &esc);
return 0;
}