Java Insert Sort

Source: Internet
Author: User
插入排序的思想就和玩扑克是的摸牌一样,摸到一张牌放手上,再摸一 张和之前的比较,大的就放后面,小的就放前面。 一个数列我们把它分为两个区,一个是已经排序的区,一个是乱序区,选取第 一个元素出来作为排序区的元素,然后从第二个元素开始往后作为乱序区,从 第二个元素开始(并把这个元素复制出来叫做下标元素),分别和排序区的元素比较大小,如果这个元素比排序区的元 素小,则把排序区的元素依次往后一位,然后把下标元素复制进空出来的位 置,这样就完成一次排序,直到排序完成。 下面看代码: int[] a = new int[10]{2,443,34,231,34,34,4,5,43,4}; for(int i = 1;i<a.length;i++){ int index = a[i]; for(int j = i-1;j>=0;j--){ if(index<a[j]&&i==1||index<a[j]&&index>a[j-1]){ a[j+1]=a[j]; a[j] = index; } else if(index>=a[j]){ break; } }

Java 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.