lspci

Bubble Sort

Mar 28th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #define N 20
  4. using namespace std;
  5.  
  6. int bubble(int n);
  7. int i,j,A[N];
  8.  
  9. int main()
  10. {
  11. int jml;
  12. cout<<"\t METODE BUBBLE SORT \n\n";
  13. cout<<"Masukkan jumlah bilangan: ";
  14. cin>>jml;
  15. cout<<endl;
  16.  
  17. // input data
  18. for (i=0;i<jml;i++){
  19. cout<<"Bilangan ke "<<i+1 <<" : ";
  20. cin>>A[i];
  21. }
  22. cout<<endl;
  23. // mengurutkan data
  24. bubble(jml);
  25. // menampilkan data
  26. cout<<"Data yang sudah terurut : \n";
  27. for (i=0;i<jml;i++){
  28. cout<<A[i]<<endl;
  29. }
  30. }
  31. // fungsi bubble
  32. int bubble(int n){
  33. int temp;
  34. for (i=1;i<=n-1;i++){
  35. for (j=i;j<n;j++){
  36. if (A[i-1]>A[j]){
  37. temp = A[i-1];
  38. A[i-1] = A[j];
  39. A[j] = temp;
  40. }
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment