-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_arrays.c
60 lines (47 loc) · 1.16 KB
/
sorting_arrays.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <time.h>
// g++ -o main main.c
// This program generates 10 individual random numbers and arranges them from smallest to largest.
int main() {
int i, temp, swapped;
int howMany = 10;
int goals[howMany];
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
// Randomize 10 numbers list.
for(i=0; i<howMany; i++)
{
goals[i] = ( rand()%100 )+1;
}
printf("Orginal List\n");
for(i=0; i<howMany; i++){
printf("%d \n", goals[i]);
}
// Sorting (arrays) randoms numbers to low to high.
// It is easy read the list
while(1){
swapped = 0; // sorted
for(i=0; i<howMany-1; i++)
{
if(goals[i]>goals[i+1]){
int temp = goals[i];
goals[i] = goals[i+1];
goals[i+1] = temp;
swapped = 1;
}
}
if(swapped==0){
break;
}
}
printf("\nSorted List\n");
for(i=0; i<howMany; i++){
printf("%d \n", goals[i]);
}
return 0;
}