| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 548 人关注过本帖
标题:求帮助,以下是我学习的内排序的一些算法,求大神帮我解决有注释的
只看楼主 加入收藏
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
结帖率:75%
收藏
已结贴  问题点数:10 回复次数:6 
求帮助,以下是我学习的内排序的一些算法,求大神帮我解决有注释的
#include <iostream>
using namespace std;
template<class Type>
void Swap(Type *array,Type a,Type b)
{    Type temp;
    temp=array[a];
    array[a]=array[b];
    array[b]=temp;
}
template<class Type>
void FastSort(Type *array,Type n)
{    for(int i=0;i<n;i++)
    {    for(int j=i;j>0;j--)
        {    if(array[j]<array[j-1])
            Swap(array,j,j-1);
        }
        //else break;//求解这句话为啥是错的
    }

}
template<class Type>
void ImprovedFastSort(Type *array,Type n)
{    Type temprecord;
    for(int i=1;i<n;i++)
    {temprecord=array[i];
        int j=i-1;
        while((j>=0)&&(temprecord<array[j]))
        {array[j+1]=array[j];
            j=j-1;
        }
        array[j+1]=temprecord;
    }
}
template<class Type>
void Display(Type *array,Type n)
{cout<<"The array is :"<<endl;
    for(int i=0;i<n;i++)
        cout<<" "<<array[i];
    cout<<endl;
}
template<class Type>
void BinaryIsertSort(Type *array,Type n)
{    Type temprecord;
    int left,right,middle;
    for(int i=1;i<n;i++)
    {temprecord=array[i];
        left=0;
        right=i-1;
        while(left<=right)
        {middle=(left+right)/2;
        if(temprecord<array[middle])
            right=middle-1;
        else left=middle+1;
        }
   
    for(int j=i-1;j>=left;j--)
        array[j+1]=array[j];
    array[left]=temprecord;
    }
}
template<class Type>
void BubbleSorter(Type *array,Type n)
{    for(int i=1;i<n;i++)
        for(int j=n-1;j>=i;j--)
            if(array[j]<array[j-1])
                Swap(array,j,j-1);
}
template<class Type>
void ImprovedBubbleSorter(Type *array,Type n)
{    bool noswap;
    for(int i=1;i<n;i++)
    {    noswap=true;
        for(int j=n-1;j>=i;j--)
            if(array[j]<array[j-1])
            {Swap(array,j,j-1);
            noswap=false;
            }
            if(noswap)
                return;
    }
}

template<class Type>
void ShellSort(Type *array,Type n)
{    //ModifideIsertSort(Type ,Type);//为啥这里函数的声明是错的
    for(int delta=n/2;delta>0;delta/=2)
        for(int j=0;j<delta;j++)
            //ModifiedInsertSort(array,n);//以下是调用的函数,不能调用
        {for(int i=delta;i<n;i+=delta)//这是函数体的内容,就是下面的一个函数
                for(int j=i;j>=delta;j-=delta)
            {if(array[j]<array[j-delta])
            Swap(array,j,j-delta);
               
            }
        }
}
template<class Type>
void ModifideIsertSort(Type *array,Type n)
{    for(int i=delta;i<n;i+=delta)
        for(int j=i;j>=delta;j-=delta)
        {if(array[j]<array[j-delta])
            Swap(array,j,j-delta);
        }
}
template<class Type>
void Merge(Type *array,Type *temprecord,Type left,Type right,Type middle)
{    for(int j=left;j<=right;j++)
        temprecord[j]=array[j];
    int index1=left;
    int index2=middle+1;
    int i=left;
    while((index1<=middle)&&(index2<=right))
    {if(temprecord[index1]<=temprecord[index2])
        array[i++]=temprecord[index1++];
        else    array[i++]=temprecord[index2++];
    }
    while(index1<=middle)
        array[i++]=temprecord[index1++];
    while(index2<=right)
    array[i++]=temprecord[index2++];
}
template<class Type>
void TwoWayMergeSort(Type *array,Type *temprecord,Type left,Type right)
{    if(left<right)
    {Type middle=(left+right)/2;
        TwoWayMergeSort(array,temprecord,left,middle);
        TwoWayMergeSort(array,temprecord,middle+1,right);
                Merge(array,temprecord,left,right,middle);
    }
}
template<class Type>
Type SelectPivot(Type left,Type right)
{    return (left+right)/2;
}
template<class Type>
Type Partition(Type *array,Type left,Type right)
{    Type temp;
    int i=left;
    int j=right;
    temp=array[j];
    while(i!=j)
    {while((array[i]<temp)&&(j>i))
        i++;
        if(i<j)
        {array[j]=array[i];
            j--;
        }
        while((array[j]>temp)&&(j>i))
            j--;
        if(i<j)
        {array[i]=array[j];
        i++;
        }
    }
    array[i]=temp;
    return i;
}
template<class Type>
void QuickSort(Type *array,Type left,Type right)
{    if(left<=right)
    return ;
    int pivot=SelectPivot(left,right);
    Swap(array,pivot,right);
    pivot=Partition(array,left,right);
    QuickSort(array,left,pivot-1);
    QuickSort(array,pivot+1,right);

}
int main()
{    int *a,i,n,*b;
    cin>>n;
    a=new int [n];
    b=new int [n];
    for(i=0;i<n;i++)
        cin>>a[i];
    Swap(a,3,2);
    Display(a,n);
    FastSort(a,n);
    Display(a,n);
    ImprovedFastSort(a,n);
    Display(a,n);
    BinaryIsertSort(a, n);
    Display(a,n);
    BubbleSorter(a,n);
    Display(a,n);
    ImprovedBubbleSorter(a,n);
    Display(a,n);
    ShellSort(a,n);
    Display(a,n);
    TwoWayMergeSort(a,b,0,n-1);
    Display(a,n);
    QuickSort(a,0,n-1);
    Display(a,n);
    return 0;
}
搜索更多相关主题的帖子: void include 
2012-12-22 12:08
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:5 
程序代码:
void FastSort(Type *array,Type n)
{    for(int i=0;i<n;i++)
    {    for(int j=i;j>0;j--)
        {    if(array[j]<array[j-1])
            Swap(array,j,j-1);
        }
        //else break;//求解这句话为啥是错的
    }
}

这是语法问题, else 必须和 if成对使用.
2012-12-22 12:58
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:0 
程序代码:
//ModifideIsertSort(Type ,Type);//为啥这里函数的声明是错的

//应该这样声明
template<class Type>
void ModifideIsertSort(Type ,Type);

2012-12-22 13:00
crystall
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:184
专家分:809
注 册:2012-12-1
收藏
得分:0 
程序代码:
//声明
template<class Type>
void ModifideIsertSort(Type* ,Type);


template<class Type>
void ShellSort(Type *array,Type n)
{   
    //ModifideIsertSort(Type ,Type);//为啥这里函数的声明是错的
    for(int delta=n/2;delta>0;delta/=2)
        for(int j=0;j<delta;j++)
            ModifideIsertSort(array,n); //可以调用
        /*
        ModifideIsertSort(array,n);//以下是调用的函数,不能调用
        {for(int i=delta;i<n;i+=delta)//这是函数体的内容,就是下面的一个函数
                for(int j=i;j>=delta;j-=delta)
            {if(array[j]<array[j-delta])
            Swap(array,j,j-delta);
               
            }
        }*/
}
template<class Type>
void ModifideIsertSort(Type *array,Type n)
{  
    //delta 未定义
    for(int i=delta;i<n;i+=delta)
        for(int j=i;j>=delta;j-=delta)
        {if(array[j]<array[j-delta])
            Swap(array,j,j-delta);
        }
}
2012-12-22 13:04
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
谢谢啦。你说的我明白,不过就是弄不对啊,谢谢你能帮我全弄好不嘛,就上面的问题,就那两个函数,调用的时候老说没有定义。

同学习......同进步....你帮我......我帮你.....上善若水.....
2012-12-22 15:53
cyy998
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2012-12-22
收藏
得分:5 
回复 楼主 不玩虚的
if(array[j]<array[j-1])
             Swap(array,j,j-1);
         }
         //else break;//求解这句话为啥是错的

我看到少了个大括号
程序代码:
if(array[j]<array[j-1])
         {
             Swap(array,j,j-1);
         }
         else break;//求解这句话为啥是错的

应该是这样吧
2012-12-22 16:11
不玩虚的
Rank: 9Rank: 9Rank: 9
来 自:四川
等 级:贵宾
威 望:10
帖 子:331
专家分:1301
注 册:2012-12-9
收藏
得分:0 
这个回答好玩

同学习......同进步....你帮我......我帮你.....上善若水.....
2012-12-22 16:16
快速回复:求帮助,以下是我学习的内排序的一些算法,求大神帮我解决有注释的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019941 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved