Summary of the introduction of algorithms

Source: Internet
Author: User

1. Insert Sort

namespaceInsert Sort {//the insertion sort is much faster when it is probably ordered, such as: 3,1,7,4,5,9,10,15,12//This, you will find that it is probably already ascending, when inserting fast,//the speed is almost the same as the other sort.    classProgram {Static voidMain (string[] args) {            int[] arr = { -, A, About, +,12125, About, -,212, $,787, +, A, About, $};//Array to sortInsertsort (arr);//Call the Insert Sort function            foreach(intItemincharr)        Console.WriteLine (item); }        Private Static voidInsertsort (int[] arr) {            //The insertion sort is to insert a sequence of numbers into an ordered number//The default subscript is 0 This number is already ordered             for(inti =1; I < arr. Length; i++)            {                intInsertval = Arr[i];//First, remember the number that you want to insert.                intInsertindex = i-1;//Find out the subscript of the previous number (wait for the number of inserts to be compared to this number)//If this condition is met, it is stated that we have not found the proper location                 while(Insertindex >=0&& Insertval < Arr[insertindex])//here Small Yu Shi order, big so descending{Arr[insertindex+1] = Arr[insertindex];//and move the number larger than the number of inserts backwardsinsertindex--;//The pointer continues to move backwards, and the number of inserts is compared to the number pointed to by the pointer.                }                //Insert (this time to insertval find the appropriate location)Arr[insertindex +1] =Insertval; }        }    }}

Summary of the introduction of algorithms

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