Why can't I do the following for the selection sort as it shows correct output on custom input?

#include

using namespace std;

#define ll long long

int main() { ll i,n,j,temp;

cin>>n;

ll a[n];

for(i=0;i<=n-1;++i){

  cin>>a[i];

}

for(i=0;i<n-1;++i){

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

    if(a[j]<a[i]){
   	
    temp=a[j];
	
    a[j]=a[i];
		
    a[i]=temp;

}

else{

    continue;

  }

}

}

for(i=0;i<=n-1;++i){

      cout<<a[i]<<endl;

}

return 0;

}

hello @gargprachi1203
u can do this as well.
but then it is called bubble sort instead of selection sort

Isn’t bubble sort pair wise swapping? In this, I have transversed through the array to find the minimum element.

yeah , i m not saying it is incorrect . it is correct .
but what u r doing is u are swapping current element j with i if value at j is less than i .
in selction sort what we do is , we first find the index of minimum element and then we swap value at that index with current index i . (note in each interation only one swapping is their )

urs algorithm is performing multiple swapping in a single interation

Yes, but that doesn’t make it bubble sort and this problem is on HackerBlocks as well. It’s showing an error (wrong output) there. Perhaps, there is the wrong with the multiple swapping. It is an expensive operation. Am I correct?

yeah not exactly bubble but similar to it.

yeah multiple swapping may take more time

Thank you for your time, I worked it out :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.