ada lagi cara mengurutkan bilangan secara acak tapi berdasarkan angka yang paling besar langsung aja kali ya
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package insertionsort;
/**
*
* @author asus
*/
public class InsertionDescending {
public static void main(String[] args) {
int arr[] = {2,3,4,5,1,6};
insertionSort(arr, arr.length);
for(int r = 0; r < arr.length; r++)
{
System.out.print(arr[r] + " ");
}
}
static int[] insertionSort(int array[], int n)
{
for (int r = 1; r<n; r++)
{
int j = r;
int b = array[r];
while ((j > 0) && (array[j-1]< b ))
{
array[j] = array[j-1];
j--;
}
array[j]= b;
}
return array;
}
}
Tidak ada komentar:
Posting Komentar