## Sorting

Sorting is a process of ordering individual elements of a list according to their proper rank, either in ascending or descending order. We can easily sort a list of elements by means of iteration /loop and if-condition check statements. Sorting algorithms can be implemented by any programming languages. One outer loop and one inner loop inside the outer loop will do the purpose.

``` 1  for(i = 0; i < element_count; i++)
2  {
3    for(j = i + 1; j < (element_count - 1); j++)
4    {
5      if(element[i] < element[j])
6      {
7        swap_element (i, j);
8      }
9    }
10  }```

When the element count of the list is small the number of iteration will be less and as the number grows the iteration count also grows. Now suppose we have a record of elements inside a database having count of several thousands. The problem comes with the performance of the sorting. Computer takes much longer time to sort the elements in the list. Another problem comes when the size of individual elements are large and total number of elements are also large that the entire list of elements cannot be fitted in main memory for sorting at one go.

Therefore the sorting mechanism has been divided into are two main categories :-

• Internal sorting
• External sorting

## Internal sorting

Internal sorting is done by loading all the elements in the main memory.

## External sorting

When individual element size is more and number of elements are large enough to hold all the elements in main memory external sorting is used. External sorting loads a portion of elements from secondary memory (like HDD) in main memory and sorts and then saves back to secondary storage. Later all the individual sorted fragments are merged in the main group.

We are discussing mainly internal sorting here. There are several algorithms used for this purpose each one has its own optimization techniques to execute the sort task in minimum CPU cycle thus to save time and energy.

## Sorting algorithms

There are the several internal sorting used in practical fields.

Merge sort can be used for external sorting also. In that case, a subset of the entire list of the elements are loaded in main memory and sorted at a time.

These sorting algorithms are one of the most used in computer science. Each algorithm has its own advantage and disadvantage and complexity. Reader must have a good understanding of complexity and big O notation before he/she can understand the efficiency of each algorithm. We will discuss each algorithm in details in the next few sections. We will explain the mechanism and will give an example with code in C language to understand the algorithm deeply.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

0

Similar topics related to this section