Write C program to sort an array in ascending order
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 | #include <stdio.h> int main() { int arr[100]; int size, i, j, temp; // Reading the size of the array printf("Enter size of array: "); scanf("%d", &size); //Reading elements of array printf("Enter elements in array: "); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } //Sorting an array in ascending order for(i=0; i<size; i++) { for(j=i+1; j<size; j++) { //If there is a smaller element found on right of the array then swap it. if(arr[j] < arr[i]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } //Printing the sorted array in ascending order printf("\nElements of array in sorted ascending order:\n"); for(i=0; i<size; i++) { printf("%d\n", arr[i]); } return 0; } |
11 July 2019 1752 Written By: Rohit
© 2020 Tech Study. All rights reserved | Developed by Tech Study| Privacy Policy | Sitemap