If you task is to sort the data, and then get the smallest two elements, then stop looking for the smallest and the second smallest, and concentrate on getting the sort to work. Remove all "smallest" code, and just sort.

When you have that working, the smallest two are the first and second elements of the resulting array...

If you aren't supposed to sort it, then you can find the smallest and second smallest in a single pass through the data: Create a smallest and secondSmallest variables, and set them both to the maximum value an integer can hold.

Pass through the data and compare each value (V) with the second smallest (S2). If it's smaller, compare it to the smallest (S1). Then:

if V >= S2 ignore it.

if V < S2 and V >= S1 set S2 to V.

if V < S2 and V < S1 set S2 to S1, set S1 to V.

At the end of the loop, you have the smallest and second smallest.

16,002,004 members

What is the Problem?

Improve questionto update your question.Explain that and example data (input and output that exhibit the problem.

Update your code to a full function, so we can run it.

its not sorting this array x[8]={2,7,9,5,6,7,8,10};

the output is 2 5 6 7 8 9 7 10