C# 選擇排序

黄舟
發布: 2017-02-09 16:14:11
原創
1329 人瀏覽過

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;  
        }  
    }  
}
登入後複製

選擇排序的想法:

C# 選擇排序


例子:

相關內容請關注PHP中文網(www.php.cn)! C# 選擇排序

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!