Selection Sort Algorithm in Programming
Selection Sort Algorithm in Programming
Selection sort algorithm starts by compairing first two elements of an array and swapping if
necessary, i.e., if you want to sort the elements of array in ascending order and if the first element
is greater than second then, you need to swap the elements but, if the first element is smaller than
second, leave the elements as it is. Then, again first element and third element are compared and
swapped if necessary. This process goes on until first and last element of an array is compared.
This completes the first step of selection sort.
If there are n elements to be sorted then, the process mentioned above should be repeated n-1
times to get required result. But, for better performance, in second step, comparison starts from
second element because after first step, the required number is automatically placed at the first
(i.e, In case of sorting in ascending order, smallest element will be at first and in case of sorting
in descending order, largest element will be at first.). Similarly, in third step, comparison starts
from third element and so on.
A figure is worth 1000 words. This figure below clearly explains the working of selection sort
algorithm.
1
data[steps]=data[i];
data[i]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Output
Note: Though this program is in C, selection sort algorithm can be similarly used in other
programming language as well.
Selection sort algorithm is easy to use but, there are other sorting algorithm which perform better
than selection sort. Specially, selection sort shouldn't be used to sort large number of elements if
the performance matters in that program.
This tutorial is intended to provide you information about what insertion sort algorithm is and
how to implement it in programming rather than it's technical stuff, properties and comparision
with other sorting algorithm. If you know what insertion sort algorithm is then, visit this page to
learn about properties of insertion sort and comparision with other sorting algorithm.
2
Explanation
1. Step 1: The second element of an array is compared with the elements that appears before it
(only first element in this case). If the second element is smaller than first element, second
element is inserted in the position of first element. After first step, first two elements of an array
will be sorted.
2. Step 2: The third element of an array is compared with the elements that appears before it (first
and second element). If third element is smaller than first element, it is inserted in the position
of first element. If third element is larger than first element but, smaller than second element, it
is inserted in the position of second element. If third element is larger than both the elements, it
is kept in the position as it is. After second step, first three elements of an array will be sorted.
3. Step 3: Similary, the fourth element of an array is compared with the elements that appears
before it (first, second and third element) and the same procedure is applied and that element is
inserted in the proper position. After third step, first four elements of an array will be sorted.
If there are n elements to be sorted. Then, this procedure is repeated n-1 times to get sorted list of
array.
3
return 0;
}
Note: Though this program is in C, insertion sort algorithm can be similarly used in other
programming language as well.
Output
4
}
Bubble sort algorithm starts by comparing the first two elements of an array and swapping if
necessary, i.e., if you want to sort the elements of array in ascending order and if the first element
is greater than second then, you need to swap the elements but, if the first element is smaller than
second, you mustn't swap the element. Then, again second and third elements are compared and
swapped if it is necessary and this process go on until last and second last element is compared
and swapped. This completes the first step of bubble sort.
If there are n elements to be sorted then, the process mentioned above should be repeated n-1
times to get required result. But, for better performance, in second step, last and second last
elements are not compared becuase, the proper element is automatically placed at last after first
step. Similarly, in third step, last and second last and second last and third last elements are not
compared and so on.
A figure is worth a thousand words so, acknowledge this figure for better understanding of
bubble sort.
Here, there are 5 elements to the sorted. So, there are 4 steps.
5
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1]) /* To sort in descending order, change > to <
in this line. */
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}
Note: Though this code is in C programming, this technique can be applied in any programming
to sort elements of an array.
Though bubble sort algorithm is quite popular, there are many other better algorithm than bubble
sort. Specially, bubble sort should not be used to sort large data if performance matters in that
program.