fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n, NumList[10]={6,4,7,8,9,45,3,12,41,99};
  5. void tukar(int a,int b) //procedure for swap
  6. {
  7. int t;
  8. t = NumList[b];
  9. NumList[b] = NumList[a];
  10. NumList[a] = t;
  11. }
  12.  
  13. void Selection_sort()
  14. {
  15. cout<<"Data Before sorted : "<<NumList[0]<<","<<NumList[1]<<","<<NumList[2]<<","<<NumList[3]<<","<<NumList[4]<<","<<NumList[5]<<","<<NumList[6]<<","<<NumList[7]<<","<<NumList[8]<<","<<NumList[9]<<endl;
  16. int pos,i,j; //variabel pos digunakan untuk menaruh nilai sementara untuk nantinya ditempatkan di awal atau di akhir sesuai besar nilainya.
  17. for(i=0;i<n-1;i++)
  18. {
  19. pos = i;
  20. for(j = i+1;j<n;j++)
  21. {
  22. if(NumList[j] < NumList[pos]) pos = j;
  23. }
  24. if(pos != i) tukar(pos,i); //memanggil prosedur tukar
  25. cout<<NumList[0]<<","<<NumList[1]<<","<<NumList[2]<<","<<NumList[3]<<","<<NumList[4]<<","<<NumList[5]<<","<<NumList[6]<<","<<NumList[7]<<","<<NumList[8]<<","<<NumList[9]<<endl;
  26. }
  27. cout<<"Selection sort Finish!!"<<endl;
  28. }
  29.  
  30. int main() {
  31. n=10;
  32. Selection_sort();
  33. return 0;
  34. }
Success #stdin #stdout 0s 3144KB
stdin
Standard input is empty
stdout
Data Before sorted : 6,4,7,8,9,45,3,12,41,99
3,4,7,8,9,45,6,12,41,99
3,4,7,8,9,45,6,12,41,99
3,4,6,8,9,45,7,12,41,99
3,4,6,7,9,45,8,12,41,99
3,4,6,7,8,45,9,12,41,99
3,4,6,7,8,9,45,12,41,99
3,4,6,7,8,9,12,45,41,99
3,4,6,7,8,9,12,41,45,99
3,4,6,7,8,9,12,41,45,99
Selection sort Finish!!