Https://hack.codingblocks.com/app/dcb/207 daily codebytes question selection sort

Question says to do do selecton sort i did passed two test cases where am i going wrong

#include
using namespace std;
#include<bits/stdc++.h>
int main() {
int N,min_idx;
cin>>N;
int A[N];
for(int i=0; i<N; i++)
cin>>A[i];
for(int i=0; i<N-1; i++)
{
for(int j=i+1; j<N; j++)
{
min_idx=i;
if(A[min_idx]> A[j])
{
min_idx=j;
}

	 }
	  swap(A[min_idx],A[i]);

 }
 for(auto s:A)
 cout<<s<<" ";
 

return 0;

}

hey @AyushKumar9032, you are initializing “min_idx=i” inside second for loop keep it before second for loop and after first for loop.

But why how will this affect my code

you are updating min_idx inside for loop but for every J’th iteration you again put min_idx=i,so you won’t get real min_idx in your code and your code will fail.

1 Like

@AyushKumar9032 if you have any other doubts you can ask me else please mark this doubt as resolved.Thankyou.