loading...
Contoh Soal Array Algoritma
Holla Mina ! Mimin punya tugas nih , siapa tau ada yang butuh juga kan.Nah tugas tentang array nih dengan cara Bubble sort dan Selection sort nya yang tertera dibawah ini !
Given array with element as follows
1.Write improved bubble sort steps of variables [i],[j],L[i], and [switched] of the array above!
2.Write selection sort steps of variables [i],[j],L[i], and idx_min of the array above!
3.Write your opinion : is improved bubble sort is better in terms of performance than selection sort?
Answer :
1. Algoritma Bubble Sort dengan Switched
void bubbleSort(input/output L:Larik, input n:integer)
Deklarasi
i : integer
j : integer
temp : integer
switched : boolean
Deskripsi
i <--0
while (switched = true) do
switched<-- false
j<--0
while(j<=(n-1)-i) do
if (L[j]>L[j+1]) then
temp<-- L[j]
L[j] <-- L[j+1]
L[j+1] <-- temp
switched<-- true
endif
j++
endwhile
i++
endwhile
switched<-- true
1. Step 1
i = 0 & switched = true & n=7
while (switched==true) -> Accepting state then
switched=false
j=0
while(0<=(7-1)-0) so while(0<=6) -> Accepting state
if(L[0]>L[0+1]) -> L[0]=7 and L[1]=4 so if (7>4) -> True statement
temp=L[0] -> temp =7
L[0]=L[1] -> L[0] = 4
L[1] = 7
Switched=true
Stop if
2. J++ so now j=1
While(1<=6) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=7 and L[2]=1 so if (7>1) -> True statement
temp=L[1] -> temp = 7
L[1]=L[2] -> L[1] = 1
L[2] = 7
Switched=true
Stop if
3. J++ so now j=2
While(2<=6) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=7 and L[3]=5 so if (7>5) -> True statement
temp=L[2] -> temp = 7
L[2]=L[3] -> L[2] = 5
L[3] = 7
Switched=true
Stop if
4. J++ so now j=3
While(3<=6) -> Accepting state again
if(L[3]<L[3+1]) -> L[3]=7 and L[4]=6 so if (7<6) -> True statement
temp=L[3] -> temp = 7
L[3]=L[4] -> L[3] = 6
L[4] = 7
Switched=true
Stop if
5. J++ so now j=4
While(4<=6) -> Accepting state again
if(L[4]>L[4+1]) -> L[4]=7 and L[5]=8 so if (7>8) -> False statement
Stop if
6. J++ so now j=5
While(5<=6) -> Accepting state again
if(L[5]>L[5+1]) -> L[5]=8 and L[6]=3 so if (8>3) -> True statement
temp=L[5] -> temp = 8
L[5]=L[6] -> L[5] = 3
L[6] = 8
Switched=true
Stop if
7. J++ so now j=6
4 1 5 6 7 3 8
8. While(6<=6) -> false statement
9. I++ so now i=1 and switched is false
j=0 again
While(0<=(7-1)-1) so while(0<=5) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=4 and L[1]=1 so if (4>1) -> True statement
temp=L[0] -> temp = 4
L[0]=L[1] -> L[0] = 1
L[1] = 4
Switched=true
Stop if
10. J++ so now j=1
While(1<=5) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
Nothing change on array
11. J++ so now j=2
While(2<=5) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=6 so if (5>6) -> False statement
Stop if
Nothing change on array
12. J++ so now j=3
While(3<=5) -> Accepting state again
if(L[3]>L[3+1]) -> L[3]=6 and L[4]=7 so if (6>7) -> False statement
Stop if
Nothing change on array
13. J++ so now j=4
While(4<=5) -> Accepting state again
if(L[4]>L[4+1]) -> L[4]=7 and L[5]=3 so if (7>3) -> True statement
temp=L[4] -> temp = 7
L[4]=L[5] -> L[4] = 3
L[5] = 7
Switched=true
Stop if
14. J++ so now j=5
While(5<=5) -> false statement
15. I++ so now i=2 and switched isfalse
j=0 again
While(0<=(7-1)-2) so while(0<=4) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
16. J++ so now j=1
While(1<=4) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
17. J++ so now j=2
While(2<=4) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=6 so if (5>6) -> False statement
Stop if
18. J++ so now j=3
While(3<=4) -> Accepting state again
if(L[3]>L[3+1]) -> L[3]=6 and L[4]=3 so if (6>3) -> True statement
temp=L[3] -> temp = 6
L[3]=L[4] -> L[0] = 3
L[4] = 6
Switched=true
Stop if
19. J++ so now j=4
While(4<=4) -> False statement
20. I++ so now i=3 and switched is false
j=0 again
While(0<=(7-1)-3) so while(0<=3) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
21. J++ so now j=1
While(1<=3) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
22. J++ so now j=2
While(2<=3) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=3 so if (5>3) -> True statement
temp=L[2] -> temp = 5
L[2]=L[3] -> L[2] = 3
L[3] = 5
Stop if
23. While(3<=3) -> False Statement
24. I++ so now i=4 and switched isfalse
j=0 again
While(0<=(7-1)-4) so while(0<=2) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
25. J++ so now j=1
While(1<=2) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=3 so if (4>3) -> False statement
temp=L[1] -> temp = 4
L[1]=L[2] -> L[1] = 3
L[2] = 4
Stop if
26. J++ so now j=1
27. While(2<=2) -> False Statement
28. I++ so now i=5 and switched is false
j=0 again
While(0<=(7-1)-5) so while(0<=1) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
29. J++ so now j=1
While(1<=1) -> False Statement
30. I++ so now i=6 and switched is false
j=0 again
While(0<=(7-1)-6) so while(0<=0) -> False Statement
31. I++ so now i=7 and switched is false
j=0 again
While(0<=(7-1)-7) so while(0<=-1) -> False Statement so End while
ARRAY Terakhir
1 3 4 5 6 7 8
Ada 31 steps dengan bubble sort switched.
2. Algorithm Selection sort
Declaration
idx_min:integer
i: integer
j: integer
temp: integer
1. N = 7
For(i=0 until ((7-1)=6)) do
Idx_min = 0
For(j=(0+1) until 7) do
If(L[1]<L[0]) L[1]=1 and L[0]=7 (1<7) ->True statement
Idx_min=1
If(1!=0) -> True statement
Temp=L[1] -> temp=1
L[1]=L[0] -> L[1]=7
L[0]=temp -> L[0]=1
2. Now i=1
Idx_min=1
For(j=(1+1) until 7) do
If(L[2]<L[1]) L[2]=4 and L[1]=7 (4<7) ->True statement
Idx_min=2
If(2!=1) -> True statement
Temp=L[2] -> temp=4
L[2]=L[1] -> L[2]=7
L[1]=4
3. Now i=2
Idx_min=2
For(j=(2+1) until 7) do
If(L[3]<L[2]) L[3]=5 and L[2]=7 (5<7) ->True statement
Idx_min=3
If(3!=2) -> True statement
Temp=L[3] -> temp=5
L[3]=L[2] -> L[3]=7
L[2]=5
4. Increment j
So now j=4
If(L[4]<L[3]) L[4]=6 and L[3]=7 (6<7) ->True statement
Idx_min=4
If(4!=2) -> True statement
Temp=L[4] -> temp=6
L[4]=L[3] -> L[4]=7
L[3]=6
5. Increment j
So now j=5
If(L[5]<L[4]) L[5]=8 and L[4]=7 (8<7) ->False statement
6. Increment j
So now j=6
If(L[6]<L[4]) L[6]=3 and L[4]=7 (3<7) ->True statement
Idx_min=6
If(6!=2) -> True statement
Temp=L[6] -> temp=3
L[6]=L[2] -> L[6]=5
L[2]=3
7. Now i=3
Idx_min=3
For(j=(3+1) until 7) do
If(L[4]<L[3]) L[4]=7 and L[3]=6 (7<6) -> False statement
8. increment of j
So now j=5
If(L[5]<L[3]) L[5]=8 and L[3]=6 (8<6) -> false statement
9. Increment j
So now j=6
If(L[6]<L[3]) L[6]=5 and L[3]=6 (5<6) ->True statement
Idx_min=6
If(6!=3) -> true statement
Temp=L[6] -> temp=5
L[6]=L[3] -> L[6]=6
L[3]=temp -> L[3]=5
10. Now i=4
Idx_min=4
For(j=(4+1) until 7) do
If(L[5]<L[4]) L[5]=8 and L[4]=7 (8<7) -> False statement
11. Increment j
So now j=6
If(L[6]<L[4]) L[6]=6 and L4]=7 (6<7) -> True statement
Idx_min=6
If(6!=4) -> true statement
Temp=L[6] -> temp=6
L[6]=L[4] -> L[6]=7
L[4]=temp -> L[5]=6
12. Now i=5
Idx_min=5
For(j=(5+1) until 6) do
If(L[6]<L[5]) L[6]=7 and L[5]=8 (7<8) ->True statement
Idx_min=6
If(6!=5) -> true statement
Temp=L[6] -> temp=7
L[6]=L[5] -> L[6]=8
L[5]=temp -> L[5]=7
3. Menurut saya, Selection sort lebih baik daripada bubble sort. Pada selection sort diperlukan kurang lebih 12 langkah untuk mengurutkan 7 data acak, nah sedangkan bubble sort memerlukan kurang lebih 24 langkah untuk mengurutkan data dengan jumlah yang sama, walaupun selection sort agak ribet dan membingungkan tetapi untuk performa nya selection yang terbaik.
Holla Mina ! Mimin punya tugas nih , siapa tau ada yang butuh juga kan.Nah tugas tentang array nih dengan cara Bubble sort dan Selection sort nya yang tertera dibawah ini !
Given array with element as follows
7
|
1
|
4
|
5
|
6
|
8
|
3
|
1.Write improved bubble sort steps of variables [i],[j],L[i], and [switched] of the array above!
2.Write selection sort steps of variables [i],[j],L[i], and idx_min of the array above!
3.Write your opinion : is improved bubble sort is better in terms of performance than selection sort?
Answer :
1. Algoritma Bubble Sort dengan Switched
void bubbleSort(input/output L:Larik, input n:integer)
Deklarasi
i : integer
j : integer
temp : integer
switched : boolean
Deskripsi
i <--0
while (switched = true) do
switched<-- false
j<--0
while(j<=(n-1)-i) do
if (L[j]>L[j+1]) then
temp<-- L[j]
L[j] <-- L[j+1]
L[j+1] <-- temp
switched<-- true
endif
j++
endwhile
i++
endwhile
switched<-- true
1. Step 1
7
|
1
|
4
|
5
|
6
|
8
|
3
|
i = 0 & switched = true & n=7
while (switched==true) -> Accepting state then
switched=false
j=0
while(0<=(7-1)-0) so while(0<=6) -> Accepting state
if(L[0]>L[0+1]) -> L[0]=7 and L[1]=4 so if (7>4) -> True statement
temp=L[0] -> temp =7
L[0]=L[1] -> L[0] = 4
L[1] = 7
Switched=true
Stop if
2. J++ so now j=1
4
|
7
|
1
|
5
|
6
|
8
|
3
|
if(L[1]>L[1+1]) -> L[1]=7 and L[2]=1 so if (7>1) -> True statement
temp=L[1] -> temp = 7
L[1]=L[2] -> L[1] = 1
L[2] = 7
Switched=true
Stop if
3. J++ so now j=2
4
|
1
|
7
|
5
|
6
|
8
|
3
|
if(L[2]>L[2+1]) -> L[2]=7 and L[3]=5 so if (7>5) -> True statement
temp=L[2] -> temp = 7
L[2]=L[3] -> L[2] = 5
L[3] = 7
Switched=true
Stop if
4. J++ so now j=3
4
|
1
|
5
|
7
|
6
|
8
|
3
|
While(3<=6) -> Accepting state again
if(L[3]<L[3+1]) -> L[3]=7 and L[4]=6 so if (7<6) -> True statement
temp=L[3] -> temp = 7
L[3]=L[4] -> L[3] = 6
L[4] = 7
Switched=true
Stop if
5. J++ so now j=4
4
|
1
|
5
|
6
|
7
|
8
|
3
|
While(4<=6) -> Accepting state again
if(L[4]>L[4+1]) -> L[4]=7 and L[5]=8 so if (7>8) -> False statement
Stop if
6. J++ so now j=5
4
|
1
|
5
|
6
|
7
|
8
|
3
|
While(5<=6) -> Accepting state again
if(L[5]>L[5+1]) -> L[5]=8 and L[6]=3 so if (8>3) -> True statement
temp=L[5] -> temp = 8
L[5]=L[6] -> L[5] = 3
L[6] = 8
Switched=true
Stop if
7. J++ so now j=6
4 1 5 6 7 3 8
4
|
1
|
5
|
6
|
7
|
3
|
8
|
8. While(6<=6) -> false statement
9. I++ so now i=1 and switched is false
j=0 again
While(0<=(7-1)-1) so while(0<=5) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=4 and L[1]=1 so if (4>1) -> True statement
temp=L[0] -> temp = 4
L[0]=L[1] -> L[0] = 1
L[1] = 4
Switched=true
Stop if
1
|
4
|
5
|
7
|
6
|
3
|
8
|
10. J++ so now j=1
While(1<=5) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
Nothing change on array
11. J++ so now j=2
While(2<=5) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=6 so if (5>6) -> False statement
Stop if
Nothing change on array
12. J++ so now j=3
While(3<=5) -> Accepting state again
if(L[3]>L[3+1]) -> L[3]=6 and L[4]=7 so if (6>7) -> False statement
Stop if
Nothing change on array
13. J++ so now j=4
While(4<=5) -> Accepting state again
if(L[4]>L[4+1]) -> L[4]=7 and L[5]=3 so if (7>3) -> True statement
temp=L[4] -> temp = 7
L[4]=L[5] -> L[4] = 3
L[5] = 7
Switched=true
Stop if
1
|
4
|
5
|
6
|
3
|
7
|
8
|
While(5<=5) -> false statement
15. I++ so now i=2 and switched isfalse
j=0 again
While(0<=(7-1)-2) so while(0<=4) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
16. J++ so now j=1
While(1<=4) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
17. J++ so now j=2
While(2<=4) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=6 so if (5>6) -> False statement
Stop if
18. J++ so now j=3
While(3<=4) -> Accepting state again
if(L[3]>L[3+1]) -> L[3]=6 and L[4]=3 so if (6>3) -> True statement
temp=L[3] -> temp = 6
L[3]=L[4] -> L[0] = 3
L[4] = 6
Switched=true
Stop if
1
|
4
|
5
|
3
|
6
|
7
|
8
|
19. J++ so now j=4
While(4<=4) -> False statement
20. I++ so now i=3 and switched is false
j=0 again
While(0<=(7-1)-3) so while(0<=3) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
21. J++ so now j=1
While(1<=3) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=5 so if (4>5) -> False statement
Stop if
22. J++ so now j=2
While(2<=3) -> Accepting state again
if(L[2]>L[2+1]) -> L[2]=5 and L[3]=3 so if (5>3) -> True statement
temp=L[2] -> temp = 5
L[2]=L[3] -> L[2] = 3
L[3] = 5
Stop if
1
|
4
|
3
|
5
|
6
|
7
|
8
|
23. While(3<=3) -> False Statement
24. I++ so now i=4 and switched isfalse
j=0 again
While(0<=(7-1)-4) so while(0<=2) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
25. J++ so now j=1
While(1<=2) -> Accepting state again
if(L[1]>L[1+1]) -> L[1]=4 and L[2]=3 so if (4>3) -> False statement
temp=L[1] -> temp = 4
L[1]=L[2] -> L[1] = 3
L[2] = 4
Stop if
1
|
3
|
4
|
5
|
6
|
7
|
8
|
26. J++ so now j=1
27. While(2<=2) -> False Statement
28. I++ so now i=5 and switched is false
j=0 again
While(0<=(7-1)-5) so while(0<=1) -> Accepting state again
if(L[0]>L[0+1]) -> L[0]=1 and L[1]=4 so if (1>4) -> False statement
Stop if
29. J++ so now j=1
While(1<=1) -> False Statement
30. I++ so now i=6 and switched is false
j=0 again
While(0<=(7-1)-6) so while(0<=0) -> False Statement
31. I++ so now i=7 and switched is false
j=0 again
While(0<=(7-1)-7) so while(0<=-1) -> False Statement so End while
ARRAY Terakhir
1 3 4 5 6 7 8
1
|
3
|
4
|
5
|
6
|
7
|
8
|
Ada 31 steps dengan bubble sort switched.
2. Algorithm Selection sort
Declaration
idx_min:integer
i: integer
j: integer
temp: integer
7
|
1
|
4
|
5
|
6
|
8
|
3
|
1. N = 7
For(i=0 until ((7-1)=6)) do
Idx_min = 0
For(j=(0+1) until 7) do
If(L[1]<L[0]) L[1]=1 and L[0]=7 (1<7) ->True statement
Idx_min=1
If(1!=0) -> True statement
Temp=L[1] -> temp=1
L[1]=L[0] -> L[1]=7
1
|
7
|
4
|
5
|
6
|
8
|
3
|
2. Now i=1
Idx_min=1
For(j=(1+1) until 7) do
If(L[2]<L[1]) L[2]=4 and L[1]=7 (4<7) ->True statement
Idx_min=2
If(2!=1) -> True statement
Temp=L[2] -> temp=4
L[2]=L[1] -> L[2]=7
L[1]=4
1
|
4
|
7
|
5
|
6
|
8
|
3
|
Idx_min=2
For(j=(2+1) until 7) do
If(L[3]<L[2]) L[3]=5 and L[2]=7 (5<7) ->True statement
Idx_min=3
If(3!=2) -> True statement
Temp=L[3] -> temp=5
L[3]=L[2] -> L[3]=7
L[2]=5
1
|
4
|
5
|
7
|
6
|
8
|
3
|
4. Increment j
So now j=4
If(L[4]<L[3]) L[4]=6 and L[3]=7 (6<7) ->True statement
Idx_min=4
If(4!=2) -> True statement
Temp=L[4] -> temp=6
L[4]=L[3] -> L[4]=7
L[3]=6
1
|
4
|
5
|
6
|
7
|
8
|
3
|
5. Increment j
So now j=5
If(L[5]<L[4]) L[5]=8 and L[4]=7 (8<7) ->False statement
6. Increment j
So now j=6
If(L[6]<L[4]) L[6]=3 and L[4]=7 (3<7) ->True statement
Idx_min=6
If(6!=2) -> True statement
Temp=L[6] -> temp=3
L[6]=L[2] -> L[6]=5
L[2]=3
1
|
4
|
3
|
6
|
7
|
8
|
5
|
7. Now i=3
Idx_min=3
For(j=(3+1) until 7) do
If(L[4]<L[3]) L[4]=7 and L[3]=6 (7<6) -> False statement
8. increment of j
So now j=5
If(L[5]<L[3]) L[5]=8 and L[3]=6 (8<6) -> false statement
9. Increment j
So now j=6
If(L[6]<L[3]) L[6]=5 and L[3]=6 (5<6) ->True statement
Idx_min=6
If(6!=3) -> true statement
Temp=L[6] -> temp=5
L[6]=L[3] -> L[6]=6
L[3]=temp -> L[3]=5
1
|
4
|
3
|
5
|
7
|
8
|
6
|
10. Now i=4
Idx_min=4
For(j=(4+1) until 7) do
If(L[5]<L[4]) L[5]=8 and L[4]=7 (8<7) -> False statement
11. Increment j
So now j=6
If(L[6]<L[4]) L[6]=6 and L4]=7 (6<7) -> True statement
Idx_min=6
If(6!=4) -> true statement
Temp=L[6] -> temp=6
L[6]=L[4] -> L[6]=7
L[4]=temp -> L[5]=6
1
|
4
|
3
|
5
|
6
|
8
|
7
|
12. Now i=5
Idx_min=5
For(j=(5+1) until 6) do
If(L[6]<L[5]) L[6]=7 and L[5]=8 (7<8) ->True statement
Idx_min=6
If(6!=5) -> true statement
Temp=L[6] -> temp=7
L[6]=L[5] -> L[6]=8
L[5]=temp -> L[5]=7
1
|
4
|
3
|
5
|
6
|
7
|
8
|
3. Menurut saya, Selection sort lebih baik daripada bubble sort. Pada selection sort diperlukan kurang lebih 12 langkah untuk mengurutkan 7 data acak, nah sedangkan bubble sort memerlukan kurang lebih 24 langkah untuk mengurutkan data dengan jumlah yang sama, walaupun selection sort agak ribet dan membingungkan tetapi untuk performa nya selection yang terbaik.
Sekian yang dapat saya sampaikan tentang Contoh Soal Array Algoritma.Jika ada pertanyaan sampaikan dikolom komentar.Semoga dapat membantu dan bermanfaat.
2 comments
Click here for commentsini dia yang saya cari gan,mantap !
Replyok gan mantap
ReplyConversionConversion EmoticonEmoticon