fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void swap(int a[],int n,int h);
  5. int main()
  6. {
  7. int n,a[50],loc=0,temp=0;
  8. //cout<<"enter size of array";
  9. cin>>n;
  10. //cout<<"enter elements";
  11. for(int i=0;i<n;i++)
  12. {
  13. cin>>a[i];
  14. }
  15. //sort(a,n);
  16. //for(int i=0;i<n;i++)
  17. //cout<<a[i]<<" ";
  18.  
  19. for(int i=0;i<n;i++)
  20. {
  21. temp=a[i];
  22. for(int j=i+1;j<n;j++)
  23. {
  24. if(a[j]<temp)
  25. {
  26. loc=j;
  27. temp=a[j];
  28. }
  29. }
  30. swap(a,i,loc);
  31. }
  32. for(int i=0;i<n;i++)
  33. cout<<a[i]<<" ";
  34. }
  35.  
  36. void swap(int a[],int k,int l)
  37. {
  38. int temp=a[k];
  39. a[k]=a[l];
  40. a[l]=temp;
  41. }
  42.  
  43.  
Success #stdin #stdout 0s 16048KB
stdin
5
1 3 5 2 1
stdout
1 1 2 3 5