选择排序selection sort

简介: 找最小的和第一个位置做交换,递归下去#include using namespace std;void selectionSort(int arr[],int n) { for (int i = 0; i < n; ++i) { ...

找最小的和第一个位置做交换,递归下去

#include <iostream>

using  namespace std;

void selectionSort(int arr[],int n) {
    for (int i = 0; i < n; ++i) {
        //寻找【i,n】区间的最小值
        int minIndex = i;
        for (int j = i + 1; j < n; ++j) {
            if (arr[j] < arr[minIndex]) minIndex = j;
            swap(arr[i], arr[minIndex]);
        }
    }
}
int main() {
    int a[10] = {10,9,8,7,6,5,4,3,2,1};
    selectionSort(a,10);
    for (int i = 0; i < 10; ++i) {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}
img_4c23e54602f8b6778d28b97d61676854.png
image.png
相关文章
|
9月前
|
搜索推荐 算法 C#
C#选择排序(Selection Sort)算法
C#选择排序(Selection Sort)算法
|
9月前
|
人工智能 算法 搜索推荐
|
9月前
|
存储 算法 搜索推荐
|
10月前
|
存储 搜索推荐 算法
十大排序之Selection Sort 选择排序
十大排序之Selection Sort 选择排序
|
搜索推荐 算法 数据可视化
【基础篇】9 # 排序:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)、选择排序(Selection Sort)
【基础篇】9 # 排序:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)、选择排序(Selection Sort)
75 0
【基础篇】9 # 排序:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)、选择排序(Selection Sort)
2-路插入排序(Two-Way Insertion Sort)
算法介绍 算法描述 算法分析 代码实现 参考
|
搜索推荐 容器
常用排序算法 sort() random_shuffle() merge() reverse()
常用排序算法 sort() random_shuffle() merge() reverse()
常用排序算法 sort() random_shuffle() merge() reverse()
|
算法 Java
简单选择排序(Simple Selection Sort)
算法介绍 算法描述 动图演示 算法分析 代码实现 算法优化 参考
简单选择排序(Simple Selection Sort)
|
算法 搜索推荐 Java
冒泡排序(Bubble Sort)
算法介绍 算法描述 动图演示 算法分析 代码实现 算法优化 参考
冒泡排序(Bubble Sort)