Sorting Array Integers In Ascending Or Descending Order In C Programming

• Tuesday, 27th September 2011

In this C Programming tutorial I am going to take you through how you can sort Integers in either ascending or descending order as far as C programming is concerned.
With ascending order integers will be arranged from smallest to biggest in terms of values.
With descending order integers will be arranged from biggest to smallest in terms of their values.
Sorting array elements is something you will encounter more often if you are writing programs that involve numerical value ranking. Examples of programs that require numerical value ranking include, a program that assigns students positions according to marks obtained, a program that finds the median of data that is not grouped, etc.
The following is the code that sorts integers in array in ascending order.

Sorting.c
 1 #include 2 #include 3 4 int main() 5 { 6 int temporaryStorage; 7 int counter; 8 int numArray[] = {2, 4, 1, 5, 7, 6, 3, 10, 8, 9}; 9 bool sortExists = true; 10 11 printf(“Array Before Sorting\n”); 12 for(counter = 0; counter < 10; ++counter){ 13 printf(“%d “, numArray[counter]); 14 } 15 16 while (sortExists == true) 17 { 18 sortExists = false; 19 for (counter = 0; counter < 9; ++counter) 20 { 21 if (numArray[counter] > numArray[counter + 1]) 22 { 23 temporaryStorage = numArray[counter]; 24 numArray[counter] = numArray[counter + 1]; 25 numArray[counter + 1] = temporaryStorage; 26 sortExists = true; 27 } 28 } 29 } 30 31 printf(“\n\nArray After Sorting – Ascending Order\n”); 32 for(counter = 0; counter < 10; ++counter){ 33 printf(“%d “, numArray[counter]); 34 } 35 36 return 0; 37 }
Explanation

From the code in Sorting.c, on line 1 and line 2 I included two C headers i.e. stdio.h and stdbool.h. You should be familiar with the stdio.h header since it’s the standard header included in almost every C program where inputting or outputting is expected to be done. On the other hand, I included the stdbool.h header because somewhere in the code on line 9 I wanted to declare a variable of data type bool. Besides including the stdbool.h header  there are other ways you can deal with Boolean values but I won’t look at them in this tutorial since the way (including the stdbool.h header) I have showed you is the standard way of working with Boolean values in C.

On line 4 is where the main method begins from, its code block starts from line 5 and its ends at line 37.

On line 6 to line 9, I declared variables temporaryStorage, counter, sortExists and an array numArray of type int, int, bool and int respectively. Furthermore I initialized the array numArray and the variable sortExist to {2, 4, 1, 5, 7, 6, 3, 10, 8, 9} and true respectively. I will be telling you letter the role of each of the declared variables later in this tutorial, “for now let’s not cross the bridge before we reach it”. However if you have already gone through the code and figures out the role of each of them, Bingo.

On line 11, printing of the words “Array Before Sorting” is done and at the end of those words a new line character (\n) is inserted to push all that is printed thereafter to a new line.

On line12 to line 15 is a for loop which loops through the elements in the numArray array printing every single integer stored in it.

On line 16 I started a while loop which executes only if the value stored in in the variable sortExists is true. Recall that the variable sortExists was initialized with true so the while loop will execute. The block for this while loop starts from line 17 and ends on line 29, all the code in between there is executed after checking whether the condition set in the while loop is true.

On line 18 the variable sortExists exists is assigned false.

On line 19 a for loop is started, its execution block starts from line 20 and ends on line 28. This for loop helps in looping through the integers in the numArray array up to the integer in the second last position of the array. I will be giving you the reason why you only have to loop through up to the integer in the second last position of the array.

On line 21 a condition is set. If the integer in the position represented by the value of the counter is greater than the integer in the position represented by the value of the counter + 1 then swap the positions of those integers. The integer position swapping is done on line 23 up to line 25.
Here is the logic in swapping positions, The integer in the position represented by the counter is first assigned to the variable temporaryStorage for temporary storage (line 23), then the integer in the position represented by counter + 1 is assigned to the place holder for the integer that in position represented by counter (line 24). Finally the value that was temporarily stored in the variable temporaryStorage is assigned to the place holder which was formerly occupied by the integer in postion represented by counter + 1 (line 25). So the role of the variable temporaryStorage is to store one of the integers temporarily as position swapping is being done.
Earlier on I promised to tell you why when looping through the array as you are sorting you should stop looping on the integer in the second position of the array. Now is the time, Since two values are compared in each loop if the last value is also looped then it will be having no value to compare it with and that will lead to an array out of bound error. So to avoid the error that’s why you should stop the looping on the integer in the second last position of the array.
The variable counter plays a role of holding the increment value for the for loops.

On line 26, true is assigned to the variable sortExists. Note that the variable sortExists can only be assigned true if the set condition on line 21 is fulfilled, in other words if the code block from line 22 to line 27 is executed. If the condition on line 21 is not fulfilled the variable sortExists will be holding false as assigned on line 18. The role of the variable sortExists is to monitor whether there was a sort  – position swap that was done for a particular while loop executed. If the variable sortExists holds false then that means no sort  – position  swap was done and hence all integers in the array are already orders in ascending order as per the code in Sorting.c and the while loop will not be executed any further.

On line 31 printing of words “Array After Sorting – Ascending Order” is done. Two new line characters (\n) are inserted before those words to skip a line before printing the words. Also at the end of the words a new line character is added to push the content that will be printed later to a new line.

On line 32 to line 34, the sorted array is looped through while printing the integers.

Finally on line 36 there is a return statement.

That was sorting in ascending order. If you want to sort array integers in Descending order you will have to change something small in the code in Sorting.c. Replace the the greater than symbol (>) on line 21 to a less than symbol (<) and that will enable you to sort the array integers in descending order.

The following is the screenshot of the above C sorting programming in execution. The red border I put them to demarcate the outcomes of the program, anything outside it is not the result of the code above.

You realize I did not put comments in the code, that is because I was going to explained every bit of it in the explanation above. However on your side always comment out as you are writing your code so that you can make your code easy to follow along to you and anyone who will read it.

Category: C Programming   •   Tags: