-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbubbleSort.c
More file actions
46 lines (38 loc) · 894 Bytes
/
bubbleSort.c
File metadata and controls
46 lines (38 loc) · 894 Bytes
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
#include <stdio.h>
int swap(int array[], int i){
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
int bubbleSort(int array[], int size) {
for (int step = 0; step < size - 1; ++step) {
int swapped = 0;
for (int i = 0; i < size - step - 1; ++i) {
if (array[i] > array[i + 1]) {
swap(array, i);
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
int printarray(int array[], int size) {
printf("\nSorted Array in Ascending Order: ");
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int arraySize;
printf("Enter the array size: ");
scanf("%d",&arraySize);
int data[arraySize];
printf("Enter the array elements: ");
for(int i=0;i<arraySize;i++){
scanf("%d",&data[i]);
}
bubbleSort(data, arraySize);
printarray(data, arraySize);
}