0% found this document useful (0 votes)
138 views5 pages

Selection Sort and Insertion Sort

Selection sort and insertion sort are simple sorting algorithms with average and worst case time complexities of Ο(n2), where n is the number of items. Selection sort works by iterating through the list and swapping elements to put the minimum element in the sorted position. Insertion sort maintains a sorted sub-list, taking unsorted items and inserting them into the proper place within the sorted portion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views5 pages

Selection Sort and Insertion Sort

Selection sort and insertion sort are simple sorting algorithms with average and worst case time complexities of Ο(n2), where n is the number of items. Selection sort works by iterating through the list and swapping elements to put the minimum element in the sorted position. Insertion sort maintains a sorted sub-list, taking unsorted items and inserting them into the proper place within the sorted portion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Selection Sort and Insertion Sort

The Concept:

Selection Sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the
unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This process continues moving unsorted
array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are of
Ο(n2), where n is the number of items.

Insertion Sort is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained


which is always sorted. For example, the lower part of an array is maintained to be sorted. An
element which is to be 'insert' ed in this sorted sub-list, has to find its appropriate place and then it
has to be inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into the sorted
sub-list (in the same array). This algorithm is not suitable for large data sets as its average and
worst case complexity are of Ο(n2), where n is the number of items.
How the sorting is done step by step.
Selection Sort :
Consider the following depicted array as an example.

12 31 25 8 33 17 40 42
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 12 is
stored presently, we search the whole list and find that 8 is the lowest value.

12 31 25 8 33 17 40 42

So we replace 12 with 8. After one iteration 8, which happens to be the minimum value in the list, appears
in the first position of the sorted list.

8 31 25 12 33 17 40 42
For the second position, where 31 is residing, we start scanning the rest of the list in a linear manner.

8 31 25 12 33 17 40 42
We find that 12 is the second lowest value in the list and it should appear at the second place. We swap
these values.

8 31 25 12 33 17 40 42
After two iterations, two least values are positioned at the beginning in a sorted manner.

8 12 25 31 33 17 40 42
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process 

8 12 25 31 33 17 40 42

8 12 25 31 33 17 40 42

8 12 19 31 33 25 40 42

8 12 19 31 33 25 40 42

8 12 19 25 33 31 40 42

8 12 19 25 33 31 40 42

8 12 19 25 33 31 40 42

8 12 19 25 31 35 40 42

8 12 19 25 31 33 40 42

Insertion Sort
We take an unsorted array for our example.

12 31 25 8 33 17 40 42
Insertion sort compares the first two elements.

12 31 25 8 33 17 40 42
It finds that both 12 and 31 are already in ascending order. For now, 12 is in sorted sub-list.

12 31 25 8 33 17 40 42
Insertion sort moves ahead and compares 31 with 25.

12 31 25 8 33 17 40 42
And finds that 31 is not in the correct position.

12 31 25 8 33 17 40 42
It swaps 31 with 25. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-
list has only one element 12, and 25 is greater than 12. Hence, the sorted sub-list remains sorted after
swapping.

12 25 31 8 33 17 40 42
By now we have 12 and 25 in the sorted sub-list. Next, it compares 31 with 8.

12 25 31 8 33 17 40 42
These values are not in a sorted order.

12 25 31 8 33 17 40 42
So we swap them.

12 25 8 31 33 17 40 42
However, swapping makes 25 and 8 unsorted.

12 25 8 31 33 17 40 42
Hence, we swap them too.

12 8 25 31 33 17 40 42
Again we find 12 and 8 in an unsorted order.

12 8 25 31 33 17 40 42
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.

8 12 25 31 33 17 40 42
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some
programming aspects of insertion sort.

The algorithm,focus on the crucial part of swamping


Selection Sort Insertion Sort
Step 1 − Set MIN to location 0 Step 1 − If it is the first element, it is already
Step 2 − Search the minimum element in sorted. return 1;
the list Step 2 − Pick next element
Step 3 − Swap with value at location MIN Step 3 − Compare with all elements in the
Step 4 − Increment MIN to point to next sorted sub-list
element Step 4 − Shift all the elements in the sorted
Step 5 − Repeat until list is sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

How it is different from other algorithms

Sorting Time Complexity Space Complexity Comparison Based sorting


Algorithm
Best Case Average Worst Worst
Case Case Case
Selection Best, average and worst
Sort Ω(N2) Θ(N2) O(N2) O(1) case time complexity: n^2
which is independent of
distribution of data.
Insertion Ω(N) Θ(N2) O(1) Average and worst case
Sort
O(N2)
time complexity: n^2
Best case time complexity:
n when array is already
sorted.
Worst case: when the
array is reverse sorted.
Bubble Sort Ω(N) Θ(N2) O(N2) O(1) Average and worst case
time complexity: n^2
Best case time complexity:
n when array is already
sorted.
Worst case: when the
array is reverse sorted.
Merge Sort Ω(N log Θ(N log N) O(N log O(N) Best, average and worst
N) N) case time complexity:
nlogn which is
independent of distribution
of data.

Example through animation

You might also like