Sorting in c
Sorting in c
elements in any particular order, which can be set ready for further processing by
algorithms available, which can be incorporated inside the code. The various
types of sorting methods possible in the C language are Bubble sort, Selection
sort, Quick sort, Merge sort, Heap sort and Insertion sort.
list. The term sorting states arranging of data in a particular manner usually
in ascending order. Though the way to sort the data is different in all of
Usually, in sorting, the program searches for the minimum number and
shifted that number to the beginning of the list and repeat the same
to the next space in the list right after the first index and this process keeps
on repeating until the sort list is obtained. This is the way sorting is done in
store the list of the elements that have to be sorted. For instance, in bubble
sort, the elements are stored in the single array and the values in the array
In the selection sort, the same array has been treated as two arrays where
the first array is considered to be vacant in order to tell the sorted values
while the second array holds the unsorted list. To serve the purpose of
sorting the array is used very often instead of holding the values in
individual variables. Among all of the algorithms, quick sort works very
quick and hence named quick sort. It takes much less time as compared to
Types of Sorting in C
The types of sorting in C are listed below.
1. Bubble Sort
Bubble sort may be defined as the sorting algorithm that follows the
approach of replacing the value in the first index with the smallest value in
the array and keep it repeating until the list is sorted. It is a very simple
way of performing sorting. In this way to sort the array, the value has to be
have been taken from the user. Once the program is compiled and run, it
will ask the user for the number of elements that they want to sort. Once
the number is provided, the program will ask the user to provide values
equivalent to the count that they have provided. The values will be stored
in the array and will be processed further using nested for loop together
The first smallest value found in the array has been moved to the first
index of the array and then the search begins again to find the other
smallest number. Once the next smallest number is found, it replaces the
value in the second index and the process keeps on repeating until the
Code:
#include <stdio.h>
int main()
int array[20];
scanf("%d", &total_count);
printf("Please enter %d integers that has to be sorted\n",
total_count);
scanf("%d", &array[counter]);
counter1++)
swap_var = array[counter1];
array[counter1] = array[counter1+1];
array[counter1+1] = swap_var;
order:\n");
printf("%d\n", array[counter]);
return 0;
The user has submitted the input 5 3 60 14 1 2 645. The algorithm has been
applied on the array consisting of values in the manner it is provided by the user
Output:
2. Selection Sort
The selection sort may be defined as another algorithm for sorting the list
in which the array is bifurcated into two arrays where the first array is
supposed to be empty while the second array consists of the unsorted list
of values. The program searches for the smallest values in the second
array and when the value is found, it has been moved to the beginning of
the first array that was empty. The approach is repeated again and the
next smallest values will be shifted to the second index of the first array.
The processes will keep on repeating until the second array became
empty.
algorithm. Once the program runs successfully, it will request the user to
input the count of values that they are willing to sort. Once the count is
obtained, the program will ask the user to input the values for the array that
has to be sorted. The value is then processed using nested for loop in
order to sort the numbers. The if condition checking has also been
The processes will be repeated until the first list is full of the sorted list.
Meanwhile, the programs keep its primary focus to check if the second
array is having value and if it is found positive, the program runs the
sorting algorithm again. Though it sorts the list in easy manner, it may take
a bit more time as compared to the other algorithms. But by the end, the
result it will generate will be the same as the other sorting algorithms.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int total_count,counter1,counter2,minimum,temp_value;
int a[20];
scanf("%d",&total_count);
for(counter1=0;counter1<total_count;counter1++)
scanf("%d",&a[counter1]);
for(counter1=0;counter1<total_count-1;counter1++)
minimum=counter1;
for(counter2=counter1+1;counter2<total_count;counter2++)
if(a[minimum]>a[counter2])
minimum=counter2;
if(minimum!=counter1)
temp_value=a[counter1];
a[counter1]=a[minimum];
a[minimum]=temp_value;
for(counter1=0;counter1<total_count;counter1++)
printf("%d ",a[counter1]);
getch();
On asking for the count of elements that has to be sorted, the user has provided
6 in the below output. Later the values that have been input are 25 65 36 86 96
45. These values are stored in the array which is expected to be bifurcated into
two arrays where one will be empty to store the sorted list and the other will be
having the unsorted list. After processing the input, the outcome was 25 36 45
65 86 96. This lost has been sorted using the selection sort. Once all the six
values have been moved to the first array in the sorted form, the second array
Output:
3. Quick Sort
Quicksort can be defined as the other algorithm for sorting the list in which
the approach is to divide the array in terms of greater than and less than
values until the entire values if divided into individuals forms. In this
algorithm, the value of the last index of the array has been selected as a
pivot and all the values smaller than pivot has been shifted to the array that
is expected to occur in the left of the value and the elements having a
higher value than the pivot are shifted to the right array. Again one pivot is
selected from the newly formed array that had the values less than the last
pivot value. Similarly, the values smaller than the new pivot will be shifted
to the array that will be left and the values more than the new pivot will be
programming language. Once the program runs, it will ask the user for the
number of elements that they want to sort. Based on the count, the for loop
will iterate estimated times to take the input from the user. The input will be
processed using the if conditions together with the for loop in order to
generate a sorted list. The array will keep on arranging the values using
the pivot value until all the values have been checked for the smallest
value.
The sorting done using this algorithm is way too faster as compared to the
other sorting algorithms and that’s why it has been named quick sort.
Quicksort is the only algorithm that leads to dividing the array until all the
values are separated into the individual arrays. They will be then added or
Code:
#include <stdio.h>
int main()
scanf("%d", &count);
sorted:\n");
for (counter = 0; counter < count; counter++)
scanf("%d", &element_list[counter]);
printf("\n");
return 0;
high)
pivot = low;
value1 = low;
value2 = high;
while (value1 < value2)
value1++;
>= low)
value2--;
temp = element_list[value1];
element_list[value1] = element_list[value2];
element_list[value2] = temp;
temp = element_list[value2];
element_list[value2] = element_list[pivot];
element_list[pivot] = temp;
quicksort_method(element_list, low, value2 - 1);
In the below output, the user confirmed that they will be submitting 6 values and
to form a list of sorted data. After providing the count, the values provided by the
user are 56, 35, 24, 86, 98, 2. The quicksort has been applied to these values
and the sorted list has been generated that has the value 2, 24, 35, 56,86,98.
Output:
4. Merge Sort
Merge sort may be defined as another sorting algorithm that performs the
sorting by segregating the array till last when it turns into an individual
value and then aggregating them in a manner so that it could turn into a
sorted array.
The process consumes a bit much time as compared to the other rival
it comes to sorting a large list, this algorithm works very fine and hence
preferred in developing the application that has to process the large list.
Code:
#include<stdio.h>
int main()
int val[100],chk,counter1;
scanf("%d",&chk);
sorted:\n");
for(counter1=0;counter1<chk;counter1++)
scanf("%d",&val[counter1]);
algo_merge_sort(val,0,chk-1);
printf("%d ",val[counter1]);
return 0;
int mid;
if(counter1<counter2)
mid=(counter1+counter2)/2;
algo_merge_sort(val,counter1,mid);
algo_merge_sort(val,mid+1,counter2);
perfrom_merge(val,counter1,mid,mid+1,counter2);
int temp_val[50];
int c1,c2,c3;
c1=counter11;
c2=counter22;
c3=0;
if(val[c1]<val[c2])
temp_val[c3++]=val[c1++];
else
temp_val[c3++]=val[c2++];
while(c1<=counter12)
temp_val[c3++]=val[c1++];
while(c2<=counter21)
temp_val[c3++]=val[c2++];
for(c1=counter11,c2=0;c1<=counter21;c1++,c2++)
val[c1]=temp_val[c2];
When the above code runs, it first asks the user to provide the number of
elements that they want to sort. Once the number has been submitted, they will
need to provide the values of equal count that they have provided initially. Once
the values have been submitted, the algorithm will hold those values in the array
and will process it to transform the array into the sorted array. After the array is
5. Heapsort
The heap sort can be defined as the sorting algorithm that works by
searching the maximum element in the list and place it to the last. The
algorithm performs the action recursively until the array gets sorted into the
ascending way.
It is very time taking the process to choose the maximum value and move
when it comes to sorting the large list. However, it works fine with the list
Code:
#include<stdio.h>
void form(int []);
int main()
int val[100],chk,counter,end,temp_val;
scanf("%d",&chk);
sorted:\n");
for(counter=1;counter<=chk;counter++)
scanf("%d",&val[counter]);
val[0]=chk;
form(val);
while(val[0] > 1)
end=val[0];
temp_val=val[1];
val[1]=val[end];
val[end]=temp_val;
val[0]--;
set_down(val,1);
}
for(counter=1;counter<=chk;counter++)
printf("%d ",val[counter]);
int counter,chk;
chk=val[0];
for(counter=chk/2;counter>=1;counter--)
set_down(val,counter);
int counter2,temp_val,chk,flag=1;
chk=val[0];
counter2=2*counter;
counter2=counter2+1;
else
temp_val=val[counter];
val[counter]=val[counter2];
val[counter2]=temp_val;
counter=counter2;
The working of this algorithm is the same as that of other sorting algorithms as it
also sorts the list in ascending order. When the above-written code runs, the user
has to submit the count of values that they will be sorting. Once the values are
submitted, the code will process them in order to turn the array into the sorted
one. The output will be shown eventually and it can be observed that the values
that have been submitted by the user has sorted in ascending order.
Output:
6. Insertion Sort
Insertion sort may be defined as the sorting algorithm that works by
moving the minimum value at the beginning of the list one at a time. This is
a very less efficient sorting algorithm and not found suitable to deal with
This approach of sorting the algorithm works very slowly and usually not
preferred in any of the applications. It can work good with the list that has
pretty few numbers of elements. For the applications, that have the
algorithm.
Code:
#include<stdio.h>
int main()
{
int counter1,counter2,chk,temp_val,val[100];
scanf("%d",&chk);
sorted:\n");
for(counter1=0;counter1<chk;counter1++)
scanf("%d",&val[counter1]);
for(counter1=1;counter1<=chk-1;counter1++)
temp_val=val[counter1];
counter2=counter1-1;
while((temp_val<val[counter2])&&(counter2>=0))
val[counter2+1]=val[counter2];
counter2=counter2-1;
val[counter2+1]=temp_val;
}
printf("\n Output generated after using insertion sort
\n");
for(counter1=0;counter1<chk;counter1++)
printf("%d ",val[counter1]);
return 0;
When the program runs, the user will have to input the number of values that
they need to sort. Afterward, the values entered by the user will be stored into the
array. They will then go under processing and by the use of for loop and
condition checking, the minimum value will be moved to the beginning in every
recursion and end up by generating a sorted array. The values will be displayed
Output:
Conclusion
The sorting algorithm is used to generate a sorted list which is a normal list
where all the values are sorted in a particular manner. The list has been used
very often in the actual application to bring some functionalities. In this article we
have covered bubble sort, selection sort, and quicksort while there are several
other algorithms like merge sort are also there that can be leveraged to generate
a sorted list. Among all of the sorting algorithms, quicksort works very fast and
helps to sort the list very quickly. The programs written here are basically to
are willing to implement the same in other programming languages, you can use
the same logic and the only thing that may vary can be the syntax and keywords.