首页 > 编程语言 > 详细

排序算法练习

时间:2014-10-31 15:20:35      阅读:334      评论:0      收藏:0      [点我收藏+]

一、直接插入排序

 

bubuko.com,布布扣
 1     class Program
 2     {
 3         /// <summary>
 4         /// swap two number
 5         /// </summary>
 6         /// <param name="a"></param>
 7         /// <param name="b"></param>
 8         static void Swap(ref int a, ref int b)
 9         {
10             int temp = a;
11             a = b;
12             b = temp;
13         }
14         static void Main(string[] args)
15         {
16             Console.WriteLine("Algorithm test first!\nInsert Sort Start....");
17             int[] arr = new int[] { 2, 3, 1, 0, 5 };
18 
19             for (int i = 0; i < arr.Length; i++)
20             {
21                 for (int j = i + 1; j >= 1; j--)
22                 {
23                     if (j >= arr.Length)
24                         break;
25                     if (arr[j] < arr[j - 1])
26                     {
27                         Swap(ref arr[j], ref arr[j-1]);
28                     }
29                 }
30             }
31 
32             foreach(int i in arr)
33             {
34                 Console.Write(i + " ");
35             }
36 
37         }
38     }
View Code

 

排序算法练习

原文:http://www.cnblogs.com/Vincentblogs/p/4064910.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!