博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
排序算法一 插入
阅读量:6940 次
发布时间:2019-06-27

本文共 1439 字,大约阅读时间需要 4 分钟。

hot3.png

#include<iostream>

using namespace std;

#define Max_ 10

void InsertSort(int arr[], int nLength, int nDelt = 1);  //for Straight && Shell

void Show(int arr[], int nLength)

{
    for (int i = 0; i < nLength; i ++)
    {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}

void InsertSort(int arr[], int nLength, int nDelt /*= 1*/)

{
    for (int i = nDelt; i < nLength; i++)
    {
        if (arr[i] < arr[i - nDelt])
        {
            int nTemp = arr[i];
            arr[i] = arr[i - nDelt];
            int j = i - nDelt;
            while(nTemp < arr[j])    //if j = -1 , VS is right ?
            {
                arr[j + nDelt] = arr[j];
                j -= nDelt;
            }
            arr[j + nDelt] = nTemp;
        }
    }
}

void ShellSort(int arr[], int nLength)

{
    int nDelt = nLength /2;
    while (nDelt >= 1)
    {
        InsertSort(arr, nLength, nDelt);
        nDelt /= 2;
    }
}

int main()

{
    int arr_test[Max_] = {877, 477, 252, 22233, 5554, 155, 644, 932, 77, 27};
    Show(arr_test, Max_);
    InsertSort(arr_test, Max_);
    Show(arr_test, Max_);

    //

    //int nLength = 0;
    //int nInput = 0;
    //printf("Please input the length of array: ");
    //scanf("%d",&nLength);
    //for (int i = 0; i < nLength; i++)
    //{
    //    printf("Please input element %d : ", i + 1);
    //    fflush(stdin);
    //    scanf("%d", &nInput);
    //    arr_test[i] = nInput;
    //}
    //Show(arr_test, Max_);
    //InsertSort(arr_test, Max_);
    //Show(arr_test, Max_);
    //

    int arr_test1[Max_] = {877, 477, 252, 22233, 5554, 155, 644, 932, 77, 27};

    Show(arr_test1, Max_);
    ShellSort(arr_test1, Max_);
    Show(arr_test1, Max_);

    system("pause");

    return 0;
}

转载于:https://my.oschina.net/ucliaohh/blog/822138

你可能感兴趣的文章
支持向量机(五)SMO算法
查看>>
编译Android源码
查看>>
聚类分析算法---学习
查看>>
通过SQL Server命令行启动及停止SQL服务的方法
查看>>
笔记本电脑不显示电量图标-20180103
查看>>
JVM致命错误日志(hs_err_pid.log)解读
查看>>
springboot~openfeign从此和httpClient说再见
查看>>
CSRF攻击和防护
查看>>
FileUpload控件实例应用 上传文件
查看>>
springboot入门_数据库访问_jdbcTemplate
查看>>
迭代器 Iterator
查看>>
0初识Linux
查看>>
1048 Find Coins
查看>>
弱语法
查看>>
kafka api
查看>>
[WC2019] 数树
查看>>
fedora23的打印服务
查看>>
人类进程(一)
查看>>
常用mysql语句
查看>>
【原创】自动更新程序1--网站的部署(技术:spring.net+三层架构+webservice+IrisSkin2换肤)...
查看>>