美文网首页
Selection Sort

Selection Sort

作者: BeeCaffe | 来源:发表于2020-05-02 10:44 被阅读0次

Selection Sort

Supposing we have a unordered array nums. Firstly, we swap the minmum element in nums to nums[0].Then, swaping to sub-minmum element in nums to nums[1] ..., the rest can do the same manner, until we find the last elemnts. This algorithm is so-called selection sort, because for each time we choose the minimum element in the rest array, and swap it to the front element.

Code

#include<iostream>
#include<vector>
using namespace std;
void choose_sort(vector<int> &nums){
    int min_=0,idx=0;
    for(int i=0;i<nums.size()-1;++i){
        min_=nums[i];idx=i;
        for(int j=i+1;j<nums.size();++j){
            if(min_>nums[j]){
                min_=nums[j];
                idx=j;
            }
        }
        nums[idx]=nums[i];
        nums[i]=min_;
    }
}

int main(){
    vector<int> nums={3,1,2,4,6,7,9,8};
    choose_sort(nums);
    for(auto i:nums) cout<<i<<" ";
    cout<<endl;
    exit(0);
}

相关文章

  • Algorithms

    BinarySearch Sort Selection sort Insertion sort

  • sorting algorithoms

    Bubble Sort Selection Sort Insertion Sort search : O(n) o...

  • 常见排序算法

    冒泡排序 Bubble Sort 选择排序 Selection Sort 计数排序 Counting Sort 桶...

  • Selection Sort

    Given an array of integers, sort the elements in the arra...

  • selection sort

    从第一个开始,找到序列中最小的,和第一个交换;然后从第二个开始,找到最小的和第二个交换……O(N*N) selec...

  • Selection Sort

    Selection Sort Supposing we have a unordered array nums. ...

  • Selection Sort

  • 排序算法之选择排序

    选择排序法(selection sort) 来自维基百科选择排序(Selection sort)是一种简单直观的排...

  • Algorithm learning: Selection So

    The selection sort algorithm sorts an array by repeatedly...

  • 排序经典算法

    冒泡算法(bubble sort) 选择排序(selection sort) 插入排序(insertion sor...

网友评论

      本文标题:Selection Sort

      本文链接:https://www.haomeiwen.com/subject/gazhghtx.html