冒泡排序与插入排序(C#实现)

本人应届生面试,发现被问了2次关于排序的算法。当时竟然没写出来!!!好吧,可能是用库函数多了,很久没搞算法了,在纸上写没感觉吧。

今天花了1个多小时写了下冒泡排序与插入排序(C#实现),并写了注释和小函数,力求算法分析透彻,自解释的代码。

  1 //优化建议:
  2 //1.定义<>比较器(而不是传入字符串命令让函数判断),方便指定排序顺序
  3 //2.凡是带有int endIndex, int insertValue参数的函数都应该做成重载,不指定则从数组开始,或直到数组末尾
  4 using System;
  5 using System.Collections.Generic;
  6 using System.Linq;
  7 using System.Text;
  8 
  9 namespace SimpleDelegate
 10 {
 11     public class ArraySort
 12     {
 13         /// <summary>
 14         /// 选择排序,从小到大
 15         /// </summary>
 16         /// <param name="arr">无需ref,数组为类(引用类型)自动传递指针(引用)</param>
 17         public static void SelectionSort(int[] arr)
 18         {
 19             //循环,将下标为0(第一个元素)...len-2(最后第二个元素)的元素与其后元素中最小的交换
 20             for (int i = 0; i < arr.Length - 1; i++)
 21             {
 22                 //获取从i至数组末尾范围内最小元素的下标
 23                 int minIndex = GetMinIndex(arr, i);
 24                 //交换,将最小元素移至i处
 25                 Swap(ref arr[minIndex], ref arr[i]);
 26             }
 27         }
 28         /// <summary>
 29         /// 交换变量值
 30         /// </summary>
 31         public static void Swap(ref int i,ref int j)
 32         {
 33             int tmp = i;i = j; j = tmp;
 34         }
 35         /// <summary>
 36         /// 找出下标beginIndex...len-1(最后一个元素)中最小的元素的下标 
 37         /// </summary>
 38         /// <param name="arr">数组</param>
 39         /// <param name="beginIndex">起始下标</param>
 40         /// <returns>数组从下标i...len-1(最后一个元素)最小的元素的下标</returns>
 41         public static int GetMinIndex(int[] arr, int beginIndex)
 42         {
 43             int min = beginIndex;
 44             for (int j = beginIndex + 1; j < arr.Length; j++)
 45             {
 46                 if (arr[min] > arr[j])
 47                     min = j;
 48             }
 49             return min;
 50         }
 51 
 52         /*********************************************************************/
 53         /// <summary>
 54         /// 插入排序,从小到大
 55         /// </summary>
 56         public static void InsertionSort(int[] arr)
 57         {
 58             for (int i = 1; i < arr.Length; i++)
 59             {
 60                 int insertValue = arr[i];
 61                 SortInsert(arr, i, insertValue);
 62             }
 63         }
 64         /// <summary>
 65         /// 按从小到大顺序插入元素。数组范围:开始至endIndex参数
 66         /// </summary>
 67         /// <param name="arr"></param>
 68         /// <param name="endIndex">插入的数组结束范围</param>
 69         /// <param name="insertValue">插入的元素</param>
 70         public static void SortInsert(int[] arr, int endIndex, int insertValue)
 71         {
 72             //找出插入点的下标
 73             int insertIndex = 0;    //假设是0
 74             for (insertIndex = 0; insertIndex < endIndex; insertIndex++)
 75             {
 76                 if (insertValue < arr[insertIndex])
 77                     break;
 78             }
 79             //插入点和其后的元素后移
 80             NextMove(arr,insertIndex,endIndex);
 81             //插入元素
 82             arr[insertIndex] = insertValue;
 83         }
 84         /// <summary>
 85         /// 从后往前后移,并返回被移出的元素
 86         /// 从前往后后移会导致覆盖(所有元素值变为arr[begIndex]的值)
 87         /// </summary>
 88         /// <param name="arr"></param>
 89         /// <param name="begIndex">数组的开始索引</param>
 90         /// <param name="endIndex">数组的结束索引</param>
 91         /// <returns>被移出的元素</returns>
 92         public static int NextMove(int[] arr, int begIndex, int endIndex)
 93         {
 94 
 95             for (int i = endIndex; i > begIndex; i--)
 96             {
 97                 arr[i] = arr[i - 1];
 98             }
 99             return arr[endIndex];
100         }     
101     }
102 }
View Code

但还可以继续抽象,也没写冒泡排序和快速排序等,未完待续。。。

以下为运行代码:

 1  static void Main(string[] args)
 2         {
 3             int[] arr = new int[] { 0, 0, 0, 100, 20, 200, -1, 30, 1000, 90 };
 4             ArraySort.SelectionSort(arr);
 5             //ArraySort.InsertionSort(arr);
 6             foreach (var item in arr)
 7             {
 8                 Console.WriteLine(item);
 9             }
10             Console.WriteLine("------------");
11             Console.Read();
12         }
View Code

 

希望对各位有用!!有错也请指出。

 

 

 

posted on 2013-12-08 20:06  nlh774  阅读(230)  评论(0编辑  收藏  举报