<strike id="cakm0"></strike>
  • <button id="cakm0"><dl id="cakm0"></dl></button>
  • <samp id="cakm0"><tbody id="cakm0"></tbody></samp>
    <samp id="cakm0"><pre id="cakm0"></pre></samp><ul id="cakm0"></ul>
    <strike id="cakm0"></strike>
    <li id="cakm0"></li>
  • <ul id="cakm0"></ul>
  • 更多精彩內(nèi)容,歡迎關(guān)注:

    視頻號(hào)
    視頻號(hào)

    抖音
    抖音

    快手
    快手

    微博
    微博

    選擇一個(gè)排序算法時(shí)要考慮

    文檔

    選擇一個(gè)排序算法時(shí)要考慮

    選擇排序是一種簡(jiǎn)單直觀的排序算法,無(wú)論什么數(shù)據(jù)進(jìn)去都是 O(n?) 的時(shí)間復(fù)雜度。所以用到它的時(shí)候,數(shù)據(jù)規(guī)模越小越好。唯一的好處可能就是不占用額外的內(nèi)存空間。
    推薦度:
    導(dǎo)讀選擇排序是一種簡(jiǎn)單直觀的排序算法,無(wú)論什么數(shù)據(jù)進(jìn)去都是 O(n?) 的時(shí)間復(fù)雜度。所以用到它的時(shí)候,數(shù)據(jù)規(guī)模越小越好。唯一的好處可能就是不占用額外的內(nèi)存空間。
    .example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px}

    排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是選擇排序算法:

    選擇排序是一種簡(jiǎn)單直觀的排序算法,無(wú)論什么數(shù)據(jù)進(jìn)去都是 O(n?) 的時(shí)間復(fù)雜度。所以用到它的時(shí)候,數(shù)據(jù)規(guī)模越小越好。唯一的好處可能就是不占用額外的內(nèi)存空間了吧。

    1. 算法步驟

    首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。

    再?gòu)氖S辔磁判蛟刂欣^續(xù)尋找最小(大)元素,然后放到已排序序列的末尾。

    重復(fù)第二步,直到所有元素均排序完畢。

    2. 動(dòng)圖演示

    代碼實(shí)現(xiàn)JavaScript 代碼實(shí)現(xiàn)實(shí)例 function selectionSort(arr) {? ? var len = arr.length;? ? var minIndex, temp;? ? for (var i = 0; i < len - 1; i++) {? ? ? ? minIndex = i;? ? ? ? for (var j = i + 1; j < len; j++) {? ? ? ? ? ? if (arr[j] < arr[minIndex]) { ? ? // 尋找最小的數(shù)? ? ? ? ? ? ? ? minIndex = j; ? ? ? ? ? ? ? ? // 將最小數(shù)的索引保存? ? ? ? ? ? }? ? ? ? }? ? ? ? temp = arr[i];? ? ? ? arr[i] = arr[minIndex];? ? ? ? arr[minIndex] = temp;? ? }? ? return arr;}Python 代碼實(shí)現(xiàn)實(shí)例 def selectionSort(arr):? ? for i in range(len(arr) - 1):? ? ? ? # 記錄最小數(shù)的索引? ? ? ? minIndex = i? ? ? ? for j in range(i + 1, len(arr)):? ? ? ? ? ? if arr[j] < arr[minIndex]:? ? ? ? ? ? ? ? minIndex = j? ? ? ? # i 不是最小數(shù)時(shí),將 i 和最小數(shù)進(jìn)行交換? ? ? ? if i != minIndex:? ? ? ? ? ? arr[i], arr[minIndex] = arr[minIndex], arr[i]? ? return arrGo 代碼實(shí)現(xiàn)實(shí)例 func selectionSort(arr []int) []int {? ? ? ? length := len(arr)? ? ? ? for i := 0; i < length-1; i++ {? ? ? ? ? ? ? ? min := i? ? ? ? ? ? ? ? for j := i + 1; j < length; j++ {? ? ? ? ? ? ? ? ? ? ? ? if arr[min] > arr[j] {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? min = j? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? arr[i], arr[min] = arr[min], arr[i]? ? ? ? }? ? ? ? return arr}Java 代碼實(shí)現(xiàn)實(shí)例 public class SelectionSort implements IArraySort {? ? @Override? ? public int[] sort(int[] sourceArray) throws Exception {? ? ? ? int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);? ? ? ? // 總共要經(jīng)過 N-1 輪比較? ? ? ? for (int i = 0; i < arr.length - 1; i++) {? ? ? ? ? ? int min = i;? ? ? ? ? ? // 每輪需要比較的次數(shù) N-i? ? ? ? ? ? for (int j = i + 1; j < arr.length; j++) {? ? ? ? ? ? ? ? if (arr[j] < arr[min]) {? ? ? ? ? ? ? ? ? ? // 記錄目前能找到的最小值元素的下標(biāo)? ? ? ? ? ? ? ? ? ? min = j;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? // 將找到的最小值和i位置所在的值進(jìn)行交換? ? ? ? ? ? if (i != min) {? ? ? ? ? ? ? ? int tmp = arr[i];? ? ? ? ? ? ? ? arr[i] = arr[min];? ? ? ? ? ? ? ? arr[min] = tmp;? ? ? ? ? ? }? ? ? ? }? ? ? ? return arr;? ? }}PHP 代碼實(shí)現(xiàn)實(shí)例 function selectionSort($arr){? ? $len = count($arr);? ? for ($i = 0; $i < $len - 1; $i++) {? ? ? ? $minIndex = $i;? ? ? ? for ($j = $i + 1; $j < $len; $j++) {? ? ? ? ? ? if ($arr[$j] < $arr[$minIndex]) {? ? ? ? ? ? ? ? $minIndex = $j;? ? ? ? ? ? }? ? ? ? }? ? ? ? $temp = $arr[$i];? ? ? ? $arr[$i] = $arr[$minIndex];? ? ? ? $arr[$minIndex] = $temp;? ? }? ? return $arr;}C 語(yǔ)言實(shí)例 void swap(int *a,int *b) //交換兩個(gè)變數(shù){? ? int temp = *a;? ? *a = *b;? ? *b = temp;}void selection_sort(int arr[], int len) {? ? int i,j;? ? ? ? for (i = 0 ; i < len - 1 ; i++) ? ? {? ? ? ? ? ? ? ? int min = i;? ? ? ? ? ? ? ? for (j = i + 1; j < len; j++) ? ? //走訪未排序的元素? ? ? ? ? ? ? ? ? ? ? ? if (arr[j] < arr[min]) ? ?//找到目前最小值? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? min = j; ? ?//紀(jì)錄最小值? ? ? ? ? ? ? ? swap(&arr[min], &arr[i]); ? ?//做交換? ? ? ? }}C++實(shí)例 template //整數(shù)或浮點(diǎn)數(shù)皆可使用,若要使用物件(class)時(shí)必須設(shè)定大於(>)的運(yùn)算子功能void selection_sort(std::vector& arr) {? ? ? ? for (int i = 0; i < arr.size() - 1; i++) {? ? ? ? ? ? ? ? int min = i;? ? ? ? ? ? ? ? for (int j = i + 1; j < arr.size(); j++)? ? ? ? ? ? ? ? ? ? ? ? if (arr[j] < arr[min])? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? min = j;? ? ? ? ? ? ? ? std::swap(arr[i], arr[min]);? ? ? ? }}C#實(shí)例 static void selection_sort(T[] arr) where T : System.IComparable{//整數(shù)或浮點(diǎn)數(shù)皆可使用? ? ? ? int i, j, min, len = arr.Length;? ? ? ? T temp;? ? ? ? for (i = 0; i < len - 1; i++) {? ? ? ? ? ? ? ? min = i;? ? ? ? ? ? ? ? for (j = i + 1; j < len; j++)? ? ? ? ? ? ? ? ? ? ? ? if (arr[min].CompareTo(arr[j]) > 0)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? min = j;? ? ? ? ? ? ? ? temp = arr[min];? ? ? ? ? ? ? ? arr[min] = arr[i];? ? ? ? ? ? ? ? arr[i] = temp;? ? ? ? }}Swift實(shí)例 import Foundation/// 選擇排序////// - Parameter list: 需要排序的數(shù)組func selectionSort(_ list: inout [Int]) -> Void {? ? for j in 0.. list[i] {? ? ? ? ? ? ? ? minIndex = i? ? ? ? ? ? }? ? ? ? }? ? ? ? list.swapAt(j, minIndex)? ? }}

    原文地址:https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/2.selectionSort.md

    參考地址:https://zh.wikipedia.org/wiki/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F

    以下是熱心網(wǎng)友對(duì)選擇排序算法的補(bǔ)充,僅供參考:

    熱心網(wǎng)友提供的補(bǔ)充1:

    Kotlin 實(shí)現(xiàn)

    class SelectionSort { 
        /** 
        * 拓展IntArray為他提供數(shù)據(jù)兩個(gè)數(shù)交換位置的方法 
        * @param i 第一個(gè)數(shù)的下標(biāo) 
        * @param j 第二個(gè)數(shù)的下標(biāo) 
        */ 
        fun IntArray.swap(i:Int,j:Int){ 
            var temp=this[i] 
            this[i]=this[j] 
            this[j]=temp 
        } 
        fun selectionSort(array: IntArray):IntArray{
            for (i in array.indices){ 
                //假設(shè)最小值是i 
                var min=i 
                var j=i+1 
                while (j in array.indices){ 
                    if (array[j]以上為選擇排序算法詳細(xì)介紹,插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等排序算法各有優(yōu)缺點(diǎn),用一張圖概括: 

    關(guān)于時(shí)間復(fù)雜度

    平方階 (O(n2)) 排序 各類簡(jiǎn)單排序:直接插入、直接選擇和冒泡排序。

    線性對(duì)數(shù)階 (O(nlog2n)) 排序 快速排序、堆排序和歸并排序;

    O(n1+§)) 排序,§ 是介于 0 和 1 之間的常數(shù)。 希爾排序

    線性階 (O(n)) 排序 基數(shù)排序,此外還有桶、箱排序。

    關(guān)于穩(wěn)定性

    穩(wěn)定的排序算法:冒泡排序、插入排序、歸并排序和基數(shù)排序。

    不是穩(wěn)定的排序算法:選擇排序、快速排序、希爾排序、堆排序。

    名詞解釋:

    n:數(shù)據(jù)規(guī)模

    k:"桶"的個(gè)數(shù)

    In-place:占用常數(shù)內(nèi)存,不占用額外內(nèi)存

    Out-place:占用額外內(nèi)存

    穩(wěn)定性:排序后 2 個(gè)相等鍵值的順序和排序之前它們的順序相同

    文檔

    選擇一個(gè)排序算法時(shí)要考慮

    選擇排序是一種簡(jiǎn)單直觀的排序算法,無(wú)論什么數(shù)據(jù)進(jìn)去都是 O(n?) 的時(shí)間復(fù)雜度。所以用到它的時(shí)候,數(shù)據(jù)規(guī)模越小越好。唯一的好處可能就是不占用額外的內(nèi)存空間。
    推薦度:
    為你推薦
    資訊專欄
    熱門視頻
    相關(guān)推薦
    增加標(biāo)志的冒泡法排序 簡(jiǎn)單選擇排序圖解 冒泡排序流程圖怎么畫 直接選擇排序法圖解 冒泡排序 c語(yǔ)言數(shù)組選擇排序 冒泡排序比較次數(shù)公式 簡(jiǎn)單選擇排序過程 數(shù)據(jù)結(jié)構(gòu)冒泡排序 實(shí)現(xiàn)選擇排序算法 冒泡排序java 選擇排序c語(yǔ)言代碼 冒泡排序算法詳細(xì)舉例 簡(jiǎn)單選擇排序基本過程 數(shù)據(jù)結(jié)構(gòu)冒泡排序算法 選擇法對(duì)10個(gè)整數(shù)排序 冒泡排序算法偽代碼 直接選擇排序又叫 優(yōu)化的冒泡排序c語(yǔ)言 選擇排序法和冒泡法的區(qū)別 冒泡排序c語(yǔ)言代碼 選擇排序算法c 冒泡排序算法代碼 直接選擇排序c語(yǔ)言 c語(yǔ)言希爾排序例題 冒泡排序結(jié)果 直接選擇排序比較次數(shù) 希爾排序的算法流程圖 冒泡排序圖解 簡(jiǎn)單選擇法排序 希爾排序數(shù)據(jù)結(jié)構(gòu) 冒泡排序分析 冒泡排序比較次數(shù) 直接選擇排序圖解 希爾排序c語(yǔ)言代碼 冒泡排序法C語(yǔ)言 選擇排序的原理 希爾排序算法c語(yǔ)言 歸并排序是如何進(jìn)行的 c語(yǔ)言冒泡排序法詳解
    Top 老司机精品视频在线| 日韩精品无码久久一区二区三 | 四虎亚洲精品高清在线观看| 久久精品无码一区二区日韩AV| 大香伊人久久精品一区二区| 国产精品人成在线播放新网站| 国产成人亚洲精品电影| 精品久久人妻av中文字幕| 亚洲精品乱码久久久久久久久久久久 | 亚洲精品国产电影午夜| 久久久久久久久久免免费精品| 精品国产夜色在线| 2021国产精品久久| 精品国产福利第一区二区三区| 合区精品中文字幕| 国产精品自在线天天看片| 国产成人精品天堂| 亚洲午夜精品久久久久久人妖 | 91国在线啪精品一区| 国产农村乱子伦精品视频| 亚洲精品国产综合久久久久紧| 99国产精品免费观看视频| 亚洲精品乱码久久久久久久久久久久| 国产69精品久久久久妇女| 日产精品一卡2卡三卡4乱码| 亚洲精品午夜在线观看| 一区二区三区国产精品| 亚洲精品自产拍在线观看| 久久97久久97精品免视看秋霞| 免费视频精品一区二区| 精品国偷自产在线视频99| 99久久久精品免费观看国产| 精品无码AV一区二区三区不卡 | 亚洲精品成人片在线播放| 国产精品亚洲w码日韩中文| 精品乱码一区二区三区在线| 国产成人久久精品二三区麻豆| 98色精品视频在线| 国产成人精品日本亚洲18图| 91精品国产高清久久久久久国产嫩草 | 国产69精品久久久久观看软件|