C # Insert Sort

Source: Internet
Author: User
C #, inserting sort

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;  Namespace sort  {      class insertsorter      {public          static int[] Sort (int[] a)          {              insertsort (a);              return A;          }          private static void Insertsort (int[] myArray)          {             int i, j,temp;            for (i = 1; i < myarray.length; i++)              {                  temp = myarray[i];//Saves the current data, the current data is the data to                          be inserted//the elements before the array designator I and I are queued up in ascending sequence. C19/>for (j = i-1; J >= 0 && Myarray[j] >temp; j--)                  {                      myarray[j + 1] = Myarray[j];                                 }                  Myarray[j + 1] = temp;}}}  }

Example one:

Keyword sequence t= (13,6,3,31,9,27,5,11) write a sequence of intermediate processes that directly insert the sort.

"13", 6, 3, 31, 9, 27, 5, 11

"6, 13", 3, 31, 9, 27, 5, 11

"3, 6, 13", 31, 9, 27, 5, 11

"3, 6, 13,31", 9, 27, 5, 11

"3, 6, 9, 13,31", 27, 5, 11

"3, 6, 9, 13, 27, 31", 5, 11

"3, 5, 6, 9, 13, 27, 31", 11

"3, 5, 6, 9, 11,13,27, 31"

Small bet: Each small loop arranges only the order of the elements (similar to bubbling) in arrays 0 through I

Example two:

Example three:


In the worst case, the array is completely reversed, when inserting the 2nd element to examine the first 1 elements, when inserting the 3rd element, consider the first 2 elements, ..., insert the nth element, consider the first n-1 elements. Therefore, the worst case comparison is 1 + 2 + 3 + ... + (N-1), arithmetic progression sum, the result is N^2/2, so the worst case complexity is O (n^2).

In the best case, the array is already ordered, with each element inserted, only the previous element needs to be examined, so in the best case, the time complexity of the insertion Order is O (N).

The spatial complexity of the insertion sort is O (1), because when you insert a sort, you only need to use an extra space to store the "out" element, so the insertion order requires only an extra space to sort.

Spatial complexity (space complexity) is a measure of the amount of storage space that is temporarily occupied by an algorithm while it is running.

Because it is the array of its own sort, the latter part of the order of a little bit of comparison, movement, you can maintain the relative order of the same, so the insertion sort is a stable sorting algorithm.

The above is the C # Insert sort of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.