forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort2.c
118 lines (44 loc) · 1.13 KB
/
BubbleSort2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This program sorts an array of elements using the bubble sort algorithm
By: randerson112358
Output:
Enter total number(s) of elements: 4
Enter the 4 elements: 1 5 4 3
After Sorting: 1 3 4 5
NOTE: This bubble sort algorithm runs Best & Worst Case = O(n^2)
*/
#include <stdio.h>
int BubbleSort(int size, int *array);
int main(void){
int size, i, array[20];
printf("Enter total number(s) of elements: ");
scanf("%d", &size);
printf("Enter the %d elements: ", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);
}
//Run the Bubble Sort Algorithm to sort the list of elements
BubbleSort(size, array);
printf("After Sorting: ");
for(i=0; i<size; i++){
printf(" %d", array[i]);
}
printf("\n");
system("pause"); // uncomment if you are not using Windows OS
return 0;
}
int BubbleSort(int size, int *array){
int i, j, temp;
//Bubble sorting algorthm
for(i=1; i<size; i++){
for(j=1; j< size; j++){
//Swap
if(array[j-1] > array[j]){
temp = array[j-1];
array[j-1] = array[j];
array[j]= temp;
}
}
}
return 1;
}