Do you know C # various sorts of compilation and performance?

Source: Internet
Author: User
Tags sort sorts

Soon graduated, review the C # data structure of the sorting algorithm, which mainly has bubble sort, direct insertion sort, simple choice of sorting and quick sort, in which reference to the old Zhao's Codetimer and eaglet performance counters, hereby thank you ~ ~

OK, let's start with our sorting algorithm.

Before sorting algorithm, we first define a 100-bit random sequence to perform the performance test of various sorting algorithms.

The code is as follows:

/// <summary>
/// 随机生成100位的数组
/// </summary>
/// <returns>返回生成数组</returns>
public static int[] RandomArray()
{
 Random ran = new Random();
 int[] arr = new int[100];
 int tem;
 for (int i = 0; i < 100; i++)
 {
  tem = ran.Next(1, 100);
  arr[i] = tem;
 }
 return arr;
}

1. Bubble sort (Bubble sort)

Basic idea: Compare the key codes of the adjacent records, if the key code in the preceding record is greater than the key code of the following record, swap them, otherwise it will not be exchanged.

The code is as follows:

/// <summary>
/// 冒泡排序算法
/// </summary>
public class BubbleSort : IAction
{
 #region IAction 成员
 public void Action()
 {
  int[] array = Program.RandomArray();
  for (int a = 0; a < array.Length; a++)
  {
   int item = 0;
   for (int b = array.Length - 1; b > a; b--)
    {
     if (array[b] < array[b - 1])
      {
       item = array[b];
       array[b] = array[b - 1];
       array[b - 1] = item;
      }
    }
  }
 }
 #endregion
}

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.