Insertion Sort
import java.util.Arrays; public class InsertionSort{ public static int[] insertionsort(int[] a){ for(int i=1;i<=a.length-1;i++){ int val=a[i]; int j=i; while(j>0 && val<a[j-1]){ a[j] = a[j-1]; j--; } a[j] = val; } return a; } public static void main(String[] args){ int[] input = {2,2,435,33,3,3,3,56,6,6,7,8,3,6,9,8,6}; System.out.println("Before: "+Arrays.toString(input)); int[] output = insertionsort(input); System.out.println("After: "+Arrays.toString(output)); } } /* Before: [2, 2, 435, 33, 3, 3, 3, 56, 6, 6, 7, 8, 3, 6, 9, 8, 6] After: [2, 2, 3, 3, 3, 3, 6, 6, 6, 6, 7, 8, 8, 9, 33, 56, 435] */
0 comments:
Post a Comment