Insert Sort
package tp.zgz.chapter04;
public class InsertSort {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0; iarr[i] = (int) (100 + Math.random() * (100 + 1));
}
System.out.printf("Array before sorting:");
for (int i = 0; i <10; i++) {
System.out.printf(arr[i] + "");
}
System.out.println();
System.out.printf("sorted array:" );
SelectedSort.Sort(arr);
}
static void Sort(int arr[]) {
//Traverse the data
for ( int i = 1; i
int temp = arr[i];
int j = i-1; //j is the position to be inserted< br /> while (j >= 0 && temparr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
print(arr);
}
static void print(int arr[]) {
for (int i = 0; iSystem.out.printf(" %d ", arr[i]);
}
}
}