0% found this document useful (0 votes)
60 views8 pages

Coding: Aplikasi Sederhana Mengurutkan Bilangan

The document describes code for three sorting algorithms: bubble sort, insertion sort, and selection sort. It prompts the user to select an algorithm, then takes user input of numbers to sort and applies the selected algorithm to sort the numbers in ascending and descending order. The code samples show how each algorithm is implemented to sort an array of integers in Java.

Uploaded by

Derian Wijaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views8 pages

Coding: Aplikasi Sederhana Mengurutkan Bilangan

The document describes code for three sorting algorithms: bubble sort, insertion sort, and selection sort. It prompts the user to select an algorithm, then takes user input of numbers to sort and applies the selected algorithm to sort the numbers in ascending and descending order. The code samples show how each algorithm is implemented to sort an array of integers in Java.

Uploaded by

Derian Wijaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Aplikasi Sederhana mengurutkan bilangan

Coding

1. import java.util.Scanner;
2.
3. public class test3 {
4.
5. public test3() {
6. // TODO Auto-generated constructor stub
7. }
8.
9. public static void main(String[] args) {
10. // TODO Auto-generated method stub
11. Scanner sc = new Scanner(System.in);
12.
13. int x;
14. int jumlah,a,b,c;
15.
16.
17. do {
18. System.out.println("================");
19. System.out.println("Mengurutkan Bilangan");
20. System.out.println("================");
21. System.out.println("1. Bubble Sort");
22. System.out.println("2. Insertion Sort");
23. System.out.println("3. Selection Sort");
24. System.out.print(">>>>>");
25. x=sc.nextInt();
26. }while(x<1 || x>3);
27.
28. if(x == 1) {
29. System.out.print("Masukkan Jumlah Bilangan Yang Mau Di Input :");
30. jumlah = sc.nextInt();
31.
32. int array[] = new int[jumlah];
33.
34. System.out.println("\nMasukkan " + jumlah+" Buah Bilangan");
35. System.out.println("===========================================");
36. for(a=0; a<jumlah;a++)
37. {
38. System.out.print("Bilangan Ke- " + (a+1)+" =");
39. array[a]=sc.nextInt();
40. }
41.
42. System.out.println("\nBilangan Sebelum Terurutnya");
43. for(int z=0;z<jumlah;z++)
44. {
45. System.out.print(array[z] +" ");
46. }
47.
48. for(a=0; a<(jumlah-1);a++)
49. {
50. for(b=0;b<jumlah-a-1;b++)
51. {
52. if (array[b] > array[b+1])
53. {
54. c = array[b];
55. array[b] = array[b+1];
56. array[b+1]=c;
57. }
58. }
59. }
60.
61. System.out.println("\n\nBilangan Terurutnya (Ascending) adalah :");
62. for(a=0;a<jumlah; a++)
63. System.out.print(array[a] +" ");
64.
65. for(a=0; a<(jumlah-1);a++)
66. {
67. for(b=0;b<jumlah-a-1;b++)
68. {
69. if (array[b] < array[b+1])
70. {
71. c = array[b];
72. array[b] = array[b+1];
73. array[b+1]=c;
74. }
75. }
76. }
77. System.out.println("\n\nBilangan Terurutnya (Descending) adalah :");
78. for(a=0;a<jumlah; a++)
79. System.out.print(array[a] +" ");
80.
81.
82. }
83. else if(x == 2) {
84. System.out.print("Masukkan Jumlah Bilangan Yang Mau Di Input: ");
85. int n=sc.nextInt();
86.
87. int[]ab=new int[n];
88. int gg,ww,temp=0;
89.
90. System.out.print("Masukan "+ n +"Buah Bilangan : ");
91. for(gg=0;gg<n;gg++){
92. ab[gg]=sc.nextInt();
93.
94. }
95. System.out.print("\nBilangannya Adalah: ");
96. for(gg=0;gg<n;gg++){
97. System.out.print(" "+ab[gg]);
98. }
99.
100. for(gg=0;gg<n;gg++){
101. for(ww=gg+1;ww<n;ww++){
102. if(ab[gg]>ab[ww]){
103. temp=ab[gg];
104. ab[gg]=ab[ww];
105. ab[ww]=temp;
106. }
107. }
108. }
109. System.out.print("\nBilangan Terurutnya (Ascending) adalah: ");
110. for(gg=0;gg<n;gg++){
111. System.out.print(" "+ab[gg]);
112. }
113. for(gg=0;gg<n;gg++){
114. for(ww=gg+1;ww<n;ww++){
115. if(ab[gg]<ab[ww]){
116. temp=ab[gg];
117. ab[gg]=ab[ww];
118. ab[ww]=temp;
119. }
120. }
121. }
122. System.out.print("\nBilangan Terurutnya (Descending) adalah: ");
123. for(gg=0;gg<n;gg++){
124. System.out.print(" "+ab[gg]);
125. }
126.
127. }
128. else if (x==3) {
129. int ukuran, aa, bb, cc;
130. int arr[] = new int[20];
131.
132. System.out.print("Masukkan Jumlah Bilangan Yang Mau DiInput : ");

133. ukuran = sc.nextInt();


134.
135.
136. for(aa=0; aa<ukuran; aa++)
137. {
138.
139. System.out.print("Bilangan Ke-" + (aa+1)+" = ");
140. arr[aa]=sc.nextInt();
141. }
142.
143.
144. for(aa=0; aa<ukuran; aa++)
145. {
146. for(bb=aa+1; bb<ukuran; bb++)
147. {
148. if(arr[bb] > arr[bb])
149. {
150. cc = arr[bb];
151. arr[bb] = arr[bb];
152. arr[bb] = cc;
153. }
154. }
155. }
156. System.out.println("\n==========================================");

157. System.out.print("Angka yang telah di urutkan (Ascending) :");


158. for(aa=0; aa<ukuran; aa++)
159. {
160. System.out.print(arr[aa]+ " ");
161. }
162. for(aa=0; aa<ukuran; aa++)
163.
164. {
165. for(bb=aa+1; bb<ukuran; bb++)
166. {
167. if(arr[aa] < arr[bb])
168. {
169. cc = arr[aa];
170. arr[aa] = arr[bb];
171. arr[bb] = cc;
172. }
173. }
174. }
175. System.out.println(" \n");
176. System.out.print("Angka yang telah di urutkan (Descending) :");
177. for(aa=0; aa<ukuran; aa++)
178. {
179. System.out.print(arr[aa]+ " ");
180. }
181.
182.
183. }
184. }
185.
186. }
Flowchart
Screenshoot saat running program

You might also like