C_选择排序
2020-12-18 10:34
阅读:413
标签:定义排序 swap pre 主函数 min selection 函数 排序数组 mic
算法名称:选择排序
基本原理(从小到大):
S1:选出数组中最小元素(记下标为i)(一基准值也可)。
S2:从剩余待排序数组元素选出最小元素与下标为(i+1)元素交换。
S3:重复S2。
算法图示:
图片来自https://www.runoob.com/w3cnote/selection-sort.html
代码实现:
//先定义一个交换数组元素的函数
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//定义排序函数
void selection_sort(int arr[], int len)
{
int i, j;
for(i = 0; i arr[j]) //比基准值小则交换
swap(&arr[i], &arr[j]);
}
}
//编写主函数
#include
#include
int main()
{
void selection_sort(int arr[], int len);
int arr_beta[10] = {2, 4, 5, 3, 12, 6, 64, 12, 43, 22}; //测试数组
selection_sort(arr_beta, 10);
for(int i = 0; i
C_选择排序
标签:定义排序 swap pre 主函数 min selection 函数 排序数组 mic
原文地址:https://www.cnblogs.com/XiaoXin1900/p/14089803.html
评论
亲,登录后才可以留言!