-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathascending order of an array.c
50 lines (44 loc) · 1.07 KB
/
ascending order of an array.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
49
50
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
for(i=0; i<size; i++)
{
/*
* Place currently selected element array[i]
* to its correct place.
*/
for(j=i+1; j<size; j++)
{
/*
* Swap if currently selected array element
* is not at its correct position.
*/
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
/* Print the sorted array */
printf("\nElements of array in ascending order: ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}