This article illustrates the method of Java implementation insertion sequencing. Share to everyone for your reference. The implementation methods are as follows:
Import Java.util.Arrays;
/**
* Algorithm name: Insert Sort
* Best efficiency O (n); the worst Efficiency O (n²) is the same as bubbling and selecting, suitable for sorting small lists
* If the list is basically ordered, the insertion order is more efficient than bubbling and choosing.
* @author l.eric
* * *
/public class Insertionsorting {public
static void Main (string[] args) {
//Definition of an integer array
int[] nums = new int[]{4,3,-1,9,2,1,8,0,6};
Prints an array System.out.println without sorting
("No Results before sorting:" + arrays.tostring (nums));
for (int index=0; index<nums.length; index++) {
//Get the numeric
int key = Nums[index] that needs to be inserted;
Gets the subscript
int position = index;
Iterate over the sorted data before finding the right place to insert while
(position >0 && nums[position-1] > key) {
Nums[position] = nums[ POSITION-1];
position--;
}
Nums[position] = key;
}
Print sorted results
System.out.println ("Sorted results:" + arrays.tostring (nums));
}
I hope this article will help you with your Java programming.