各类排序算法(已包装好)


以下是各类排序算法(本人在VS2008下已经验证),还有一些不足(例如模板化)
思想比代码本身重要.....呵呵
sort.h   
#ifndef SORT_H
#define SORT_H

const int maxNum = 100;


class Diff_Sort
{
public:
Diff_Sort();
    virtual void Sort(int *array,int n)=0;
static void Swap(int &x, int &y);

};



class Bubble_Sort : public Diff_Sort
{
public:
Bubble_Sort(){};
void Sort(int *array, int n);
};


class StraigheInsert_Sort : public Diff_Sort
{
public:
StraigheInsert_Sort(){};
    void Sort(int *array, int n);
};

class StraigheSelect_Sort : public Diff_Sort
{
public:
StraigheSelect_Sort(){};
    void Sort(int *array, int n);
};


class Quick_Sort : public Diff_Sort
{
public:
Quick_Sort(){};
void Sort(int *array, int n);
void sort_Index(int *array,int leftIndex,int rightIndex);
};


class Merge_Sort : public Diff_Sort
{
public:
Merge_Sort(){}
void Sort(int *array, int n);
    void Merge(int  *array, int *arrayTemp,int i, int m,int n);
void MSort(int *array, int *arrayTemp, int s, int t);
};

class Binary_Sort : public Diff_Sort
{
public:
Binary_Sort(){};
void Sort(int *array, int n);
};


class Heap_Sort : public Diff_Sort
{
public:
Heap_Sort(){};
void Sort(int *array,int n);
void Adjust_Heap(int* array,int i, int n);
};

class Shell_Sort : public Diff_Sort
{
public:
Shell_Sort(){};
void Sort(int *array,int n);
};

#endif


sort.cpp
#include "stdafx.h"
#include "Sort.h"
#include <iostream>
using namespace std;

Diff_Sort::Diff_Sort()
{
}



void Diff_Sort::Swap(int &x, int &y)
{
const int Temp = x;
x = y;
y = Temp;
}



void Bubble_Sort::Sort(int *array, int n)  //冒泡排序
{
for(int i=1; i<n; ++i)
for( int j=0; j<n-i; ++j)
if(array[j] > array[j+1])
Swap(array[j],array[j+1]);
}

void StraigheInsert_Sort::Sort(int *array, int n)  //直接插入排序
{
for(int i=1; i<n; ++i)
for( int j=i;j>0;--j)  //j表示当前值
if(array[j-1] > array[j])   //如果前面一个大于后卖弄一个就进行交换
Swap(array[j-1],array[j]);//swap表示交换函数
}


void StraigheSelect_Sort::Sort(int *array, int n)  //直接选择排序
{
for(int i=1; i<n; ++i)
for( int j=0; j<n-i; ++j)
if(array[j] > array[j+1])
Swap(array[j],array[j+1]);
}


void Quick_Sort::sort_Index(int *array,int leftIndex,int rightIndex)  //快速排序
{
if(leftIndex < rightIndex)
{
int pos = array[leftIndex];
int lowIndex = leftIndex;
int highIndex = rightIndex;
while(lowIndex < highIndex)
{
while(lowIndex < highIndex && array[highIndex] > pos)
{
highIndex--;
}
array[lowIndex] = array[highIndex];

while(lowIndex < highIndex && array[lowIndex] < pos)
{
lowIndex++;
}
array[highIndex] = array[lowIndex];
}
array[lowIndex] = pos;
sort_Index(array,leftIndex,lowIndex-1); //对左半部分进行递归调用
sort_Index(array,lowIndex+1,rightIndex);//对右半部分进行递归调用
}
}

void Quick_Sort::Sort(int *array, int n)
{
int leftIndex = 0;
int rightIndex = n-1;
sort_Index(array,leftIndex,rightIndex);
}

void Merge_Sort::MSort(int *array, int *arrayTemp, int s, int t)  //合并排序
{
if(s == t)   arrayTemp[s] = array[s];
else
{
int m = (s + t)/2;
MSort(array,arrayTemp,s,m);
MSort(array,arrayTemp,m+1,t);
Merge(array,arrayTemp,s,m,t);
}

}

void Merge_Sort::Merge(int *array, int *arrayTemp,int i, int m,int n)
{
int j,k;
int first = i;
for(j=m+1,k=i; i<m && j<=n; ++k)
{
if(array[i]<array[j])
arrayTemp[k] = array[i++];
else
arrayTemp[k] = array[j++];
}

while(i<=m)
{
arrayTemp[k++] = array[i++];
}

while(j<=n)
{
arrayTemp[k++] = array[j++];
}

for(int w=0; w<k; w++)
array[w] = arrayTemp[w];
}

void Merge_Sort::Sort(int *array, int n)
{
int *p = new int[n];
MSort(array,p,0,n-1);
delete []p;
}


void Binary_Sort::Sort(int *array, int n)  //二分法排序
{
for( int index=1; index<n; ++index)
{
int const& temp = array[index];
int leftIndex = 0;
   int rightIndex = index;

while(leftIndex < rightIndex)
{
int const middleIndex = (leftIndex + rightIndex) / 2;
if(temp > array[middleIndex])
leftIndex = middleIndex +1;     //也是循环退出的条件
else
rightIndex = middleIndex;
}

for(int j=index; j>leftIndex; --j)
Swap(array[j-1],array[j]);

}
}


void Heap_Sort::Adjust_Heap(int* array,int i, int n)  //堆排序
{
int lChild, rChild, mTemp;    
while(true)
{      
rChild = (i+1) * 2;  //左边节点    
lChild = rChild - 1;  //右边节点

mTemp = lChild < n && array[lChild] > array[i] ? lChild : i; 
mTemp = rChild < n && array[rChild] > array[mTemp] ? rChild : mTemp; 

if(i != mTemp)
{
Swap(array[i], array[mTemp]);
i = mTemp; 
}
else
break; 
}
}

void Heap_Sort::Sort(int *array,int n)
{
int i; 
for(i = (n-1)/2; i >= 0; --i) 
Adjust_Heap(array, i, n);   //进行堆的调整


for(i = n-1; i > 0; --i) 
{
Swap(array[0], array[i]);   //与第一个元素进行交换 
Adjust_Heap(array, 0, --n);   //重新进行堆的调整
}
}


void Shell_Sort::Sort(int *array,int n)  //希尔排序
{
    int i,j,inc;
int val;  //T
inc = 0;
do
{
inc++;
inc *= 3;
} while(inc <= n);

do
{
inc /= 3;
for(i=inc; i<n; ++i)
{
val = array[i];
j = i;
while(array[j - inc] > val)
{
array[j] = array[j-inc];
j -= inc;
if(j <= inc)  break;
}
array[j] = val;
}
}while(inc > 0);
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值