归并排序
来源:互联网 发布:淘宝网夏秋棉麻连衣裙 编辑:程序博客网 时间:2024/06/14 07:42
/*归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。归并排序是一种稳定的排序方法。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。*/#include<iostream>#include<stdio.h>using namespace std;///将有二个有序数列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); ///再将二个有序数列合并 }}int main(){ int a[10],temp[10]; for(int i = 0; i < 10; i ++) scanf("%d",&a[i]); mergesort(a,0,9,temp); for(int i = 0; i < 10; i ++) cout << temp[i] << " "; cout << endl; return 0;}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 关于gcc中string的sizeof
- 希尔排序
- 第十。十一周项目3 - 警察和厨师——1
- java中获取当前日期和前一天日期
- 转:java中时间戳各种实现方式获取效率
- 归并排序
- CountDownTimer 解决 Cancel无效的问题
- 【Spring4揭秘 BeanFactory】修改Bean---BeanPostProcessor
- LeetCode-168.Excel Sheet Column Title
- 怎样在Eclipse中快速查看各种源代码?
- 鼠标双击
- 基数排序
- 月薪3万的程序员告诉你:这样工作才能拿高薪
- Android有效地减少方法数