-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample-7.c
48 lines (39 loc) · 995 Bytes
/
Example-7.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
#include <stdio.h>
// Function to perform Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Function to print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n;
// Get the number of elements
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
// Get array elements
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Perform Bubble Sort
bubbleSort(arr, n);
// Display sorted array
printf("Sorted array: ");
printArray(arr, n);
return 0;
}