Advertisement
Hydrase

selection sort

Aug 6th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. int main()
  4. {
  5. clrscr();
  6. int n,i,j,k,min,loc,temp,a[10];
  7. cout<<"\nEnter the value for n"<<endl;
  8. cin>>n;
  9. cout<<"enter values"<<endl;
  10. for(i=0;i<n;i++)
  11. {
  12. cin>>a[i];
  13. }
  14. for(k=0;k<n;k++)
  15. {
  16. min=a[k];
  17. loc=k;
  18. for(j=k+1;j<n;j++)
  19. {
  20. if(min>a[j])
  21. {
  22. min=a[j];
  23. loc=j;
  24. }
  25. }
  26. temp=a[k];
  27. a[k]=a[loc];
  28. a[loc]=temp;
  29. }
  30. cout<<"\nSelected array:"<<endl;
  31. for(i=0;i<n;i++)
  32. cout<<a[i]<<"\t";
  33. getch();
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement