forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascendingorder_array.c
More file actions
45 lines (40 loc) · 1.01 KB
/
ascendingorder_array.c
File metadata and controls
45 lines (40 loc) · 1.01 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
//Program to arrange numbers of array in ascending order
#include <stdio.h>
int main() {
int n, i, j, temp;
int arr[100];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nEntered array: [");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
if (i < n - 1) {
printf(", ");
}
}
printf("]\n");
// Sorting array in ascending order using bubble sort
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\nSorted array in ascending order: [");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
if(i < n - 1) {
printf(", ");
}
}
printf("]\n");
return 0;
}