-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP-040 Bubble sort.c
More file actions
37 lines (33 loc) · 947 Bytes
/
Copy pathP-040 Bubble sort.c
File metadata and controls
37 lines (33 loc) · 947 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
/*
Author : Dawn K Vinod
Date : 14/02/2025
Description : Sorting an array of numbers using Bubble sort; Displaying the sorted array along with number of swaps taken to completely sort the array.
*/
#include <stdio.h>
int main() {
int n;
printf("Enter how many numbers you want to input: ");
scanf("%d",&n);
int arr[n];
for (int i=0; i<n; i++) {
printf("Enter number %d: ",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
int temp, swap_count=0;
for (int i=0; i<(n-1); i++) {
for (int k=0; k<(n-1-i); k++) {
if (arr[k] > arr[k+1]) {
temp = arr[k];
arr[k] = arr[k+1];
arr[k+1] = temp;
swap_count++;
}
}
}
for (int i=0; i<n; i++) {
printf("%d ",arr[i]);
}
printf("\nNo. of swaps: %d",swap_count);
return 0;
}