Sort (1)--bubbling, inserting, selecting

Source: Internet
Author: User

1. Bubble sort

(1) Principle: Multi-pass comparison, each pair of adjacent data through the constant judgment and exchange of the largest data placed in the last unsorted sequence, like a large bubble constantly upward floating

(2) Complexity of Time:

(3) Algorithm Stability: stable

(4) Code implementation:

 Public void sort (int[] array)    {        int size=array.length;          for (int i=1;i<size;i++)        {            for (int j=0;j<size-i;j++)            {                if(Array[j] >array[j+1])                {                    swap (array,j,j+1);     }}}

2. Insert Sort

(1) Principle: Each step of a record to be sorted, according to the size of its key code value inserted in the previously sorted file in the appropriate position, until all is inserted.

(2) Complexity of Time:

(3) Algorithm Stability: stable

(4) Code implementation:

 Public voidSortint[] Array) {        intSize=Array.Length;  for(inti=1;i<size;i++)        {            inttemp=Array[i]; intJ=i-1;  while(j>=0&&temp<Array[j]) {Array[j+1]=Array[j]; J--; } array[j+1]=temp; }    }     Public voidBinarysort (int[] array)//binary Insert {intSize=Array.Length;  for(inti=1;i<size;i++)        {            inttemp=Array[i]; intLow=0; intHigh=i-1;  while(low<=High ) {                intMid= (Low+high)/2; if(temp<Array[mid]) { High=mid-1; }                Else{ Low=mid+1; }            }             for(intj=i-1;j>=low;j--) {array[j+1]=Array[j]; } Array[low]=temp; }    }

3. Select sort

(1) Principle: each time from the data to be sorted to select the smallest element, stored in the starting position of the unordered sequence, until all the data elements to be sorted out

(2) Complexity of Time:

(3) Algorithm Stability: unstable

(4) Code implementation:

 Public voidSortint[] Array) {        intSize=Array.Length;  for(inti=0;i<size;i++)        {            intmin=i;  for(intj=i+1;j<size;j++)            {                if(array[min]>Array[j]) {min=J; }            }            if(min!=i) {swap (array,min,i); }        }    }

Sort (1)--bubbling, inserting, selecting

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.