資料結構-排序: 選擇排序(直接選擇排序法)

來源:互聯網
上載者:User

直接選擇排序(Straight Selection Sort)
直接選擇排序的基本思想
     n個記錄的檔案的直接選擇排序可經過n-1趟直接選擇排序得到有序結果:
 ①初始狀態:無序區為R[1..n],有序區為空白。
 ②第1趟排序
     在無序區R[1..n]中選出關鍵字最小的記錄R[k],將它與無序區的第1個記錄R[1]交換,使R[1..1]和R[2..n]分別變為記錄個數增加1個的新有序區和記錄個數減少1個的新無序區。
  ……
 ③第i趟排序
  第i趟排序開始時,當前有序區和無序區分別為R[1..i-1]和R[i..n](1≤i≤n-1)。該趟排序從當前無序區中選出關鍵字最小的記錄R[k],將它與無序區的第1個記錄R[i]交換,使R[1..i]和R[i+1..n]分別變為記錄個數增加1個的新有序區和記錄個數減少1個的新無序區。
     這樣,n個記錄的檔案的直接選擇排序可經過n-1趟直接選擇排序得到有序結果。

using System;
using System.Collections.Generic;
using System.Text;

namespace ExSelectionSorter
{
    class SelectionSorter
    {
        private int min;
        public void Sort(int[] arr)
        {
            for (int i = 0; i < arr.Length - 1; ++i)
            {
                min = i;
                for (int j = i + 1; j < arr.Length; ++j)
                {
                    if (arr[j] < arr[min])
                        min = j;
                }
                int t = arr[min];
                arr[min] = arr[i];
                arr[i] = t;
            }
        }
        static void Main(string[] args)
        {
            int[] array = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
            SelectionSorter s = new SelectionSorter();
            s.Sort(array);
            foreach (int m in array)
                Console.WriteLine("{0}", m);
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.