merge_sort2 do not use the sentinels (recursive)
2006-02-27 11:48
393 查看
//algo/merge_sort2.cpp
//do not use the sentinels
#include <iostream>
using namespace std;
//#define LOG_OUT
void merge(int list[],int p,int q,int r)
{
//merge the sorted list, list[p,q],list[q+1,r] to one sorted list, list[p,r]
int n1 = q-p+1;
int n2 = r-q;
int i,j;
int *L = new int[n1];
int *R = new int[n2];
if(L==NULL || R==NULL)
cerr<<"memory new error!"<<endl;
//assignment
#ifdef LOG_OUT
cout<<"Array L: "<<endl;
#endif
for(i=0;i<n1;++i){
L[i] = list[p+i];
#ifdef LOG_OUT
cout<<"L["<<i<<"] = "<<L[i]<<" ";
#endif
}
#ifdef LOG_OUT
cout<<endl;
cout<<"Array R: "<<endl;
#endif
for(j=0;j<n2;++j){
R[j] = list[q+1+j];
#ifdef LOG_OUT
cout<<"R["<<j<<"] = "<<R[j]<<" ";
#endif
}
#ifdef LOG_OUT
cout<<endl;
#endif
i=0;j=0;
for(int k=p;k<=r;++k){
if(i==n1){
for(;j<n2;++j){
list[k++] = R[j];
}
break;
}
if(j==n2){
for(;i<n1;++i){
list[k++] = L[i];
}
break;
}
if(L[i]<=R[j]){
list[k] = L[i++];
}
else{
list[k] = R[j++];
}
}
//if not use delete, will memory leak
delete []L;
delete []R;
}
void merge_sort(int list[],int p,int r)
{
int sz= r-p+1;
if(sz>1){
int q = (p+r)/2;
merge_sort(list,p,q);
merge_sort(list,q+1,r);
merge(list,p,q,r);
}
}
int main()
{
int list[] = {7,4,5,8,9,1,3,5,6,8};
//int list[] = {3,41,52,26,38,57,9,49};
for(int i=0;i<10;++i){
cout<<list[i]<<" ";
}
cout<<endl;
merge_sort(list,0,9);
for(int i=0;i<10;++i){
cout<<list[i]<<" ";
}
cout<<endl;
return 0;
}
//do not use the sentinels
#include <iostream>
using namespace std;
//#define LOG_OUT
void merge(int list[],int p,int q,int r)
{
//merge the sorted list, list[p,q],list[q+1,r] to one sorted list, list[p,r]
int n1 = q-p+1;
int n2 = r-q;
int i,j;
int *L = new int[n1];
int *R = new int[n2];
if(L==NULL || R==NULL)
cerr<<"memory new error!"<<endl;
//assignment
#ifdef LOG_OUT
cout<<"Array L: "<<endl;
#endif
for(i=0;i<n1;++i){
L[i] = list[p+i];
#ifdef LOG_OUT
cout<<"L["<<i<<"] = "<<L[i]<<" ";
#endif
}
#ifdef LOG_OUT
cout<<endl;
cout<<"Array R: "<<endl;
#endif
for(j=0;j<n2;++j){
R[j] = list[q+1+j];
#ifdef LOG_OUT
cout<<"R["<<j<<"] = "<<R[j]<<" ";
#endif
}
#ifdef LOG_OUT
cout<<endl;
#endif
i=0;j=0;
for(int k=p;k<=r;++k){
if(i==n1){
for(;j<n2;++j){
list[k++] = R[j];
}
break;
}
if(j==n2){
for(;i<n1;++i){
list[k++] = L[i];
}
break;
}
if(L[i]<=R[j]){
list[k] = L[i++];
}
else{
list[k] = R[j++];
}
}
//if not use delete, will memory leak
delete []L;
delete []R;
}
void merge_sort(int list[],int p,int r)
{
int sz= r-p+1;
if(sz>1){
int q = (p+r)/2;
merge_sort(list,p,q);
merge_sort(list,q+1,r);
merge(list,p,q,r);
}
}
int main()
{
int list[] = {7,4,5,8,9,1,3,5,6,8};
//int list[] = {3,41,52,26,38,57,9,49};
for(int i=0;i<10;++i){
cout<<list[i]<<" ";
}
cout<<endl;
merge_sort(list,0,9);
for(int i=0;i<10;++i){
cout<<list[i]<<" ";
}
cout<<endl;
return 0;
}
相关文章推荐
- Best Practice 5 Do not use SingleThreadModel
- Use recursive procedure to perform a bubble sort on a list of data
- application使用@符合问题:'@' that cannot start any token. (Do not use @ for indentation)
- Do not use shared variable to check thread status
- merge sort using sentinel (recursive)
- vue Do not use built-in or reserved HTML elements as component id: nav
- use this as the default and do not ask again
- Do not concatenate text displayed with setText. Use resource string with placeho
- “Problem with MergeList” or “status file could not be parsed” error when trying to do an update?
- Do not concatenate text displayed with setText,use resource string with placeholders.
- Kernel: Do NOT use global variable as possible as you can 尽量不使用全局变量
- JBoss 中的 "Do not use InterceptorsFactory with a ManagedObjectAdvisor"处理方案
- merge sort and binary search recursive version
- Do not concatenate text displayed with setText. Use resource string with placeholders. 提示
- Android Make sure other views do not use the same id
- Spring AOP 循环应用(This means that said other beans do not use the final version of the bean..)
- 老男孩教育每日一题-2017年5月7日-加餐-linux下面如何实现,执行rm命令,就显示do not use rm command
- Wrong state class, expecting View State but received class xxx Make sure other views do not use the
- linux下面实现执行rm命令,显示do not use rm command
- vue项目报错'Do not use built-in or reserved HTML elements as component id:'