forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.c
More file actions
59 lines (43 loc) · 1.02 KB
/
Copy pathBubbleSort.c
File metadata and controls
59 lines (43 loc) · 1.02 KB
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
/*
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
*/
#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"); // comment this line if you are not using Windows OS
return 0;
}
int BubbleSort(int size, int *array){
int i, j, temp;
//Bubble sorting algorthm
for(i=size-2; i>= 0; i--){
for(j=0; j<=i; j++){
//Swap
if(array[j] > array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1]= temp;
}
}
}
return 1;
}