Anda di halaman 1dari 3

Nama

: Aisyah
Muhayani

i = j - 1;

Nim : 11140940000026

while (i > -1 && A[i] > key){


A[i+1] = A[i];

INSERTION SORT

i--;

#include <iostream>

#include <ctime>

A[i+1] = key;

#include <cstdlib>

}
clock_t t_end = clock();

using namespace std;

int main(){

cout << "Barisan Terurut: ";


for(int i=0; i < n; i++){

int
A[20]; /
/n=20,40,60,80,100,1000,10000,100
000

cout << A[i] <<" ";


}

int n;

cout << endl;

int key, i;

cout << "waktu proses: "


<< (double)(t_end - t_start)/
CLOCKS_PER_SEC*1000

srand(time(NULL));
for(int i=0; i < 20; i++){
A[i] = rand() % 8 + 1; //jumlah
ada 8
}

n = (sizeof(A)/sizeof(*A));

cout << "Barisan tidak Terurut: ";


for(int i=0; i < n; i++){
cout << A[i] << " ";
}
cout << endl;

clock_t t_start = clock();

for (int j = 1; j < n; j++){


key = A[j];

<< " ms ";


return 0;
}
Waktu proses yang dibutuhkan saat
n = 20 adalah 0s dan waktu
eksekusi saat n=20 adalah 0,063 s.

Hub antara n dengan waktu eksekusi (s)

A[k]
= A [k-1];
A[k-

Tabel Insertion Sort


n
o

1
2
3
4
5
6
7

20
40
60
80
100
1000
1000
0
1000
00

Waktu
proses
(ms)
0
0
0
0
0
0
234

Waktu
eksekusi (s)

23549

134,209

1] = x;
}
}
}

0,063
0,266
0,156
0,172
0,283
1,143
15,810

clock_t t_end = clock();


cout<<"\nBarisan
terurut : "<<endl;
for(int i = 0; i <n; i++)
cout<<A[i]<<" ";
cout << endl;
cout << "waktu proses: "
<< (double)(t_end t_start)/
CLOCKS_PER_SEC*1000
<< " ms ";
return 0;
}

BUBBLE SORT
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int A[20]; //
n=20,40,60,80,100,1000,1000
0,100000
int n, x;
int key, i;

Waktu proses yang dibutuhkan saat


n = 20 adalah 0s dan waktu
eksekusi saat n=20 adalah 0,071 s.

Hub antara n dengan waktu eksekusi (s)

srand(time(NULL));
for(int i=0; i < 20; i++){
A[i] = rand() % 8 + 1;
}
n = (sizeof(A)/sizeof(*A));
cout<<"\nBarisan
belum terurut : "<<endl;
for(int i = 0; i<n; i++)
cout<<A[i]<<" ";
cout << endl;
clock_t t_start = clock();
for(int i = 0; i<=n ; i++){
for(int k =1;
k<n; k++){
if(A[k] <
A[k-1]){
x=
A[k];

Column2

Tabel Bubble Sort


n
o

1
2
3
4
5
6
7

20
40
60
80
100
1000
1000
0

Waktu
proses
(ms)
0
0
0
0
0
15
1375

Waktu
eksekusi (s)
0,071
0,120
0,196
1,101
0,194
1,451
18,618

1000
00

139853

277,560

Hub antara insertion sort & bubble sort dgn waktu eksekusi

Insertion sort
Bubble
(s)
sort (s)
20
0,063
0,071
40
0,266
0,12
60
0,156
0,196
80
0,172
1,101
100
0,283
0,194
1000
1,143
1,451
10000
15,81
18,516
100000
134,209
277,56

KESIMPULAN:
Insertion sort

Column1

Berdasarkan grafik diatas,


terlihat bahwa waktu eksekusi
terjadi saat n nya diatas 1000
dan berdasarkan grafik diatas
terlihat bawa proses insertion
sort lebih cepat dari pada bubble
sort.

Anda mungkin juga menyukai