C# 选择排序

黄舟
黄舟 原创
2017-02-09 16:14:11 1017浏览

C# 选择排序

using System;  
  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace Sort  
{  
    class SelectSorter  
    {  
        public static int[] Sort(int[] a)  
        {  
            SelectSort(a);  
            return a;  
        }  
        private static void SelectSort(int[] myArray)  
        {  
            int i, j, smallest;  
            //数据起始位置,从0到倒数第二个数据  
  
       for (i = 0; i < myArray.Length - 1; i++)  
            {  
                smallest = i;//记录最小数据的下标  
                for (j = i + 1; j < myArray.Length; j++)  
                {  
                    //在剩下的数据中寻找最小数据  
  
            if (myArray[j] < myArray[smallest])  
                    {  
                        smallest = j;//如果有比它更小的,记录下标  
                    }  
                }  
        //将最小数据和未排序的第一个数据交换  
                Swap(ref myArray[i], ref myArray[smallest]);  
            }  
        }  
        private static void Swap(ref int left, ref int right)  
        {  
            int temp;  
            temp = left;  
            left = right;  
            right = temp;  
        }  
    }  
}

选择排序的思想:

1051.jpg


例子:

1052.jpg

以上就是C# 选择排序的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:C#希尔排序 下一条:C# 快速排序