by Dinesh Thakur Category: Array

Suppose, we want to sort an array in ascending order. The elements with higher values will move back, while elements with smaller values will move to the front; the smallest element will become the 0th element and the largest will be placed at the end. The mechanism of sorting is explained below.

Consider an array int Marks[n] and let Marks[i] and Marks [i+l] be the two adjacent members of the array Marks [];now, the two are compared and if Marks [i] is found to be greater than Marks [ i +1] the two are swapped. The comparison then shifts to the next element and so on up to the end of the array. In this way, the element with the highest value goes to the end. However, if Marks [ i] is less than Marks [i +1] then no swapping is done and the comparison shifts to the next member, i.e., between Marks [ i +1] and Marks [i +2]. This process of comparison and swapping is successively carried out with all the elements of array. Thus, if n is the number of elements in the array, the process of comparison will be repeated n-1 times. In the first round only one value, i.e., the largest, goes to end. The process has to be repeated again to bring the next lower value to last but one position. And again it is to be repeated to place the next largest value at the proper position. Thus, this process will have to be carried out (n -1) times to complete the sorting.

Program  is coded to illustrate the process of sorting an array of characters in descending order. The output is given after each round of comparisons and swapping to make it illustrative, otherwise, output after every round of sorting is not necessary.

Illustrates sorting of arrays

#include <stdio.h>

void Sort(char Array[], int); // prototype of function Sort()
void main ()
    int i, k, m;
    char Array[7];
    printf("Enter 7 characters: ");
    for (i=0; i<7; i++)
        scanf("%c", &Array[i]);
        printf("you have entered the following characters:\n");
    for(k =0; k<7; k++)
        printf("%c\t", Array[k]);
        printf("\n The process of sorting is given below.\n");
   Sort(Array, 7);
   for( m = 0; m < 7; m++)
     printf("%c\t", Array[m]);
     printf ("\n");
     void Sort (char B [], int n) //Definition of function Sort()
       int i, j, k ;
       char temp;
       for(i =0; i<n-1; i++)
          for (j=0; j<n-1; j++)
            if (B [j]< B[j+1])
               temp= B[j];
               B[j] = B[j+1];
               B[j+1] = temp;
               printf ("\n");
               for (k=0; k<n; k++)

The expected output is as given below

     C Program sorting of an int array using Bubble Sort

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles