Arrange the Elements of an integer array in sorted order

 

4. Arrange the Elements 

Aim:

To write a C program to sort an array of integers in Ascending order using the Bubble Sort algorithm.

 Program :


#include <stdio.h>
void main (){
   int num[20];
   int i, j, a, n;
   printf("enter number of elements in an array ");
   scanf("%d", &n);
   printf("Enter the elements\n");
   for (i = 0; i < n; ++i)
      scanf("%d", &num[i]);
   for (i = 0; i < n; ++i){
      for (j = i + 1; j < n; ++j){
         if (num[i] > num[j]){
            a = num[i];
            num[i] = num[j];
            num[j] = a;
         }
      }
   }
   printf("The numbers in ascending order is: \n");
   for (i = 0; i < n; ++i){
      printf("%d\t", num[i]);
   }
}

No comments:

Post a Comment

P24CA3P5 - Core Practical V: SMART DEVICES PROGRAMMING LAB

  Core Practical V: SMART DEVICES PROGRAMMING LAB SEMESTER: III                                 CODE: P24CA3P5 CREDITS: 2                  ...