Senin, 24 September 2012

InsertsionSort

Ada program baru nih , cara menyusun bilangan secara acak yang hasilnya bakalan nyusun lagi heheehe

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package insertionsort;

import java.util.Scanner;

/**
 *
 * @author asus
 */


    /**
     * @param args the command line arguments
     */
public class insertsionSort {
  public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
       
        int arr[] = {};

        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;
    }

}


barangkali ada yang butuh....

Tidak ada komentar:

Posting Komentar