In selection sort algorithm, list is divided into 2 lists: sorted & unsorted list.
In each iteration, smallest element is selected from unsorted list and appended to the sorted list.
While starting the scan of list, assume that the first element is the smallest, then compare it with the rest of the elements in the list, save the index of smallest element along the way. Once all the elements are scanned, swap the starting index element with the minimum index element (if the indices are different).
Here is a Java implementation of a selection sort algorithm:
In the next post, we will look at the time & space complexity of the selection sort algorithm.
No comments:
Post a Comment