C语言实现八种基本排序(二)
2010-08-08 20:26
295 查看
1、merge_sort.cpp: 自顶向下,使用递归
2、merge_sort.c:自底向上,使用迭代
#include <stdio.h> //#include <stdlib.h> using namespace std; void merge(int *a,int *temp,int lPos,int rPos,int rEnd) { int i,lEnd,tempPos,cout; lEnd = rPos - 1; tempPos = lPos; cout = rEnd - lPos + 1; while(lPos <= lEnd && rPos <= rEnd) { if(a[lPos] <= a[rPos]) temp[tempPos++] = a[lPos++]; else temp[tempPos++] = a[rPos++]; } if(lPos <= lEnd) temp[tempPos++] = a[lPos++]; if(rPos <= rEnd) temp[tempPos++] = a[rPos++]; for(i = 0;i < cout;i++,rEnd--) { a[rEnd] = temp[rEnd]; } } void msort(int *a, int *temp, int low, int high) { if(low >= high) return; int mid = (low + high) / 2; msort(a, temp, low, mid); msort(a, temp, mid + 1, high); merge(a, temp, low, mid + 1, high); } void merge_sort(int *a, int len) { int *temp = NULL; temp = new int[len]; //temp = (int *)malloc(sizeof(int) * len); if(temp != NULL) { msort(a, temp, 0, len - 1); //free(temp); delete [] temp; } } void print_array(int *a, int len) { int i; for(i = 0; i < len;i++) { printf("%d ", a[i]); } printf("/n"); } int main() { int a[] = {8,0,3,4,2,1,5,6,9,7}; print_array(a, 10); merge_sort(a, 10); print_array(a, 10); return 0; }
2、merge_sort.c:自底向上,使用迭代
#include <stdio.h> #include <stdlib.h> #include <string.h> void print_array(int *a, int len) { int i; for(i = 0;i < len;i++) { printf("%d ", a[i]); } printf("/n"); } //this is down to up merge method. //arg lPos and rPos are the begin of the two part, //and arg step stand by the cout of each part, //and arg len is the length of the a[]. void merge(int *a, int lPos, int rPos, int step, int len) { int len_right, index, lEnd, rEnd; //the length of right part should be compute. if(rPos + step - 1 >= len - 1) len_right = len - rPos; else len_right = step; //index is used to remember the lPos to copy from temp[]. index = lPos; lEnd = rPos - 1; rEnd = rPos + len_right - 1; int *temp = (int *)malloc(sizeof(int) * (step + len_right)); int i = 0; while(lPos <= lEnd && rPos <= rEnd) { if(a[lPos] <= a[rPos]) temp[i++] = a[lPos++]; else temp[i++] = a[rPos++]; } while(lPos <= lEnd) temp[i++] = a[lPos++]; while(rPos <= rEnd) temp[i++] = a[rPos++]; int j; for(j = 0;j < i;j++) { a[index + j] = temp[j]; } free(temp); } //the step is from 1 to until len. void merge_sort(int *a, int len) { int step = 1; while(step < len) { int i; for(i = 0;i <= (len - step - 1);i += (2 * step)) { merge(a, i, i + step, step, len); } step *= 2; } } int main() { int a[] = {2,4,8,0,1,3,7,5,6,9}; print_array(a, 10); merge_sort(a, 10); print_array(a, 10); return 0; }
相关文章推荐
- C语言实现八种基本排序(四)
- C语言实现八种基本排序(三)
- C语言实现八种基本排序(一)
- C语言实现基本排序算法----排序(直接插入排序,SHELL排序,冒泡排序,快速排序,简单选择排序,堆排序)
- C语言提高-第26/27讲: 实现线性表基本操作的函数/编写查找和排序函数
- C语言基本排序算法之插入排序与直接选择排序实现方法
- 几种基本的排序算法(选择排序,冒泡排序,快速排序,归并排序,希尔排序)C语言实现
- java八种基本排序的实现
- 快速排序--C语言实现
- 排序(6)---------归并排序(C语言实现)
- c语言实现通讯录,包括增加名字,删除信息,查找,修改,排序
- C语言 链表的基本操作实现 源码
- C语言 指针方法实现三个数排序
- 学习笔记——线性表的动态分配顺序存储结构基本操作(C语言实现)
- 顺序存储结构线性表基本操作 纯C语言实现
- 快速排序的C语言实现
- 数据结构--单链表的基本操作(C语言实现)
- 【笔试面试知识点查缺补漏深入理解之C与C++篇】排序的C语言实现-插入-选择-快排
- 快速排序--C语言实现、python实现
- C语言实现栈的基本操作