Sort-insert Sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.