C Array – Sorting Ascending and Descending in C Language

Array-Sorting-Ascending-and-Descending-in-C-Language

Sorting Ascending and Descending in C – Program Description:

Write a C Program for Sorting Ascending and Descending in C programming language. We are going to use the qsort() function from the stdlib.h header file to sort the array.

We are going to write two programs in this article.

  1. Sorting Array in Ascending order using QSort.
  2. Sorting Array in Descending order using QSort.

Syntax of the QSort() function:

  • array is the array to be sorted
  • num is the size of the array
  • size is the size of the data type. If you are using integer data, then it should sizeof(int)
  • compare_function is a function pointer, which points to the comparison function.

1. Sorting Array in Ascending Order Using QSort Function:

Sort Array – Ascending Order Program Explanation:

  1. Declare the values array and desired variables.
  2. Prompt the user to provide the desired size and update size variable
  3. Create the read() and display() functions to read and print the array elements
  4. Take the array elements from the user and update the array using read() function.
  5. Display the array elements using the display() function.
  6. Now, We need to sort the array of elements using the qsort() function.
  7. The qsort() function needs a compare_function, Define a compare function named compare_asc.
    • Prototype of the compare_asc function is int compare_asc(const void *x, const void *y)
    • Compare function compares the values pointed by the x and y variables and returns
      • A Positive Number if the variable x should be placed after the variable y in ascending order
      • A Negative value if variable x should be placed before y in ascending order
      • or Returns Zero(0) if both the variables x and y are equal.
  8. Make the call to qsort() function with parameters values array, size, and sizeof(int), and compare_asc function pointer.
    • qsort(values, size, sizeof(int), compare_asc );
  9. Print the sorted Array on the console.

The QSort Compare function Logic for Sorting Array in Ascending Order:

Compare function compares the values pointed out by the x and y variables and returns

  • A Positive Number if the variable x should be placed after the variable y in ascending order
  • A Negative value if variable x should be placed before y in ascending order
  • or Returns Zero(0) if both variables x and y are equal.

Sort Array in Ascending order using QSort function:

Program Output:

Compile and Run the program using GCC (any compiler)

Test 1:

sort-array-in-ascending-order-using-qsort-function-in-c

Test 2:

The program is providing the expected output.

2. Sorting Array in Descending Order Using QSort Function:

Sort Array – Descending Order Program Explanation:

The program is very similar to the above Ascending order program except for the compare function ( compare_func).

We need to change the compare function so that, the qsort() function sorts the array in descending order.

Compare function logic for QSort() to sort elements in Descending order:

Define a compare function named compare_dsc.

  • Prototype of the compare_dsc function is int compare_dsc(const void *x, const void *y)
  • Compare function compares the values pointed out by the x and y variables and returns
    • A Positive value if variable x should be placed before y in descending order
    • A Negative Number if the variable x should be placed after the variable y in descending order
    • or Returns Zero(0) if both variables x and y are equal.

Sort Array in Descending order using QSort function:

Here is the program to sort the array in descending order using the qsort() function in the c programming language.

Program Output:

Compile and Run the Program.

sort-array-using-qsort-in-descending-order-in-c-language

As we can from the above output, The program is sorting the array elements in descending order.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

13 Responses

  1. […] better understand the following program, Please go through the C Arrays, Functions in C, and How to Pass Arrays to Functions […]

  2. […] C Program to Sort Array in Ascending and Descending Order using QSort function […]

  3. […] Arrays in C Language with Example Programs […]

  4. […] Arrays in C Language with Example Programs […]

  5. […] C Arrays […]

  6. […] One-dimensional Arrays in C […]

  7. […] C Arrays […]

  8. […] One dimensional Arrays in C […]

  9. […] C Arrays […]

  10. […] One dimensional Arrays in C […]

  11. […] C Program to Sort Array in Ascending and Descending Order using QSort function […]

  12. […] C Arrays […]

  13. […] C Program to Sort Array in Ascending and Descending Order using QSort function […]

Leave a Reply