-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.java
More file actions
72 lines (57 loc) · 2.05 KB
/
Sorting.java
File metadata and controls
72 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.Scanner;
public class Sorting {
public static int[] selection(int[] data,int n){
System.out.println();
System.out.print("Data Sebelum di Sorting : ");
for(int x = 0; x < n; x++)
System.out.print(data[x]+" ");
System.out.println("\n\nProses Selection Sort");
for(int x = 0; x < n-1; x++){
System.out.println("Iterasi ke-"+(x+1)+" : ");
for(int y = 0; y < n; y++)
System.out.print(data[y]+" ");
System.out.println(" Apakah Data "+data[x]+" sudah benar pada urutannya?");
boolean tukar = false;
int index = 0;
int min = data[x];
String pesan = " Tidak Ada Pertukaran";
for(int y = x+1; y < n; y++){
if(min > data[y]){
tukar = true;
index = y;
min = data[y];
}
}
if(tukar == true){
pesan = " Data "+data[x]+" ditukar dengan Data "+data[index];
int temp = data[x];
data[x] = data[index];
data[index] = temp;
}
for(int y = 0; y < n; y++)
System.out.print(data[y]+" ");
System.out.println(pesan+"\n");
}
System.out.print("Data Setelah di sorting : ");
for(int x = 0; x < n; x++)
System.out.print(data[x]+" ");
return null;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int n;
System.out.println("");
System.out.print("Masukkan jumlah Data : ");
n = scan.nextInt();
int[] data = new int[n];
System.out.println();
for(int x = 0; x < n; x++)
{
System.out.print("Input nilai Data ke-"+(x+1)+" : ");
data[x] = scan.nextInt();
}
System.out.println("");
System.out.println("Selection Sort");
data = selection(data,n);
}
}