## 14. C program to sort the elements of an array in ascending order

In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

Original array:

Array after sorting:

Elements will be sort in such a way that smallest element will appear on extreme left which in this case is 1. The largest element will appear on extreme right which in this case is 8.

### ALGORITHM:

• STEP 1: START
• STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }..
• STEP 3: SET temp =0
• STEP 4: length= sizeof(arr)/sizeof(arr[0])
• STEP 5: PRINT "Elements of Original Array"
• STEP 6: SET i=0. REPEAT STEP 7 and STEP 8 UNTIL i<length
• STEP 7: PRINT arr[i]
• STEP 8: i=i+1.
• STEP 9: SET i=0. REPEAT STEP 10 to STEP UNTIL i<n
• STEP 10: SET j=i+1. REPEAT STEP 11 UNTIL j<length
• STEP 11: if(arr[i]>arr[j]) then
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp
• STEP 12: j=j+1.
• STEP 13: i=i+1.
• STEP 14: PRINT new line
• STEP 15: PRINT "Elements of array sorted in ascending order"
• STEP 16: SET i=0. REPEAT STEP 17 and STEP 18 UNTIL i<length
• STEP 17: PRINT arr[i]
• STEP 18: i=i+1.
• STEP 19: RETURN 0.
• STEP 20: END.

### PROGRAM:

Output:

```Elements of original array:
5 2 8 7 1
Elements of array sorted in ascending order:
1 2 5 7 8
```
Next TopicC Programs