Reputation:
What sort's name means? I know how select sort works , but I can't understanding why select sort is select sort.(insert sort, bubble sort all same) Please let me know!
Upvotes: 0
Views: 100
Reputation: 55619
Selection sort repeatedly looks for and selects the smallest value to construct the sorted output.
Insertion sort repeatedly inserts the next value into the sorted subarray.
Bubble sort repeatedly bubbles (:|) elements up by comparing and swapping adjacent elements.
More specifically:
Selection sort searches the entire array to find the smallest value at every step, so it construct the final output right away (no need to shift anything, like insertion sort does).
For 4 5 3 7 1 8
, we'll start by selecting the smallest element (1
) and placing it in the first position, then we'll select the next smallest element (3
) and place it in the second position, etc.
Insertion sort processes the elements in the order they appear in the unsorted array, inserting each next element into the correct spot of the sorted section by shifting the other elements to make room for it.
For 4 5 3 7 1 8
, we'll start by saying 4
is our sorted array, then insert 5
(for which we don't need to do anything), then insert 3
(which involves shifting 4
and 5
to make room), etc.
Bubble sort only compares and swaps adjacent elements.
You can say selection sort does insertion and insertion sort does selection, but those parts are fairly trivial (inserting at the end and selecting the very next element) - the selection and insertion parts are more significant in the algorithm with the corresponding name.
Bubble sort is a bit more chaotic, so I wouldn't really say there's insertion or selection going on there.
Upvotes: 3