Python selection sort
Consider the following array Give the sequence of the elements in the array after the Third pass of selection sort. (Ignoring the initial array, the following array)
[29, 72, 98, 13, 87, 66, 52, 51, 36]
Selection Sort algorithm sorts an array by repeatedly finding the minimum element in the unsorted array and putting it at the beginning. It will first find the smallest element in the array and swap it with the element in the first position, then it will find the second smallest element and swap it with the element in the second position, and it will keep on doing this until the entire array is sorted.
Given array A of size 9 is:
A= [29,72,98,13,87,66,52,51,36]
1. In the first pass, it'll look for minimum element in A[0...8]. Minimum element is 13 which is present at position A[3]. So, it will be placed at the beginning i.e. A[0] and element at A[0] will be swapped with A[3].
So , array after pass 1 will be:
A = [13,72,98,29,87,66,52,51,36]
2. In the second pass it'll look for minimum element in A[1...8]. Minimum element is 29 which is present at position A[3]. So, it'll be placed at A[1] and element at A[1] will be swapped with A[3].
So, array after pass 2 will be:
A = [13,29,98,72,87,66,52,51,36]
3. In the third pass, it'll look for minimum element in A[2...8]. Minimum element is 36 which is present at position A[8]. So it'll be placed at A[2] and element at A[2] will be swapped with A[8].
So, array after pass 3 will be:
A = [13,29,36,72,87,66,52,51,98]
Get Answers For Free
Most questions answered within 1 hours.