Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * InsertionSort Program
- * Program to implement InsertionSort
- *
- * @author Cahyadi Surya Nugraha
- * @version 1.0
- * @since March 31th 2021
- */
- public class InsertionSortApp
- {
- static void insertionSort(int[] arr)
- {
- int size_arr = arr.length;
- for (int i = 1; i < size_arr; i++)
- {
- int key = arr[i];
- int j = i-1;
- for (; j > -1 && arr[j] > key; j--)
- arr[j+1] = arr[j];
- arr[j+1] = key;
- }
- }
- static void printArray(int[] arr)
- {
- for (int value : arr)
- {
- System.out.print(value + " ");
- }
- }
- public static void main(String[] args)
- {
- int test_array[] = {54,122,5,4,2,3,45,12,52};
- System.out.println("List dalam array sebelum di InsertionSort:");
- printArray(test_array);
- insertionSort(test_array);
- System.out.println("\n\nList dalam array setelah di InsertionSort:");
- printArray(test_array);
- }
- }
Add Comment
Please, Sign In to add comment