1.1. Algorithmic thinking
Direct insertion sorting is a simple interpolation method, and the basic idea is to insert the records to be sorted into an ordered sequential sequence by the size of their key values, until all the records have been inserted so that a new ordered sequence is obtained.
algorithm ideas for inserting sorting:
(1) Set the monitoring whistle r[0], assign the value of the record to be inserted to r[0];
(2) Set the position of the start search J;
(3) in the array to search, the search will be the first J record, until R[0].key≥r[j].key;
(4) Place r[0] into the r[j+1] position.
1.2. Java implementation
|
bubblesorttest |
|
 
package ch03; public class Bubblesorttest {public static void main (string[] args) {int[] Srcarr = {2,5,7,1,9,34,22, 15}; int[] Bubblesort = Bubblesort (Srcarr); for (int i:bubblesort) {System.out.println (i); }}/** * method Description: Bubble Sorting algorithm * * @param srcarr * @return * */public static Int[] Bubblesort (int[] srcarr) {for (int i = 0, i < srcarr.length; i++) {for (int j = 0; J < SR Carr.length-i-1;j++) {if (srcarr[j]>srcarr[j+1]) {int temp = srcarr[j]; SRCARR[J] = srcarr[j+1]; SRCARR[J+1] = temp; }}} return Srcarr; }}
|
Java Implementation Insert Sort