Sorting requirements: array A of length n is sorted in order from small to large.
Bubble Sort Idea : given a length of n array A, loop n-1 times, each cycle takes out a[i] and a[0] to a[i-1] The number between the comparison, from right to left sequentially, if a[i] small will continue to compare to the left, while the a[j] move to the right
One, if a[i] large then assign value a[i] to A[j]
Original array: 2, 0, 3, 6, 8, 4, 9, 5, 1, 7,
2nd round results: 0, 2, 3, 6, 8, 4, 9, 5, 1, 7,
3rd round results: 0, 2, 3, 6, 8, 4, 9, 5, 1, 7,
4th round results: 0, 2, 3, 6, 8, 4, 9, 5, 1, 7,
5th round results: 0, 2, 3, 6, 8, 4, 9, 5, 1, 7,
6th round results: 0, 2, 3, 4, 6, 8, 9, 5, 1, 7,
7th round results: 0, 2, 3, 4, 6, 8, 9, 5, 1, 7,
8th Round results: 0, 2, 3, 4, 5, 6, 8, 9, 1, 7,
9th round Results: 0, 1, 2, 3, 4, 5, 6, 8, 9, 7,
10th round Results: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Public voidInertsort (int[] Array) {PrintArray ("Original array:", array); intTemp,num; for(inti=1;i<array.length;i++) {num=Array[i]; for(intj=i-1;j>=0;j--){ if(num>Array[j]) {Array[j+1]=num; Break; }Else{array[j+1]=Array[j]; ARRAY[J]=num; }} printArray ("First" + (i + 1) + "sub-cycle sort result:", array); } }
Sort-insert Sort