Insertion sequencing of algorithms and data structures

Source: Internet
Author: User

Tag:pre   thread    Array    reading   gen     thought    name   log    time    

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace Insert Sort {class Program {static void Main (string[] args) {int[] Arry = {10,3,4,6,7            , 9,8,2,1,0};            Insertsortpro (Arry, 10);            for (int i=0;i<10;i++) {Console.WriteLine (arry[i]);        } console.readkey (); }///<summary>///Insert Sort//thought: The second element is compared to the first element, and if it is less than the first element, the position is placed at the first element.//The third element and the first two elements are compared, less than the previous The two elements are placed in the corresponding position///etc..//</summary>//<param name= "Arry" ></param>//            <param name= "Length" ></param> public static void Insertsort (int[] arry,int length) {                    for (int i =1;i<length;i++)//First element does not need to compare {for (int j=i;j>0;j--) { if (Arry[j-1]>arry[j]) {Swap (Arry, J-1, J);                    } else {break;        }}}///<summary>///Insert Sort Introduction///</summary> <param name= "Arry" ></param>///<param name= "Length" ></param> public static VO                ID InsertSort2 (int[] arry, int length) {for (int i = 1; i < length; i++)//The first element does not require a comparison { for (int j = i; j > 0 && arry[j-1] > Arry[j]; j--) {Swap (a                Rry, J-1, J);        }}}///<summary>/////Optimized Insert sort///In the previous insert sort, the inner loop is to be exchanged for elements, which is an important reason for the time to insert sort consumption  Below we optimize the insertion sort, "Only compare not Exchange"//</summary>//<param name= "Arry" ></param>//    <param name= "Length" ></param> public static void Insertsortpro (int[] arry,int length)    {for (int i=1;i<length;i++) {int e = arry[i];//Get the current element, see if this element is the smallest number in the first I element                int J;//position of current element for (j=i;j>0&&arry[j-1]>e;j--)//determine e This element is not the smallest number in the first I element            {arry[j]= arry[j-1];//A previous element to move backward one} Arry[j] = e;            }} private static void Swap (int[] arr, int i, int j) {int t = Arr[i];            Arr[i] = Arr[j];        ARR[J] = t; }    }}

"Normal insert Sort" is slower than selecting sort, but optimized insert ordering is faster than selecting sort. Insert sort has a feature when the array is more orderly, the sort will be faster. Sometimes it's faster than some advanced sort.

Also the time complexity of inserting sort is also O (n^2)

Insertion sequencing of algorithms and data structures

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.