0% found this document useful (0 votes)
41 views2 pages

Contoh 1.1 Source Code Java Selection Sort: Public Class Int Public

The document contains code for selection sort algorithms in Java. The first section of code defines a selectionSort class with an array of integers, methods to display the array and sort it using selection sort, and a main method to run the class. The sorting method iterates through the array, finds the index of the minimum element, and swaps it with the element at the current iteration if they are out of order. The second section of code also implements selection sort, with a main method that declares an integer array, nested for loops to find the minimum index and swap elements, and output statements to display the sorted array.

Uploaded by

naufal29
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)
41 views2 pages

Contoh 1.1 Source Code Java Selection Sort: Public Class Int Public

The document contains code for selection sort algorithms in Java. The first section of code defines a selectionSort class with an array of integers, methods to display the array and sort it using selection sort, and a main method to run the class. The sorting method iterates through the array, finds the index of the minimum element, and swaps it with the element at the current iteration if they are out of order. The second section of code also implements selection sort, with a main method that declares an integer array, nested for loops to find the minimum index and swap elements, and output statements to display the sorted array.

Uploaded by

naufal29
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/ 2

F -X C h a n ge F -X C h a n ge

PD PD

!
W

W
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
CONTOH 1.1 SOURCE CODE JAVA { SELECTION SORT}
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

public class selectionSort {


int [] angka= {5, 1, 12, -5, 16, 2, 12, 14};
public selectionSort()

tampilkanAngka ();
urutkanAngka ();
tampilkanAngka ();

void tampilkanAngka () {

System.out.println("\n--------------------------------");
for (int i=0;i<angka.length;i++)
{
System.out.print(angka[i]+" ");
}

void urutkanAngka() {

int tampung;

for (int i=0 ;i<angka.length-1; i++)


{
int minindek=i;
for(int j=i+1; j<angka.length; j++)
{
if(angka[j]<angka[minindek])
minindek=j;

if(minindek!=i){

tampung= angka[i];
angka[i]= angka[minindek];
angka[minindek]= tampung;
}

} //tampilkanAngka();

public static void main(String[] aksi)

{
selectionSort urut = new selectionSort();

}
F -X C h a n ge F -X C h a n ge
PD PD

!
W

W
O

O
N

N
y

y
bu

bu
to

to
ww

ww
om

om
k

k
lic

lic
CONTOH 1.2 SOURCE CODE JAVA { SELECTION SORT}
C

C
.c

.c
w

w
tr re tr re
.

.
ac ac
k e r- s o ft w a k e r- s o ft w a

Public class SelectionShort {

Public class static void main ( string [] args ) {

Int pos, temp;

Int n = 0;

Int nilai [] = { 44, 55, 12, 42, 94, 18, 06, 67};

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

Pos=i;

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

If ( nilai [j] <nilai [pos] ){

Pos=j;

If ( pos !=1 ) {

Temp = nilai [pos];

Nilai [pos] = nilai [i];

Nilai [i] = temp;

System.out.print(“hasil= “);

For ( int i=0; i<n; i++ )

System.out.print(“ “ + nilai [i]);

System.out.println(“ “);

You might also like