Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Insertion_Sort here.
- *
- * @Samuel Berkat Hulu
- * @version 5.0
- */
- public class Insertion_Sort
- {
- public static void insertionSort(int arr[]) {
- for (int j = 1; j < arr.length; j++) {
- int key = arr[j]; int i = j-1;
- while ( (i > -1) && ( arr[i] > key ) ) {
- arr[i+1] = arr[i]; i--;
- }
- arr[i+1] = key;
- }
- }
- static void printArray(int arr[]) {
- int len = arr.length;
- for (int i= 0; i<len; i++)
- System.out.print(arr[i] + " " );
- System.out.println();
- }
- public static void main(String args[]){
- int[] arr1 = {21,18,15,23,52,12,61};
- System.out.println("Array sebelum penyisipan:");
- printArray(arr1);
- insertionSort(arr1);
- System.out.println("Array array setelah penyisipan:");
- printArray(arr1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement