w3resource

C Exercises: Sort the elements of an array


6. Array Sorting

Write a C program to sort the elements of an array.

Sample Solution:

C Code:

#include<stdio.h>     // Include the standard input/output header file.
#include<stdlib.h>    // Include the standard library header file.

// Comparator function used for qsort.
int comparetor (const void * x, const void * y)
{
return ( *(int*)x - *(int*)y );
}

int main ()           // Start of the main function.
{
int my_array[100];   // Declare an integer array 'my_array' with a maximum size of 100.
int n,i;             // Declare integer variables 'n' and 'i'.

printf("\nInput the number of elements to be stored in the array :");   // Prompt the user to input the number of elements.
scanf("%d",&n);   // Read the user's input and store it in 'n'.

printf("Input %d elements in the array :\n",n+1);   // Prompt the user to input the elements of the array.
for(i=0;i<n;i++)   // Start of a for loop to iterate over the elements of the array.
    {
printf("element - %d : ",i);   // Prompt the user for the value of the current element.
scanf("%d",&my_array[i]);     // Read the user's input and store it in the current element of 'my_array'.
    }

qsort (my_array, n, sizeof(int), comparetor );   // Sort the array 'my_array' using the qsort function and the 'comparetor' function.

printf("\nAfter sorting the array are :\n");   // Print a message indicating that the array has been sorted.
for (i=0; i<n; i++)   // Start of a for loop to iterate over the sorted elements of the array.
printf ("%d \n",my_array[i]);   // Print the current element of the sorted array.

return 0;   // Return 0 to indicate successful execution of the program.
}   // End of the main function.

Sample Output:

Input the number of elements to be stored in the array :5                                                     
Input 6 elements in the array :                                                                               
element - 0 : 2                                                                                               
element - 1 : 4                                                                                               
element - 2 : 3                                                                                               
element - 3 : 1                                                                                               
element - 4 : 5                                                                                               
                                                                                                              
After sorting the array are :                                                                                 
1                                                                                                             
2                                                                                                             
3                                                                                                             
4                                                                                                             
5

Flowchart:

C Exercises Flowchart: Sort the elements of an array

For more Practice: Solve these Related Problems:

  • Write a C program to sort an array of integers in descending order using qsort() with a custom comparator.
  • Write a C program to sort an array of strings in alphabetical order using a user-defined comparison function.
  • Write a C program to implement bubble sort on an array of integers and count the number of swaps performed.
  • Write a C program to sort an array of structures based on one of their integer fields using qsort().

Go to:


PREV : Random Number Generation.
NEXT : Quotient and Remainder Calculation.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.