算法搬运之归并排序
【摘要】
原文连接:http://www.cnblogs.com/Braveliu/archive/2013/01/14/2860456.html
#include<iostream>
using n...
原文连接:http://www.cnblogs.com/Braveliu/archive/2013/01/14/2860456.html
#include<iostream>
using namespace std;
//将有序数组ar[]和br[]合并到cr[]中
void MemeryArray(int a[], int n, int b[], int m, int c[])
{
int i, j, k;
i = j = k = 0;
while (i < n && j < m)
{
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < n)
c[k++] = a[i++];
while (j < m)
c[k++] = b[j++];
}
void PrintArr(int ar[],int n)
{
for(int i = 0; i < n; ++i)
cout<<ar[i]<<" ";
cout<<endl;
}
void main()
{
int ar[5] = {12, 23, 34, 45, 56};
int br[5] = {13, 24, 35, 46, 60};
int cr[10];
cout<<"数组ar为:"<<endl;
PrintArr(ar, 5);
cout<<"数组br为:"<<endl;
PrintArr(ar, 5);
MemeryArray(ar, 5, br, 5, cr);
cout<<"合并后结果为:"<<endl;
PrintArr(cr, 10);
}
/*
数组ar为:
12 23 34 45 56
数组br为:
12 23 34 45 56
合并后结果为:
12 13 23 24 34 35 45 46 56 60
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
#include<iostream>
using namespace std;
#define MAXSIZE 10
//将两个有序数列a[first...mid] 和 a[mid...last] 合并。
void mergearray(int a[], int first, int mid, int last, int temp[])
{
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= m)
temp[k++] = a[i++];
while (j <= n)
temp[k++] = a[j++];
for (i = 0; i < k; ++i)
a[first + i] = temp[i];
}
void mergesort(int a[], int first, int last, int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); //左边有序
mergesort(a, mid + 1, last, temp); //右边有序
mergearray(a, first, mid, last, temp); //再将两个有序数列合并
}
}
bool MergeSort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return false;
mergesort(a, 0, n - 1, p);
delete[] p;
return true;
}
void PrintArr(int ar[],int n)
{
for(int i = 0; i < n; ++i)
cout<<ar[i]<<" ";
cout<<endl;
}
void main()
{
int ar[MAXSIZE] = {23, 34, 45, 78, 90, 12, 49, 92, 32, 19};
PrintArr(ar, MAXSIZE);
bool bValue = MergeSort(ar, MAXSIZE);
if(!bValue)
{
cout<<"MergeSort Failed!! "<<endl;
}
PrintArr(ar, MAXSIZE);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
#include<iostream>
#include<malloc.h>
using namespace std;
#define MAXSIZE 10
void PrintArr(int ar[],int n)
{
for(int i = 0; i < n; ++i)
cout<<ar[i]<<" ";
cout<<endl;
}
static void merge(int ar[], int low, int mid, int high)
{
int i, k = 0;
//申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
int *temp = (int *)malloc((high - low + 1)*sizeof(int));
int begin1 = low;
int end1 = mid;
int begin2 = mid + 1;
int end2 = high;
//比较两个元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
for (k = 0; begin1 <= end1 && begin2 <= end2;)
{
if(ar[begin1] < ar[begin2])
temp[k++] = ar[begin1++];
else
temp[k++] = ar[begin2++];
}
while(begin1 <= end1) //若第一个序列有剩余,直接拷贝出来粘到合并序列尾
temp[k++] = ar[begin1++];
while(begin2 <= end2) //若第二个序列有剩余,直接拷贝出来粘到合并序列尾
temp[k++] = ar[begin2++];
for (i = 0;i < k; i++) //将排序好的序列拷贝回数组中
{
ar[low+i] = temp[i];
}
free(temp);
}
void merge_sort(int ar[],int begin,int end)
{
int mid = 0;
if(begin < end)
{
mid = (begin + end) / 2;
merge_sort(ar, begin, mid);
merge_sort(ar, mid + 1, end);
merge(ar, begin, mid, end);
}
}
void main()
{
int ar[] = {12, 14, 54, 5, 6, 3, 9, 8, 47, 89};
merge_sort(ar, 0, MAXSIZE-1);
PrintArr(ar, MAXSIZE);
}
/*
*3 5 6 8 9 12 14 47 54 89
*/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
文章来源: dreamlife.blog.csdn.net,作者:DreamLife.,版权归原作者所有,如需转载,请联系作者。
原文链接:dreamlife.blog.csdn.net/article/details/50969791
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)