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. Unity Shader入门精要之 screen post-processing effect

    本篇记录了学习Unity Shader入门精要的屏幕后处理的一些知识点. OnRenderImage(RenderTexture src, RenderTexture dest) 以上函数是Unity ...

  2. ADO.NET Entity Framework学习笔记(3)ObjectContext

    ADO.NET Entity Framework学习笔记(3)ObjectContext对象[转]   说明 ObjectContext提供了管理数据的功能 Context操作数据 AddObject ...

  3. Android -------- MVC,MVP 和 MVVM 架构设计模式

    MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理解,但是要讲清楚,它与衍生的 MVP 和 MVVM 架构的区别就不容易了. 一.MVC MV ...

  4. Spring Batch 介绍

    在企业应用的关键环境中,通常有需要很多应用来来处理大量的应用.这商业操作包括了自动化,并且负责的处理程序来对大量数据进行高效的处理,通常这些程序不需要人工进行干预.这些事件包括有基于时间周期产生的操作 ...

  5. jquery插件中找到好玩插件 http://www.jq22.com/

    超实用的angular.js无刷新分页完整案例 http://www.jq22.com/jquery-info14714 js联动选择插件mobileSelect.js http://www.jq22 ...

  6. Vue音乐项目笔记(五)

    1.搜索列表的点击删除.删除全部的交互事件 https://blog.csdn.net/weixin_40814356/article/details/80496097 seach组件中放search ...

  7. 00-自测5. Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  8. SWUST OJ(955)

    单链表上查找算法的实现 #include <stdio.h> #include <stdlib.h> typedef struct LinkNode //单链表节点结构的定义 ...

  9. 【PowerDesigner】【10】绘制类图

    前言:我感觉我也是一知半解,参考博客的内容会比我的文章更有帮助 用途:描述项目中类与类的关系(即描述java文件) 正文: 1,新建oomFile→New Model→Model types→Obje ...

  10. sqlserver用timestamp帮助解决数据并发冲突 转【转】

    http://blog.csdn.net/u011014032/article/details/42936783 关于并发请求,网上很多朋友都说的很详细了,我就不在这里献丑了.这里只记录下刚刚完工的那 ...