body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

简单选择排序(Simple Selection Sort):相比较冒泡排序,每次都是两两比较交换,n个元素n-1次比较可以确定1个元素的最终位置。简单选择排序法就是通过n-i次关键字的比较,从n-i+1个记录中选出关键字最小的记录,并和第i(1≤i≤n)个记录交换。
#include<iostream>
using namespace std;
//简单选择排序
int simpleSelectionSort(int* arr,int length);
void swap(int& elem1,int& elem2);
void test();
void printArr(int* arr,int length);
void swap(int& elem1,int& elem2)
{
        int tmp = elem1;
        elem1 = elem2;
        elem2 = tmp;
}
int simpleSelectionSort(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return -1;
        int minPos = 0;
        for(int idx=0;idx!=length;++idx)
        {
                minPos = idx;
                for(int iidx=idx+1;iidx<length;++iidx)
                {
                        if(arr[iidx]<arr[minPos])
                        {
                                minPos = iidx;
                        }
                }
                if(idx!=minPos) 
                {
                        swap(arr[idx],arr[minPos]);
                }
        }
        return 0;
}
void printArr(int* arr,int length)
{
        if(NULL==arr||length<=0)
                return ;
        for(int idx=0;idx!=length;++idx)
        {
                cout<<arr[idx]<<" ";
        }
        cout<<endl;
}
void test()
{
        int arr[] = {6,5,3,1,8,7,2,4};
        printArr(arr,8);
        simpleSelectionSort(arr,8);
        printArr(arr,8);
        cout<<endl;
        int arr1[] = {1,2,3,4,5,6,7,8};
        printArr(arr1,8);
        simpleSelectionSort(arr1,8);
        printArr(arr1,8);
        cout<<endl;
        int arr2[] = {2,2,2,2};
        printArr(arr2,4);
        simpleSelectionSort(arr2,4);
        printArr(arr2,4);
        cout<<endl;
        int arr3[] = {2,2,1,2};
        printArr(arr3,4);
        simpleSelectionSort(arr3,4);
        printArr(arr3,4);
        cout<<endl;
        int* arr4 = NULL;
        printArr(arr4,4);
        simpleSelectionSort(arr4,4);
        printArr(arr4,4);
        cout<<endl;
}
int main()
{
        test();
        system("pause");
}

简单选择排序(Simple Selection Sort)的更多相关文章

  1. 数据结构 - 只需选择排序(simple selection sort) 详细说明 和 代码(C++)

    数据结构 - 只需选择排序(simple selection sort) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/28601 ...

  2. 选择排序(1)——简单选择排序(selection sort)

    选择排序是一种很常见的排序算法,它需要对数组 中的元素进行多次遍历.每经过一次循环,选择最小的元素并把它放在靠近数组前端的位置. 代码实现: public static void selectionS ...

  3. 《算法4》2.1 - 选择排序算法(Selection Sort), Python实现

    选择排序算法(Selection Sort)是排序算法的一种初级算法.虽然比较简单,但是基础,理解了有助于后面学习更高深算法,勿以勿小而不为. 排序算法的语言描述: 给定一组物体,根据他们的某种可量化 ...

  4. 数据结构 - 树形选择排序 (tree selection sort) 具体解释 及 代码(C++)

    树形选择排序 (tree selection sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 算法逻辑: 依据节点的大小, ...

  5. js 实现排序算法 -- 选择排序(Selection Sort)

    原文: 十大经典排序算法(动图演示) 选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存 ...

  6. 【排序基础】1、选择排序法 - Selection Sort

    文章目录 选择排序法 - Selection Sort 为什么要学习O(n^2)的排序算法? 选择排序算法思想 操作:选择排序代码实现 选择排序法 - Selection Sort 简单记录-bobo ...

  7. 【算法】选择排序(Selection Sort)(二)

    选择排序(Selection Sort) 选择排序(Selection-sort)是一种简单直观的排序算法.它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余 ...

  8. 简单选择排序 Selection Sort 和树形选择排序 Tree Selection Sort

    选择排序 Selection Sort 选择排序的基本思想是:每一趟在剩余未排序的若干记录中选取关键字最小的(也可以是最大的,本文中均考虑排升序)记录作为有序序列中下一个记录. 如第i趟选择排序就是在 ...

  9. 选择排序(Selection Sort)

    选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...

随机推荐

  1. Angular 学习笔记 Material

    以后都不会写 0 到 1 的学习记入了,因为官网已经写得很好了. 这里只写一些遇到的坑或则概念和需要注意的事情. Material Table 1. ng-content 无法传递 CdkColumn ...

  2. thinkphp5的Auth权限认证实战

    thinkphp5的Auth权限认证实战 一.总结 一句话总结:基于角色的权限管理(真正做一遍,就会发现很简单,不然一直都是半懂不懂的) 角色 权限 真正做一遍,就会发现很简单,不然一直都是半懂不懂的 ...

  3. R语言画点状误差线

    现在项目需要R语言做几个线性拟合,画一些点图,突然需要画误差线,网上找了下,可以用代码实现..效果如下 xx1<-c(xxxxxx,xxxx,xxxxx) yy1<-c(xxxxxx,xx ...

  4. ubuntu16.04安装LCM

    1.sudo apt-get install build-essential autoconf automake autopoint libglib2.0-dev libtool openjdk-8- ...

  5. GROUP by 方法  C#

    1.用两层循环计算,前提条件是数据已经按分组的列排好序的. DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { ...

  6. com.netflix.zuul.exception.ZuulException: Forwarding error

    一.问题描述 在使用Spring Cloud的zuul组件,做路由转发时,每次重新启动后端服务,头几次调用都会出现com.netflix.zuul.exception.ZuulException: F ...

  7. You Don't Know JS: Scope & Closures (附加:Lexical/dynamic作用域)(附加:Lexical-this)

    JavaScript只有Lexical Scope 模式 Lexical Scope就是在写代码的时候,定义函数的时候创建的作用域! 而动态作用域是在runtime时,函数被调用的地方的作用域! 实际 ...

  8. MySQL事务(三)

    一.事务(Innodb锁)的隔离级别概述 并发事务带来的问题: 更新丢失(lost update):当两个或多个事务选择同一行,然后基于最初选定的值更新该行时,由于每个事务都不知道其他事务的存在,就会 ...

  9. laravel中常用的获取路径的函数

    1. app_path() // 获取app目录的路径 2. base_path() // 根目录的路径 3. config_path() // config目录的路径 4. public_path( ...

  10. layuitable设置数据表

    如图,要设置类似这样的格式 步骤如下: 1. 设置一个列表表格 2. 获取数据 table.render({ elem: '#pl-list' ,id: 'pl_table' ,height: 480 ...