归并排序 空间复杂度为O(1)的做法
#include <iostream>
#include <cstdlib>
using namespace std;
void print(int *arr, int start, int end)
{
for (int i = start; i <= end; ++i)
cout << arr[i] << ' ';
cout << endl;
}
void randData(int *arr, int start, int end)
{
for (int i = start; i <= end; ++i)
arr[i] = rand() % 20;
print(arr, start, end);
} void merge(int *arr, int start, int mid, int end)
{
int i, j, k, key;
i = start;
j = mid;
while (i < j && j <= end) //当i等于j或者j到达末尾时终止
{
if (arr[i] > arr[j])
{
k = j;
key = arr[j];
while (k > i && arr[k - 1] > key)
{
arr[k] = arr[k - 1];
--k; }
arr[k] = key;
++j;
}
++i;
}
}
void mergeSort(int *arr, int start, int end)
{
if(start < end)
{
int mid = (end + start) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid + 1,end);
print(arr, start, end);
}
}
/*11 4 2 13 12 2 1 16 18 15*/
int main()
{
bool bIsContinue = true;
char ch = 'n';
const int Len = 10;
int arr[Len];
print(arr, 0, Len - 1); while (true == bIsContinue)
{
randData(arr, 0, Len - 1);
mergeSort(arr, 0, Len - 1);
cout << "the new array: ";
print(arr, 0, Len - 1);
cout << "please input yes or no" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
bIsContinue = true;
else
bIsContinue = false;
}
return 0;
}
归并排序 空间复杂度为O(1)的做法的更多相关文章
- java方式实现归并排序
一.基本思想 归并排序是建立在归并操作上的一种排序算法,该算法是采用分治法的一个典型应用.具体操作如下:所谓的分治就是分而治之,以一分为二的原则,先把序列平均分解成二个左右子序列,然后递归左右二个子序 ...
- [LeetCode]148. Sort List链表归并排序
要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- cojs 简单的求和问题 解题报告
一个上午写了两个数据生成器,三个暴力和两个正解以及一个未竣工的伪正解思路 真是累死本宝宝了 首先这个题目暴力我的数据是有很多良心分的 但是不同的暴力拿到的分数也会有所差距,由于是题解就不说暴力怎么写了 ...
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
- [LeetCode 题解]: First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Leetcode 448.找到所有数组中消失的数字
找到所有数组中消失的数字 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现 ...
- 2017国家集训队作业[agc006f]Blackout
2017国家集训队作业[agc006f]Blackout 题意: 有一个\(N*N\)的网格,一开始有\(M\)个格子被涂黑,给出这\(M\)个格子,和染色操作:如果有坐标为\((x,y),(y,z) ...
- 细谈Redis五大数据类型
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 上一篇文章有提到,Redis中使用最频繁的有5种数据类型:String.List.Hash.Set.SortS ...
随机推荐
- 自定义cursor
cursor: url('绝对路径/big.cur'),auto; //通用方式
- 【转】linux /centos 中OpenSSL升级方法详解
相关软件下载地址 Apache:http://httpd.apache.org/ Nginx:http://nginx.org/en/download.html OpenSSL:http://www. ...
- mybatis分页插件PageHelper的使用(转)
Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...
- Lib New
gacutil /if E:\ThirdParty\RockyLib\Rocky\bin\Release\Rocky.dll gacutil /u Rocky partial class GmailF ...
- lightoj1038
//Accepted 2860 KB 16 ms //概率 //对于n,假设n变成1的期望步数为p(n) //则p(n)=1/t*sum((1+p(d))) d|n //解得:p(n)=1/(t-1) ...
- Ubuntu 14.10 下安装java反编译工具 jd-gui
系统环境,Ubuntu 14.10 ,64位 1 下载JD-GUI,网址http://221.3.153.126/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/jd.benow.ca/ ...
- sql连接又一篇
作者:初行 – 博客园 SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表: stu表: ...
- Python 字符串处理大全.
Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...
- OD调试篇5--如何应对OD使用中的一些问题
打开小甲鱼给的进行恶搞过的程序,会发现一些问题 发现程序直接暂停,或者加载进来有问题. 那机智的我 通过对上一个没有恶搞过的exe可执行文件的PE头进行了比较 会发现其中的猫腻 那么我们去正常的修改一 ...
- ERP联系人查询和修改(十六)
查看和修改是同一个界面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="L ...