Skip to content

Instantly share code, notes, and snippets.

@teramuza
Created May 9, 2020 13:54
Show Gist options
  • Save teramuza/69b0393522ccc1a1c8f090c5b43cea97 to your computer and use it in GitHub Desktop.
Save teramuza/69b0393522ccc1a1c8f090c5b43cea97 to your computer and use it in GitHub Desktop.
per8 - struktur data
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int Nilai[20];
int i, k, N;
int temp, jarak, s;
cout << "Masukan banyak bilangan: ";
cin >> N;
for(i = 0; i < N; i++) {
cout << "Elemet ke-" << i << ": ";
cin >> Nilai[i];
}
cout << "\nData sebelum diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
//sorting
jarak = N/2;
cout << "\n\nJarak = " << jarak;
while(jarak >= 1) {
do {
s = 0;
for(i = 0; i <= (N - jarak) - 1; i++) {
k = i + jarak;
if(Nilai[i] > Nilai[k]) {
temp = Nilai[i];
Nilai[i] = Nilai[k];
Nilai[k] = temp;
s = 1;
cout << endl;
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
}
} while(s != 0);
jarak /= 2;
cout << "\nJarak = " << jarak;
}
cout << "\n\nData setelah diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int Nilai[20];
int i, k, N;
int temp, jarak, s;
cout << "Masukan banyak bilangan: ";
cin >> N;
for(i = 0; i < N; i++) {
cout << "Elemet ke-" << i << ": ";
cin >> Nilai[i];
}
cout << "\nData sebelum diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
//sorting
jarak = N/2;
cout << "\n\nJarak = " << jarak;
while(jarak >= 1) {
do {
s = 0;
for(i = 0; i <= (N - jarak) - 1; i++) {
k = i + jarak;
if(Nilai[i] < Nilai[k]) {
temp = Nilai[i];
Nilai[i] = Nilai[k];
Nilai[k] = temp;
s = 1;
cout << endl;
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
}
} while(s != 0);
jarak /= 2;
cout << "\nJarak = " << jarak;
}
cout << "\n\nData setelah diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int Nilai[20];
int i, j, N;
int temp;
cout << "Masukan banyak bilangan: ";
cin >> N;
for(i = 0; i < N; i++) {
cout << "Elemet ke-" << i << ": ";
cin >> Nilai[i];
}
cout << "\nData sebelum diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
//sorting
cout << "\n\nProses pengurutan:";
for(i = 1; i < N; i++) {
temp = Nilai[i];
j = i - 1;
while((temp <= Nilai[j]) && (j >= 1)) {
Nilai[j+1] = Nilai[j];
j--;
}
if(temp >= Nilai[j])
Nilai[j+1] = temp;
else {
Nilai[j+1] = Nilai[j];
Nilai[j] = temp;
}
cout << endl;
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
cout << "\n\nData setelah diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int Nilai[20];
int i, j, N;
int temp;
cout << "Masukan banyak bilangan: ";
cin >> N;
for(i = 0; i < N; i++) {
cout << "Elemet ke-" << i << ": ";
cin >> Nilai[i];
}
cout << "\nData sebelum diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
//sorting
cout << "\n\nProses pengurutan:";
for(i = 1; i < N; i++) {
temp = Nilai[i];
j = i - 1;
while((temp > Nilai[j]) && (j >= 1)) {
Nilai[j+1] = Nilai[j];
j--;
}
if(temp <= Nilai[j])
Nilai[j+1] = temp;
else {
Nilai[j+1] = Nilai[j];
Nilai[j] = temp;
}
cout << endl;
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
cout << "\n\nData setelah diurut: \n";
for(int j = 0; i < N; i++)
cout << setw(4) << data[j];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment