Senin, 24 September 2012

Selection

ada lagi cara mengurutkan bilangan secara acak dengan cara seleksi , kalo ada yang mau coba silahkan =))

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

package Selectionsort;

/**
 *
 * @author asus
 */
public class Selection {
     public static void main(String[] args) {
       int arr[] = {2,3,4,5,1,6};
       Selection baca = new Selection();
       baca.SelectionSort(arr);
        for (int a = 0; a < arr.length; a++)
        {
            System.out.print(arr[a] + "" );
        }


    }
     int [] SelectionSort(int[] arr ){
         for(int i=0; i<arr.length; i++)
         {
             for (int j=i+1; j<arr.length; j++)
             {
                 if(arr[i] > arr[j])
                 {
                     int temp = arr[j];
                     arr[j] = arr [i];
                     arr[i] = temp;
                 }
             }
         }
         return arr;

     }

}

sekian dan terimaksih

Tidak ada komentar:

Posting Komentar