Third week Ljy about insert sort

Source: Internet
Author: User

After learning to bubble sort and choose sort by video, go online to find out if there is no other sort method to understand and learn to insert sort. Write a bit of your own opinion here. In my opinion, inserting a sort is like queued a station by size.

A class all boys did not queued time is the size of mixed, disorderly, the teacher shouted from low to high station, first out of a student standing in the team, when the team on his own he does not need more than the stature, he is a number, and then the second person,

Is the second number came in, than he stood in front of him taller than he stood behind him, the third person came in, first with the shortest ratio, higher than he and the second, has been met taller than him, standing higher than his front, like a row of data, everyone sort, and this is a

Kind of, come in to find their own position, each data comes in the time is compared with the already ranked data, he can from the beginning of the big than, has been compared to his smaller, he ranked smaller than his number behind, but also from the beginning of the small than he has been compared to the greater than he has,

He came in front of a number bigger than him, so that the queued array is always in an orderly state, and each number is inserted into an ordered array when inserting, and I think the efficiency in some cases is higher than the bubbling sort, after understanding the principle of insertion sorting,

I decided to try to implement the code myself first:

Package com.zkc.d1; Public classExample { Public Static voidMain (string[] args) {int[] Arr ={1,5,7, -, A, -, -, -, the, +};//define an array first                 for(inti =0; i < arr.length; i++) {//gets the index of each data in the array                     for(intj = i; J >0; j--) {//Insert the exploited, from the largest beginning to the smallest, at this time he index is 1, for the second small;                                                if(Arr[j] < arr[j-1]) {//if it's smaller than the front of him. Just change the position and stick it in front of him.Exchange (arr,j,j-1); }                                                                        Else   Break; }                }                                 for(inti =0; i < arr.length; i++) {System. out. println (Arr[i]); }                        }        //method of changing position         Public Static voidExchangeint[] arr,intIintj) {inttemp =Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }}

After implementation, began to think about its bubble sort and choose the difference between sorting, the following is my practice time to knock the bubble sort of code;

 Public classSort { Public Static voidMain (string[] args) {/*array elements: {24, 69, 80, 57, 13} Please sort the arrays of elements. Bubble sort Adjacent Element 22 comparison, large back, first completed, maximum value appears at the maximum index*/                   int[] arr = { -, the, the, $, -}; intMax =0;  for(inti =0; I < arr.length-1; i++) {                     for(intj =0; J < Arr.length-1-I.; J + +) {                        if(Arr[j] > arr[j +1]) {Exchange (arr, J, J+1); }                                            }                                }                    for(inti =0; i < arr.length; i++) {System. out. println (Arr[i]); }                    }

Feel the insertion sort efficiency will be faster, bubble sort and select sort more like the Daleitai, bubble sort, five people standing there, old five and old four dozen. Compared to an old four, then old four and old three dozen,. than a old three,.... At the end of the show, boss.

And then play it over again, when the second is not used on the first tournament data, this can be used in a group of data can well reflect their gap, 1,3,2,4 these four numbers according to the insertion sort, first elected 1, the next with 3 and 1 to go than

, the order of 1,3 here is 1 times, and then 2 to compare with the third one and 1 compared to the first time, the number of comparisons to 3, when the order is three-way, then 4 to go compared to the time only need and 3 than once, because the front is less than 3,

is ordered, and this information is obtained at the time of comparison, using this information, 4 no longer compared with, the total number of comparisons is 4 times, and the bubble sort or select the number of 3+2+1 = 6 times, their comparison is fixed, the more orderly the data

Their disadvantage is greater, if a set of data is ordered for 1,2,3,4,5.....,n; Insert sort the data that is inserted each time only needs and sorts the data to be the largest than once, all only need to compare n-1 times, and the selection and bubble sort still need to compare

(n-1) + (n-2) + .... +2 +1 = (n-1)/2 * n; significantly more complex.

For the time being to understand so much, there may be errors in some places, please advise.

Third week Ljy about 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.