Get comprehensive solutions to your problems with IDNLearn.com. Ask anything and receive immediate, well-informed answers from our dedicated community of experts.

Two:
Using a linear search find the smallest element of the array and swap it for the first element of the array. Now the first element is in the sorted part of the array.
Now starting from the second element do a linear search to find the smallest element in the unsorted part (second index to the last index) and swap it for the second element of the array.
Now continue the steps above until you are starting at the end of the array. The array is now sorted.

Write the pseudo code then write the program in Java using Eclipse.


Sagot :

Answer:

A Simple Solution is to sort the array in increasing order. The first two elements in sorted array would be two smallest elements. Time complexity of this solution is O(n Log n).

A Better Solution is to scan the array twice. In first traversal find the minimum element. Let this element be x. In second traversal, find the smallest element greater than x. Time complexity of this solution is O(n).

The above solution requires two traversals of input array.

An Efficient Solution can find the minimum two elements in one traversal. Below is complete algorithm.

Algorithm:

Explanation: