-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.c
More file actions
57 lines (49 loc) · 1.22 KB
/
bubbleSort.c
File metadata and controls
57 lines (49 loc) · 1.22 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
#include <stdio.h>
void selectionSort(int [], int);
void bubbleSort(int [], int); // method declaration
int main() {
int arr[] = {85, 54, 25, 95, 62,13};
int n=sizeof(arr)/sizeof(arr[0]);
printf("Before sorting: \n");
for(int i = 0; i < n; i++){
printf("%d\t", arr[i]);
}
printf("\n");
bubbleSort(arr, n);
// selectionSort(arr, 5);
printf("After sorting: \n");
for(int i = 0; i < n; i++){
printf("%d\t", arr[i]);
}
printf("\n");
}
void bubbleSort(int a[], int n){
int passes, comp, temp;
passes = n - 1;
for(int i = 0; i < passes; i++){
comp = n - i-1;
for(int j = 0; j < n; j++){
if(a[j] > a[j + 1]){
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
void selectionSort(int a[], int n){
int temp;
for(int i = 0; i < n - 1; i++){
int smallest = i;
for(int j = i + 1; j < n; j++){
if(a[smallest] > a[j]){
smallest = j;
}
}
if(i != smallest){
temp = a[i];
a[i] = a[smallest];
a[smallest] = temp;
}
}
}