Visual C # Common sort algorithms

Source: Internet
Author: User

Some time ago because the project needs, made a class for the sorting of the array, by the way the previous learned several sorting algorithms in C # implementation. C # Some of the mechanisms to explain the algorithm is implemented. Before reading this, we need some basic understanding of C #, understand the function of the method parameter out, ref, and grasp some basic ideas of object-oriented.

1. Insert Sort

1.1. Basic ideas:

Each time the data element to be sorted is inserted into the appropriate position in the previously sorted sequence, the sequence remains in order until all the data elements to be sorted are inserted.

1.2. Sorting process:

"Sample":

[Initial keyword] [49] 38 65 97 76 13 27 49

(38) [38 49] 65 97 76 13 27 49

(65) [38 49 65] 97 76 13 27 49

(97) [38 49 65 97] 76 13 27 49

(76) [38 49 65 76 97] 13 27 49

(13) [13 38 49 65 76 97] 27 49

(27) [13 27 38 49 65 76 97] 49

(49) [13 27 38 49 49 65 76 97]

1.3. Program implementation

<summary>
/// 插入排序算法
/// </summary>
/// <param name="dblArray"></param>
static void InsertSort(ref double[] dblArray)
{
 for(int i = 1 ; i < dblArray.Length ; i++)
 {
  int frontArrayIndex = i-1 ;
  int CurrentChangeIndex = i ;
  while(frontArrayIndex>=0)
  {
   if(dblArray[CurrentChangeIndex] < dblArray[frontArrayIndex])
   {
    ChangeValue(ref dblArray[CurrentChangeIndex],ref dblArray[frontArrayIndex]);
    CurrentChangeIndex = frontArrayIndex ;
   }
   frontArrayIndex--;
  }
 }
}
/// <summary>
/// 在内存中交换两个数字的值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
static void ChangeValue (ref double A ,ref double B)
{
 double Temp = A ;
 A = B ;
 B = Temp ;
}

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.