Tugas VIII Model Shorting
Soal
- Buatlah 1 contoh program insertion sort.
- Tuliskan pengertian sorting menurut pendapat anda masing-masing.
- Buatlah contoh program bouble sort dalam bahasa pascal
- Tuliskan algoritma quick rekursif.
- Sebutkan dan jelaskan metode-metode sorting.
Contoh Program Insertion Sort.
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <iomanip>
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
using namespace std;
int main()
{
int x[5];
int i;
int temp;
int minindex;
int j;
if (system("CLS")) system("clear");
cout << "
>> Program Selection Sort << \n" << endl;
cout << "masukkan
nilai x :\n";
for (i = 0; i<5; i++)
{
cout << "x[" << i << "]
= "; cin >> x[i];
}
cout << "\n Data
sebelum di sort :";
for (i = 0; i<5; i++)
{
cout << setw(4) << x[i];
}
for (i = 0; i<5 - 1; i++) //perulangan
iterasi
{
minindex = i;
for (j = i + 1; j<5; j++) //perulangan
membandingkan data
{
if (x[minindex]>x[j])
{
minindex = j;
}
}
temp = x[i];
x[i] = x[minindex];
x[minindex] = temp;
}
cout << "\n Data setelah di sort
:";
for (i = 0; i<5; i++)
{
cout << setw(4) << x[i];
}
getchar();
cout << endl;
system("pause");
}
0 komentar: