0% found this document useful (0 votes)
20 views

Selection Sort Error

The document describes a selection sort algorithm. It defines a Selection_sort class with sort and tampilArray methods. The sort method iterates through the array, finds the minimum element, and swaps it with the current element. The tampilArray method prints the array. The main method instantiates the class, passes an integer array to be sorted, prints it before and after calling the sort method.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Selection Sort Error

The document describes a selection sort algorithm. It defines a Selection_sort class with sort and tampilArray methods. The sort method iterates through the array, finds the minimum element, and swaps it with the current element. The tampilArray method prints the array. The main method instantiates the class, passes an integer array to be sorted, prints it before and after calling the sort method.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

/*

* 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 selection_sort;

/**
*
* @author User
*/
public class Selection_sort {
void sort(int arr[])
{
int n = arr.length;
for(int i=0; i<n-1; i++)
{
int min_idx = 1;
for(int j=i+1; j<n; j++)

if(arr[i]<arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;

}
}
void tampilArray(int arr[])
{
int n = arr.length;
for(int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Selection_sort ob = new Selection_sort();//Instansiasi Objek
int arr[] = {40,34,35,65,12,45,50,32,10,12,01};//Data array yang mau
diurutkan
System.out.println("Data sebelum diurutkan");
ob.tampilArray(arr);//Panggil metod cetak array
System.out.println("Data setelah diurutkan");
ob.sort(arr);//panggil metod sort
ob.tampilArray(arr);
}

You might also like