You are on page 1of 2

NAMA : VIONNA WEDHARINY

NIM : F1B014104

Tugas membuat selectionsort menggunakan Java


/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package selectionsort;

import java.util.Scanner;

public class Selectionsort {


Scanner scan = new Scanner(System.in);
int arr[];
int aray [];
int masuk, inputan;
void inputan()
{
System.out.print("Masukkan jumlah : ");
masuk = scan.nextInt();
aray=new int[masuk];
for(int i=0; i<masuk; i++)
{
System.out.print("masukkan nilai ke-"+i+" : ");
aray[i] = scan.nextInt();
}

}
void selectionsort()
{
for (int i=0; i<aray.length; i++)
{
int min = aray[i];
int post = i;
for(int j=i; j<aray.length; j++)
{
if(aray[j]<min)
{
min = aray[j];
post = j;
}
}
if(i!=post)
{
int temp = aray[i];
aray[i] = aray[post];
aray[post]=temp;
}
}
}
void display()
{
System.out.println("Nilai aray sebelum di urutkan : ");
for(int i=0; i<aray.length; i++)
{
System.out.println("aray ke-"+i+" : "+aray[i]);
}
}
void display2()
{
System.out.println("Nilai aray setelah di urutkan : ");
for(int i=0; i<aray.length; i++)
{
System.out.println("aray ke-"+i+" : "+aray[i]);
}
}
public static void main(String[] args) {
Selectionsort a = new Selectionsort();
a.inputan();
a.display();
a.selectionsort();
a.display2();

You might also like