zoukankan      html  css  js  c++  java
  • shell 排序

    // shellSort.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    /*
        算法思想:
        1,初始化增量大小,increment = 0;
        2,以增量大小变化为循环 依次减小增量 直到增量为1 停止循环
        3,在循环体内 设置增量变化公式,从start+增量的位置开始  依次比较大小 若前面的大于后面的,
           则:①首先把当前位置保存下来,然后用前面相隔相同增量的较小的覆盖,直到完毕后,在把最前面的赋值为保存的值。
    */
    void ShellSort(int* a,int len)
    {
        int increment = len;
        int i,j;
        int temp;
        do
        {
            increment = increment/3+1;
            for(i = increment; i<len; ++i)
            {
                if(a[i] < a[i-increment])
                {
                    temp = a[i];
                    for(j =i-increment; j >= 0 && a[j] >= temp; j = j - increment)
                    {
                        a[j+increment] = a[j];
                    }
                    a[j+increment] = temp;
                }
            
            }
    
        }
        while(increment > 1);
    }
    void OutPut(int *a, int len)
    {
        for(int i = 0; i <len; ++i)
        {
          cout<<a[i]<<" ";
        }
        cout<<endl;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
        int a[] = {4,5,6,7,3,2,1,5,8,9};
        ShellSort(a,10);
        OutPut(a,10);
        return 0;
    }
  • 相关阅读:
    django上传下载大文件
    ssh隧道技术
    防止网站被抓
    lvm在线扩容
    Python之配置文件模块 ConfigParser
    Oracle常用查询
    Oracle_where子句
    Oracle_单行函数
    Oracle_多行函数
    Oracle_SQL92_连接查询
  • 原文地址:https://www.cnblogs.com/TianMG/p/2682170.html
Copyright © 2011-2022 走看看