二路归并排序
来源:互联网 发布:淘宝店铺一个好评几分 编辑:程序博客网 时间:2024/05/22 06:09
//============================================================================
// Name : merge_sort.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
using namespace std;
void generate(int *p, int n) {
srand((int) time(0));
for (int i = 0; i < n; i++) {
*p++ = rand();
}
}
void output(int *p, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", *p++);
}
printf("\n");
}
void merge(int *p, int low, int mid, int high) {
int *tmp = new int[high - low + 1];
int left = low, right = mid + 1, index = 0;
while (left <= mid && right <= high) {
if (p[left] <= p[right]) {
tmp[index++] = p[left++];
} else {
tmp[index++] = p[right++];
}
}
if (left <= mid) {
while (left <= mid) {
tmp[index++] = p[left++];
}
}
if (right <= high) {
while (right <= high) {
tmp[index++] = p[right++];
}
}
for(int i=0;i<index;i++) {
p[i+low] = tmp[i];
}
delete []tmp;
}
void merge_sort(int *p,int low,int high) {
if(low<high) {
int mid=(low+high)>>1;
merge_sort(p,low,mid);
merge_sort(p,mid+1,high);
merge(p,low,mid,high);
}
}
int main() {
int a[15];
generate(a, 15);
output(a, 15);
merge_sort(a,0,14);
output(a, 15);
return 0;
// Name : merge_sort.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
using namespace std;
void generate(int *p, int n) {
srand((int) time(0));
for (int i = 0; i < n; i++) {
*p++ = rand();
}
}
void output(int *p, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", *p++);
}
printf("\n");
}
void merge(int *p, int low, int mid, int high) {
int *tmp = new int[high - low + 1];
int left = low, right = mid + 1, index = 0;
while (left <= mid && right <= high) {
if (p[left] <= p[right]) {
tmp[index++] = p[left++];
} else {
tmp[index++] = p[right++];
}
}
if (left <= mid) {
while (left <= mid) {
tmp[index++] = p[left++];
}
}
if (right <= high) {
while (right <= high) {
tmp[index++] = p[right++];
}
}
for(int i=0;i<index;i++) {
p[i+low] = tmp[i];
}
delete []tmp;
}
void merge_sort(int *p,int low,int high) {
if(low<high) {
int mid=(low+high)>>1;
merge_sort(p,low,mid);
merge_sort(p,mid+1,high);
merge(p,low,mid,high);
}
}
int main() {
int a[15];
generate(a, 15);
output(a, 15);
merge_sort(a,0,14);
output(a, 15);
return 0;
}
核心思想是分治和递归
T(n)=o(nlogn)
S(n)=o(n)
- 归并排序:二路归并
- 归并排序--二路归并
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- 二路归并排序
- Windows中双网卡时删除错误的默认路由
- 无法引入com.sun.management.OperatingSystemMXBean .
- C语言中堆和栈的区别
- 黑马程序员-动态代理
- 输入URL按下回车后究竟发生了什么
- 二路归并排序
- __attribute__ ((__packed__))编译选项
- Fiddler 教程
- shell语法
- CSS规范定义
- 怎样才能让Linux的tty1-6显示都是英文,图形界面显示的是中文?
- 【Boost】boost库中thread多线程详解5——谈谈线程中断
- 《一九四二》高清下载
- 让孩子益智的营养物质