zoukankan      html  css  js  c++  java
  • //选择排序

    //选择排序
    #include "stdafx.h"
    using namespace std;
    #include<vector>
    #include<string>

    class Solution
    {
    public:

        static int* bubbleSort(int* array, int len)
        {
            int current;
            int preIndex;
            for (int i = 0; i < len; i++)
            {
                current = array[i+1]; //从未排序的第一个开始
                preIndex = i;  //preIndex 是已经排序好的数组的最后一个  下标
                while (current>0 && current< array[preIndex])  //拿current 从后往前比较
                {
                    // case1:
                    //     { 1, 5, 7, 6, 2, 3 };  比如说6、往前找、找到5比6小、把6插在5后面
                    array[preIndex + 1] = array[preIndex]; //找到比 current 小的、把current 插到这个数的前面,,,这个数往后移一位
                    preIndex--;                //只要current往前走一步、之前这个位置上的就要往后移一位
                                            //这里往前查的时候、刚好找到了把    current换上去、
                                            // 这里已经找到了、但是还要走case 2、所以要--、相当于所有的位置不变
                }
                // case 2:
                array[preIndex+1] = current;  //提出来一看、已经是排好序的、直接查下一个、把index +1 就是下一个要找的
            }
            return array;
        }

    };


    int main()
    {
        int aa[] = { 1, 5, 7, 6, 2, 3 };
        Solution sou;
        sou.bubbleSort(aa, 6);
        return 1;
    }

    天天向上
  • 相关阅读:
    正则中[A-z]与[A-Za-z]的区别
    .Net Core 缓存方式(二)DistributedSqlServerCache实现(2)
    .Net Core 缓存方式(二)分布式缓存及MemoryDistributedCache 实现(1)
    anaconda安装后spyder打不开的解决方法
    Pandas
    CrawlSpider、分布式、增量式
    Scrapy之数据解析与数据持久化存储
    封装axios库
    vue全国省市选择vue组件
    html+jq实现全国省的单选,弹框输入input
  • 原文地址:https://www.cnblogs.com/hg07/p/12733683.html
Copyright © 2011-2022 走看看