lspci

Bubble Sort With Array

Mar 28th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. main(){
  5. int number[5]={
  6. 3,5,2,7,4
  7. };
  8. int max = 0;
  9.  
  10. //Menampilkan isi array sebelum diurutkan
  11. cout<<"Sebelum diurut : ";
  12. for(int i=0;i<5;i++){
  13. cout<<number[i];
  14. }
  15.  
  16. for(int i=1;i<5;i++){
  17. for(int j=4;j>=i;j--){
  18. if(number[j]>number[j-1]){
  19. max=number[j];
  20. number[j]=number[j-1];
  21. number[j-1]=max;
  22. }
  23. }
  24. }
  25.  
  26. //menampilkan isi array setelah diurutkan
  27. cout<<"\n\n Data setelah diurutkan : ";
  28. for(int i=0;i<5;i++){
  29. cout<<number[i]<<" ";
  30. }
  31. return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment