selection sort
来源:互联网 发布:知无涯者 拉马努金传 编辑:程序博客网 时间:2024/06/09 23:26
选择排序算法, 一种简单的排序算法
复杂度: O(n^2)
伪代码:
procedure selectSort( A : list of sortable items ) n = length(A) for i=0 to n-1 inclusive domin = i for j = i+1 to n inclusive doif A[j] < A[min] thenmin = jend ifend forif min != i thenswap(A[i], A[min])end if end forend procedureC语言实现:
#include <stdio.h>#if 1#include "utility.h"#else#define SWAP(a,b) {(a)=(a)^(b); (b)=(a)^(b); (a)=(a)^(b);}#endifvoid select_sort(int arr[], int len){ int min; int i,j; for (i=0; i<len-1; i++) { min = i; /*find the index of smallest element*/ for (j=i+1; j<len; j++) if (arr[j]<arr[min]) min = j; if (min != i) { /*printf("swap %d, %d\n", arr[i], arr[min]);*/ SWAP(arr[i],arr[min]); } }}int main(){ int arr[] = {5,1,4,2,8,9,7,3,6,17,11}; int len = ARR_SIZE(arr); printf("list before sort :\n"); print_arr(arr, len); select_sort(arr, len); printf("select sort result:\n"); print_arr(arr, len); return 0;}运行输出:
dennis@dennis-VirtualBox:~/project/sort$ gcc -o select select_sort.c dennis@dennis-VirtualBox:~/project/sort$ ./select list before sort :5, 1, 4, 2, 8, 9, 7, 3, 6, 17, 11, select sort result:1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 17, dennis@dennis-VirtualBox:~/project/sort$
参考: Wikipedia Selection sort
- Selection sort
- Selection Sort
- Selection sort
- Selection Sort
- selection sort
- selection sort
- Selection sort
- 选择排序(selection sort)
- 选择排序(Selection Sort)
- The Selection Sort
- Algorithm---Selection Sort
- selection sort 选择排序
- 选择排序 Selection Sort
- 选择排序(Selection Sort)
- 选择排序(Selection Sort)
- 选择排序 (Selection Sort)
- 选择排序(Selection sort)
- 选择排序 (Selection sort)
- android源码资源
- New Ycnd 2D NOC版本初步规划完成
- MFC对话框设置静态文本框颜色
- Unity中的线程
- c语言常用头文件中包含的函数
- selection sort
- SQL Server 多表联合查询取最新一条数据
- 黑马程序员_获取字符串 字母出现的次数
- LDAP 用户认证!
- Android RelativeLayout 属性大全
- 如何理解c和c ++的复杂类型声明
- Google发布新一代Google Maps:全新3D、离线服务、轻便拍摄装备、用户参与
- 项目总结(1)
- 以超级管理员(root)身份运行adb和fastboot