-
Notifications
You must be signed in to change notification settings - Fork 69
/
Array_Sorting.c
46 lines (38 loc) · 1.12 KB
/
Array_Sorting.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
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Displaying elements of original array
printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
//Sort the array in ascending order
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\n");
//Displaying elements of array after sorting
printf("Elements of array sorted in ascending order: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
/*
Output
Elements of original array:
5 2 8 7 1
Elements of array sorted in ascending order:
1 2 5 7 8
*/