夯实基础——归并排序
来源:互联网 发布:viso软件流程图步骤 编辑:程序博客网 时间:2024/05/16 23:38
逻辑结构:递归栈
物理结构:数组
归并排序分析:
最优时间复杂度:O(n)
最坏时间复杂度:O(nlog2n)
平均时间复杂度:O(nlog2n)
最差空间复杂度:O(n)
稳定性:稳定
归并排序主要有两个函数:
1 一次归并排序 int MergeArray(int *list1,int list1_size,int *list2,int list2_size);
2 递归归并排序 int MergeSort(int *list,int list_size);
3 非递归归并排序 int MergeSort(int *list,int list_size);
//一次归并排序int MergeArray(int *list1,int list1_size,int *list2,int list2_size){ int i,j,k,a; i=j=k=0; int *list=(int *)malloc(sizeof(int)*(list1_size+list2_size)); while(i<list1_size && j<list2_size) { if(*(list1+i) <= *(list2+j)) { *(list+k) = *(list1+i); i++; k++; } else { *(list+k) = *(list2+j); j++; k++; } } while(i < list1_size) { *(list+k) = *(list1+i); i++; k++; } while(j < list2_size) { *(list+k) = *(list2+j); j++; k++; } for(a=0;a<(list1_size+list2_size);a++) *(list1+a)=*(list+a); free(list);}
//递归归并排序int MergeSort(int *list,int list_size){ if(list_size>1) { int *list1=list; int list1_size=list_size/2; int *list2=list+list_size/2; int list2_size=list_size-list1_size; MergeSort(list1,list1_size); MergeSort(list2,list2_size); MergeArray(list1,list1_size,list2,list2_size); }}
//非递归归并排序int NonMergeSort(int *list,int list_size){ int i,l_min,l_max,r_min,r_max,cursor; int *aux = (int *)malloc(sizeof(int)*list_size); for( i = 1 ;i < list_size; i *= 2) { for(l_min = 0; l_min < list_size - i; l_min = r_max) { r_min = l_max = l_min + i ; r_max = l_max + i; if(r_max > list_size) r_max = list_size; cursor = 0; while(l_min < l_max && r_min < r_max) { if(list[l_min] <= list[r_min]) { aux[cursor++] = list[l_min++]; } else { aux[cursor++] = list[r_min++]; } } while(l_min < l_max) aux[cursor++] = list[l_min++]; while(r_min < r_max) aux[cursor++] = list[r_min++]; while( cursor > 0 ) list[--r_min] = aux [--cursor]; } } free(aux);}
//非递归归并排序int NonMergeSort1(int *list , int length){ int i, left_min, left_max, right_min, right_max, next; int *tmp = (int*)malloc(sizeof(int) * length); if (tmp == NULL) { fputs("Error: out of memory\n", stderr); abort(); } for (i = 1; i < length; i *= 2) { for (left_min = 0; left_min < length - i; left_min = right_max) { right_min = left_max = left_min + i; right_max = left_max + i; if (right_max > length) right_max = length; next = 0; while (left_min < left_max && right_min < right_max) tmp[next++] = list[left_min] > list[right_min] ? list[right_min++] : list[left_min++]; while (left_min < left_max) list[--right_min] = list[--left_max]; while (next > 0) list[--right_min] = tmp[--next]; } } free(tmp);}
0 0
- 夯实基础——归并排序
- 夯实基础——堆排序
- 夯实基础——快速排序
- 夯实基础——插入排序
- 夯实基础——希尔排序
- 夯实基础——选择排序
- 夯实基础——流程控制
- 夯实基础——static关键字
- 基础算法学习笔记—归并排序
- 算法基础3—归并排序
- 排序—归并排序
- 【基础排序】归并排序
- 基础排序:归并排序
- 夯实基础——从键盘输入的两种方式
- 夯实基础——继承、super和final关键字
- 夯实基础
- 夯实基础
- 基础算法系列(五)——归并排序
- reshape函数—— Matlab和opencv的reshape函数 不同点
- aspupload 上传组件下载
- Java基本语言要素
- EntityManager方法简介
- 动态密码锁
- 夯实基础——归并排序
- Spring 表达式语言 (一)
- 如何突破底层思维方式的牢笼
- 计数排序
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- setTimeout原理
- Unity3d的ios项目在场景切换时候Crash的问题
- USB Key
- N点虚拟主机管理系统 企业版稳定亲测