The Interview Questions Page
Every Fresher, even experienced developers have to deal with programming assignments during their selection process. In this page we post questions that are being asked in recent times.
Please bookmark this page and visit it often. Please follow our blog to get regular updates.
Sort an array using the Selection Sort Algorithm?
The Selection Sort Algorithm works by finding the largest o smallest element in an array and then placing it in the correct position, then searching for the next element and positioning it in the same manner. This procedure is repeated till the whole array is sorted.
Here is a sample run doing an ascending order sort on an array.
Let us assume this to be the input array
a=4,3,7,-5,8
The smallest element in the array is 4,3,7,-5,6. We interchange it with the element at the 0th position.
The array is sorted up to the 0th position now.
a= -5,3,7,4,6. Search the smallest element now starting at position 1
The smallest is -5,3,7,4,6. Interchange it with the element at the 1st position now. Since it is already at the 1st position nothing happens.
a=-5,3,7,4,6
Search for the minimum element in the remaining array now.
-5,3,7,4,6.
Swap with the element at the 2nd position now.
a=-5,3,4,7,6
Search for the smallest element in the remaining array now.
-5,3,4,7,6
Swap with the element at position 3 now.
-5,3,4,6,7
The array is sorted now.
Try solving this today or look up the solution here tomorrow.
Links to the solutions on Git
Python Code for Selection Sort
C Code for Selection Sort
Contact Varanasi Software Junction for Software Development and Training.
Find the largest non decreasing sequence in an array?
Examples:
- a=[1,2,0,6,8,9,3,4]. Longest increasing sequence is [0,6,8,9]
- a=[1,5,3,5,0,9,0]. We have three increasing sequences and you can pick anyone.
0 Comments