Sorting Arrays In C Programming Examples

Published by Ubaid Khan on

Sorting arrays

Sorting is the process of arranging data in the listed order. Data which is in numeric form might be arranged either in ascending or in descending order. Similarly, character type data may be arranged in alphabetical order.

There is a different method to sort data into a list. The most commonly used method is

  1. Bubble sort
  2. Selection sort

Bubble Sort

The bubble sort method is used to arrange values of an array in ascending or in descending order.

Keep in mind that whenever to arrange an array in ascending order, only the two neighboring elements are compared rather than all at time. Through the exchange of elements the larger values slowly float or bubble up to the top. Similarly to arrange an array in descending order the smaller value slowly floats up to the top.

Bubble sort method is a slow process. It is used for sorting only a small amount of data. This method can be easily programmed.

To sort data in an array of n element, n-1 iteration is required. The following explains the sorting of the data in an ascending order using bubble sorting method.

In the first iteration, the value of the elements is interchanged if the value in the second element is greater than the value in the first element.

Similarly, the value in the second element of the array is compared with the third element and these values are interchanged if necessary. This process is repeated until the last element of the array. Thus at the end of the first iteration, the largest values move to the last position in the array.

In the second iteration, the above process is repeated. The values are compared up to the n-1 element of the array. In this iteration, the second-largest values move to the second last position in the array.

This process is repeated n-1 times in an array of n elements. At the end of the n-1 iteration, the data is arranged in ascending order.

To understand the bubbles sorting a numerical example is given below. Suppose the name of the array is ARR and it has four values in the following values.

Sorting arrays C 1

To sort this array in ascending order three n-1 iteration will be required. These are

Iteration 1

Sorting arrays C

In the first iteration, the value of element ARR[0] is compared with elements ARR[1]. Since 4 is less than 19 there will be no change in the list that is elements are not interchanged.

The value of element ARR[1] is compared with element ARR[2]. Since 19 is greater than 1 the values are interchanged.

The value of element ARR[2] is compared with element ARR[3]. Since 19 is greater than 3 the values are interchanged.

Thus at the end of the first iteration, the largest values move to the last position in the array.

Iteration 2

sorting array C

In the second iteration, the values of element ARR[0] is compared with element ARR[1]. Since 4 is greater than 1 the values are interchanged.

The values of the sound elements ARR[1] is compared with the third element ARR[2]. Since 4 is greater than 3 the values are interchanged.

At the end of the second iteration, the second-largest values move to the second last position in the array.

Iteration 3

In the second iteration, the value of element ARR[0] is compared with element ARR[1]. Since 1 is greater 3 the values are not interchanged. The array is already sorted in ascending order.

Selection Sort

This method is also used for sorting arrays in ascending or in descending order. If an array which has n elements, n-1 iteration are required to sort the array.

The following methods selection sort method is used to sort an array in ascending order.

In the first iteration, the value in the first element is assumed to be the 3 smallest. Then the next smallest is found in the array. This value is interchanged with the first element. Now the first element of the array has the smallest value.

In the second iteration, the smallest value from the second element to the last element of the list is found. This value is interchanged with the second element of the array.

This process is repeated and repeated up to the entire array is sorted.

In the following example, the array that has 4 elements is sorted by selection sort. There will be also three iterations to sort the list in ascending order with the selection sort method.

The array is

Sorting arrays C. Selection sort array

Iteration 1

Find out the smallest value from the list starting from the first element to the last element of the list. Here the smallest value is 1 in location 3. Note that the address of element that has smallest value. Interchanged the value of the first element with the third element that is swap values of ARR[0] and ARR[2].

Sorting arrays C. selection sort array

Iteration 2

Find out the smallest value from the list starting from the second element to the last element. The smallest value is 4 at location 3. Note the address of element that has the smallest value. Interchange the value of the second element with the third element that is swap values of ARR[1] and ARR[2].

Sorting arrays C. Selection sort arrays

Iteration 3

Find out the smallest value from the list starting from the third element to the last element. The smallest value is 13 in location 4. Note that the address of element that has the smallest value. Interchange the value of the fourth element with the third element that is swap values of ARR[2] and ARR[3].

Sorting arrays C. Selection sort array

Source:  Programming With C++ (Aikman Series)


Ubaid Khan

hello friends, we are teh team of engineers and blogging is our passion. our team consists of Tariq Naveed, Izhar, and Ubaid