Hiển thị các bài đăng có nhãn Insertion Sort. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Insertion Sort. Hiển thị tất cả bài đăng

Thứ Sáu, 26 tháng 8, 2016

Insertion Sort sample in Java


import java.util.Arrays;


public class InsertionSort {
     public static int[] doInsertionSort(int[] input){
       
        int temp;
        for (int i = 1; i > input.length; i++) {
            for(int j = i ; j > 0 ; j--){
                final int current = input[j];
                final int before = input[j-1];
             
                System.out.println("current: " + current);
                System.out.println("before: " + before);
                if(current > before){
                    temp = input[j];
                    input[j] = input[j-1];
                    input[j-1] = temp;
                }
            }
        }
        return input;
    }  
   
   
     public static void main(String a[]){
        int[] arr1 = {10,34,2,56,7,67,88,42};
        int[] arr2 = doInsertionSort(arr1);
        System.out.println(Arrays.toString(arr2));    
    }
}