16,002,004 members
See more:
So,instead of finding the minimum only,I have to find the minimum value and the second minimum value and store it in the right place.
my code isnt working in all cases
for example
```#include <iostream>
{
for(int i=0;i<n-1;i=i+2){
int smallest,secondsmallest;

smallest=i;
secondsmallest=i+1;

for(int j=i+1;j<n;j++)
{

if(data[j]<=data[smallest]){
secondsmallest=smallest;
smallest=j;}

else if(data[j]<=data[secondsmallest])
secondsmallest=j;
}

if(secondsmallest!=(i+1))
Swap(data[i+1],data[secondsmallest]);

if(smallest!=i)
Swap(data[i],data[smallest]);

}
}

int main(){
int x[10]=[2,5,8,7,9,12,88,74,3,10]
for(int i=0;i<10;i++)
cout<<x[i]<<" ";

}```

What I have tried:

void Swap(int &a,int&b)

{int temp=a;
a=b;
b=temp;}

{
for(int i=0;i<n-1;i=i+2){
int smallest,secondsmallest;

smallest=i;
secondsmallest=i+1;

for(int j=i+1;j<n;j++)
{

if(data[j]<=data[smallest]){
secondsmallest=smallest;
smallest=j;}

else if(data[j]<=data[secondsmallest])
secondsmallest=j;
}

if(secondsmallest!=(i+1))
Swap(data[i+1],data[secondsmallest]);

if(smallest!=i)
Swap(data[i],data[smallest]);

}
}
Posted
Updated 18-Mar-17 6:41am
v2
[no name] 18-Mar-17 11:36am
If you sort a list of data you can get Minimum the second and the third and the...
What is the Problem?
Member 13067828 18-Mar-17 12:02pm
its not sorting correctly in all cases
Patrice T 18-Mar-17 12:08pm
Use Improve question to 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.
Member 13067828 18-Mar-17 12:29pm
I updated the function
Member 13067828 18-Mar-17 12:32pm
as for the example
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

## Solution 1

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.

Member 13067828 18-Mar-17 12:42pm
thats not the concept of Selection sort, I have to find the index of the smallest and second smallest, then swap them with their correct places

## Solution 2

C++
```for(int i=0;i<n-1;i=i+2){
int smallest,secondsmallest;

smallest=i;
secondsmallest=i+1;
```

Think about it: what happen when data[i] is not smaller than data[i+1] ?

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
By the way:
C++
`int x[10]=[2,5,8,7,9,12,88,74,3,10]`

is missing the ";" at the end of line.

Try with:
C++
`int x[10]=[2,1,4,3,6,5,8,7,10,9];`

to see the problem when data[i] is not smaller than data[i+1]

v2
Member 13067828 18-Mar-17 12:44pm
im just assuming "smallest" and "secondsmallest" thats why im calling a swap function ++ looping from j=i+1
Patrice T 18-Mar-17 12:48pm
Use the debugger and see what happen.
Member 13067828 18-Mar-17 13:20pm
I couldnt find :/
Patrice T 18-Mar-17 16:19pm