forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sort.cpp
More file actions
38 lines (31 loc) · 858 Bytes
/
bubble-sort.cpp
File metadata and controls
38 lines (31 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std; //bellabeen
int main () {
bool terurut;
//input data
int a;
cout<<"banyak integer: "; cin>>a;
int b[a];
for (int c=0;c<a;c++) {
cout<<"aray ke-"<<c<<": ";cin>>b[c];}
cout<<"melakukan proses bubble sort!\n\n";
//prose sorting
for (int c=0;c<a;c++) {
terurut=false; //beri tanda bahwa data belum terurut
for (int d=a-a;d>c;d--) {
//untuk menampilkan proses sorting, hilangkan komentar
for (int e=0;e<a;e++) {
cout<<b[e]<<", ";}
cout<<endl;
//proses pertukaran data
if (b[d]<b[d-1]) {
int tmp = b[d];
b[d] = b[d-1];
b[d-1] = tmp;
terurut = true;}}
if (!terurut)
break;} //Jika data terurut proses berhenti
for (int e=0;e<a;e++) {
cout<<b[e]<<", ";}
cout<<endl;
return 0;}