SELECTION SORT
- is a sorting algorithm that works by repeatedly selecting the smallest or largest element from the unsorted portion of the list to move it to the sorted portion of the list.
So basically in a selection sort, the input array is divided into two parts: sorted and unsorted part.
Then the algorithm repeatedly finds the minimum element in the unsorted part and swaps it with the leftmost element of the unsorted part, expanding the sorted part by one element.
For example if u look at this array: arr{} = {64, 25, 12, 22 and 11}
For the first position in the sorted array the while array has the index 0-4 sequentially. It is clear 11 is the lowest value, so replace 64 with 11. After one iteration 11 appears in the first position of the sorted list.
INSERTION SORT
is a sorting algorithm where the array is split into a sorted and unsorted part. Values from the unsorted part are placed at the correct position in the sorted part.
Example
arr{} = {12, 12, 13, 5, 6}
First pass
The first two elements are compared in the insertion sort.
12 is greater than 11 so 12 is not in the correct position. So swap 11 and 12.
So 11 is stored in a sorted sub array.
And so on.
The main difference between the two are that selection sort scans unsorted part to find Min element, while insertion scans sorted part to find the correct position to place the element. Selection sort requires fewer swaps than insertion but more comparisons.
Hope that clarified your question!!
Edit: OK ima head off to bed BTW
@SadCeliac gl on paper 2 tomorrow!!!! That done and we'll be finished with English for good