关闭

排序算法

标签: 算法nullclassdeleteshell
839人阅读 评论(0) 收藏 举报
分类:
冒泡法:
#include <iostream.h>
void BubbleSort(int* pData,int Count)
{
    int iTemp;
    for(int i=1;i<Count;i++)
    {
        for(int j=Count-1;j>=i;j--)
        {
            if(pData[j]<pData[j-1])
            {
                iTemp = pData[j-1];
                pData[j-1] = pData[j];
                pData[j] = iTemp;
            }
        }
    }
}

交换法:
#include <iostream.h>
void ExchangeSort(int* pData,int Count)
{
    int iTemp;
    for(int i=0;i<Count-1;i++)
    {
        for(int j=i+1;j<Count;j++)
        {
            if(pData[j]<pData[i])
            {
                iTemp = pData[i];
                pData[i] = pData[j];
                pData[j] = iTemp;
            }
        }
    }
}
选择法:
#include <iostream.h>
void SelectSort(int* pData,int Count)
{
    int iTemp;
    int iPos;
    for(int i=0;i<Count-1;i++)
    {
        iTemp = pData[i];
        iPos = i;
        for(int j=i+1;j<Count;j++)
        {
            if(pData[j]<iTemp)
            {
                iTemp = pData[j];
                iPos = j;
            }
        }
        pData[iPos] = pData[i];
        pData[i] = iTemp;
    }
}

插入法:
#include <iostream.h>
void InsertSort(int* pData,int Count)
{
    int iTemp;
    int iPos;
    for(int i=1;i<Count;i++)
    {
        iTemp = pData[i];
        iPos = i-1;
        while((iPos>=0) && (iTemp<pData[iPos]))
        {
            pData[iPos+1] = pData[iPos];
            iPos--;
        }
        pData[iPos+1] = iTemp;
    }
}
快速排序:
#include <iostream.h>
void run(int* pData,int left,int right)
{
    int i,j;
    int middle,iTemp;
    i = left;
    j = right;
    middle = pData[(left+right)/2];  //求中间值
    do{
        while((pData[i]<middle) && (i<right))//从左扫描大于中值的数
            i++;          
        while((pData[j]>middle) && (j>left))//从右扫描大于中值的数
            j--;
        if(i<=j)//找到了一对值
        {
            //交换
            iTemp = pData[i];
            pData[i] = pData[j];
            pData[j] = iTemp;
            i++;
            j--;
        }
    }while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)

    //当左边部分有值(left<j),递归左半边
    if(left<j)
        run(pData,left,j);
    //当右边部分有值(right>i),递归右半边
    if(right>i)
        run(pData,i,right);
}

void QuickSort(int* pData,int Count)
{
    run(pData,0,Count-1);
}

双向冒泡:
#include <iostream.h>
void Bubble2Sort(int* pData,int Count)
{
    int iTemp;
    int left = 1;
    int right =Count -1;
    int t;
    do
    {
        //正向的部分
        for(int i=right;i>=left;i--)
        {
            if(pData[i]<pData[i-1])
            {
                iTemp = pData[i];
                pData[i] = pData[i-1];
                pData[i-1] = iTemp;
                t = i;
            }
        }
        left = t+1;

        //反向的部分
        for(i=left;i<right+1;i++)
        {
            if(pData[i]<pData[i-1])
            {
                iTemp = pData[i];
                pData[i] = pData[i-1];
                pData[i-1] = iTemp;
                t = i;
            }
        }
        right = t-1;
    }while(left<=right);
}

SHELL排序
#include <iostream.h>
void ShellSort(int* pData,int Count)
{
    int step[4];
    step[0] = 9;
    step[1] = 5;
    step[2] = 3;
    step[3] = 1;

    int iTemp;
    int k,s,w;
    for(int i=0;i<4;i++)
    {
        k = step[i];
        s = -k;
        for(int j=k;j<Count;j++)
        {
            iTemp = pData[j];
            w = j-k;//求上step个元素的下标
            if(s ==0)
            {
                s = -k;
                s++;
                pData[s] = iTemp;
            }
            while((iTemp<pData[w]) && (w>=0) && (w<=Count))
            {
                pData[w+k] = pData[w];
                w = w-k;
            }
            pData[w+k] = iTemp;
        }
    }
}

基于模板的通用排序:
MyData.h文件
///////////////////////////////////////////////////////
class CMyData  
{
public:
    CMyData(int Index,char* strData);
    CMyData();
    virtual ~CMyData();

    int m_iIndex;
    int GetDataSize(){ return m_iDataSize; };
    const char* GetData(){ return m_strDatamember; };
    //这里重载了操作符:
    CMyData& operator =(CMyData &SrcData);
    bool operator <(CMyData& data );
    bool operator >(CMyData& data );

private:
    char* m_strDatamember;
    int m_iDataSize;
};
////////////////////////////////////////////////////////

MyData.cpp文件
////////////////////////////////////////////////////////
CMyData::CMyData():
m_iIndex(0),
m_iDataSize(0),
m_strDatamember(NULL)
{
}

CMyData::~CMyData()
{
    if(m_strDatamember != NULL)
        delete[] m_strDatamember;
    m_strDatamember = NULL;
}

CMyData::CMyData(int Index,char* strData):
m_iIndex(Index),
m_iDataSize(0),
m_strDatamember(NULL)
{
    m_iDataSize = strlen(strData);
    m_strDatamember = new char[m_iDataSize+1];
    strcpy(m_strDatamember,strData);
}

CMyData& CMyData::operator =(CMyData &SrcData)
{
    m_iIndex = SrcData.m_iIndex;
    m_iDataSize = SrcData.GetDataSize();
    m_strDatamember = new char[m_iDataSize+1];
    strcpy(m_strDatamember,SrcData.GetData());
    return *this;
}

bool CMyData::operator <(CMyData& data )
{
    return m_iIndex<data.m_iIndex;
}

bool CMyData::operator >(CMyData& data )
{
    return m_iIndex>data.m_iIndex;
}
///////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
//主程序部分
#include <iostream.h>
#include "MyData.h"

template <class T>
void run(T* pData,int left,int right)
{
    int i,j;
    T middle,iTemp;
    i = left;
    j = right;
    //下面的比较都调用我们重载的操作符函数
    middle = pData[(left+right)/2];  //求中间值
    do{
        while((pData[i]<middle) && (i<right))//从左扫描大于中值的数
            i++;          
        while((pData[j]>middle) && (j>left))//从右扫描大于中值的数
            j--;
        if(i<=j)//找到了一对值
        {
            //交换
            iTemp = pData[i];
            pData[i] = pData[j];
            pData[j] = iTemp;
            i++;
            j--;
        }
    }while(i<=j);//如果两边扫描的下标交错,就停止(完成一次)

    //当左边部分有值(left<j),递归左半边
    if(left<j)
        run(pData,left,j);
    //当右边部分有值(right>i),递归右半边
    if(right>i)
        run(pData,i,right);
}

template <class T>
void QuickSort(T* pData,int Count)
{
    run(pData,0,Count-1);
}
双向冒泡:
#include <iostream.h>
void Bubble2Sort(int* pData,int Count)
{
    int iTemp;
    int left = 1;
    int right =Count -1;
    int t;
    do
    {
        //正向的部分
        for(int i=right;i>=left;i--)
        {
            if(pData[i]<pData[i-1])
            {
                iTemp = pData[i];
                pData[i] = pData[i-1];
                pData[i-1] = iTemp;
                t = i;
            }
        }
        left = t+1;

        //反向的部分
        for(i=left;i<right+1;i++)
        {
            if(pData[i]<pData[i-1])
            {
                iTemp = pData[i];
                pData[i] = pData[i-1];
                pData[i-1] = iTemp;
                t = i;
            }
        }
        right = t-1;
    }while(left<=right);
}

//双向交换排序(双向冒泡排序)
void Sorting::Bubble2Sort (int n,Datatype a[])
{
Datatype temp;
int flag=1;
int left,right;
int Right=n-1;
int i;
for(left=0,right=n-1;left<right && flag==1;left++,right--)
{
   flag=0;
for(i=left;i<right;i++)
{
   if(a[i]>a[i+1])  //小的往上浮
   { 
      temp=a[i]; 
      a[i]=a[i+1]; 
      a[i+1]=temp; 
     flag=1; 
   }     
        if(a[Right-i]<a[Right-i-1])   //大的往下沉
        { 
temp=a[Right-i-1]; 
a[Right-i-1]=a[Right-i]; 
a[Right-i]=temp; 
flag=1;
        }    
     }
  }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:58828次
    • 积分:835
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:14篇
    • 译文:0篇
    • 评论:7条
    最新评论