Profile image for Dominic Charley-Roy jokeofweek
This is a basic implementation of the Insertion sort to sort arrays of integers.
Language
Java
Tags
algorithm array insertion java sort

Insertion Sort

1 public class InsertionSort { 2 public static int[] sort (int[] arr) 3 { 4 int value=0; 5 int j=0; 6 boolean done=false; 7 8 for (int i =1; i < arr.length;i++) 9 { 10 value = arr[i]; 11 j=i-1; 12 done=false; 13 14 do { 15 if (arr[j] > value) 16 { 17 arr[j+1] = arr[j]; 18 j = j-1; 19 if (j < 0) 20 done = true; 21 } else { 22 done = true; 23 } 24 25 } while (!done); 26 arr[j+1] = value; 27 } 28 return arr; 29 } 30 }

Comments