交换,就是根据序列中两个记录键值的比较结果来对换这两个记录在序列中的位置,交换排序的特点是:将键值较大的记录向序列的尾部移动,键值较小的记录向序列的前部移动。
//Factory.h
#ifndef _SORTFACTORY_H_
#define _SORTFACTORY_H_
class SortProduct;
class TreeProduct;
class Factory
{
public:
virtual SortProduct* QuickSort(int *Arr,int Len,char s) = 0;
virtual SortProduct* MergSort(int *Arr, int Len) = 0;
virtual SortProduct* SwapSort(int *Arr, int Len) = 0;
virtual SortProduct* BubbleSort(int *Arr, int Len) = 0;
virtual SortProduct* SelectSort(int *Arr, int Len) = 0;
virtual SortProduct* InsertSort(int *Arr, int Len) = 0;
virtual SortProduct* ShellSort(int *Arr, int Len) = 0;
virtual SortProduct* HeapSort(int *Arr, int Len) = 0;
protected:
Factory();
virtual ~Factory() = 0;
private:
};
class ConcreteFactory:public Factory
{
public:
~ConcreteFactory();
ConcreteFactory(int *Arr, int Len);
static void Print(int *Arr, int Len);
SortProduct* QuickSort(int *Arr, int Len, char s);
SortProduct* MergSort(int *Arr, int Len);
SortProduct* SwapSort(int *Arr, int Len);
SortProduct* BubbleSort(int *Arr, int Len);
SortProduct* SelectSort(int *Arr, int Len);
SortProduct* InsertSort(int *Arr, int Len);
SortProduct* ShellSort(int *Arr, int Len);
SortProduct* HeapSort(int *Arr, int Len);
private:
int *P;
int N;
};
#endif
//SortProduct.h
#ifndef _SORTPRODUCT_H_
#define _SORTPRODUCT_H_
class SortProduct
{
public:
virtual ~SortProduct() =0;
protected:
SortProduct();
private:
};
/*
*快速排序
*/
class QuickSortN:public SortProduct
{
public:
QuickSortN(int *Arr, int Len,char s);
void RecusiveQuickSort(int *Arr, int Len);
void NoRecusiveQuickSort(int *Arr, int Len);
protected:
int Partation(int *Arr, int Low, int Hight);
void RecusiveQuickSortN(int *Arr, int Low, int Hight);
void NoRecusiveQuickSortN(int *Arr, int Low, int Hight);
private:
int *P;
int N;
char S;
};
class MergSortN :public SortProduct
{
public:
MergSortN(int *arr, int start, int end);
void Mymerge(int *arr, int start, int midd, int end);
private:
int *P;
int N;
};
class SwapSortN : public SortProduct
{
public:
void SwapSort(int *Arr, int Len);
SwapSortN(int *Aarr,int Len);
private:
int *P;
int N;
};
class BubbleSortN : public SortProduct
{
public:
void BubbleSort(int *Arr, int Len);
BubbleSortN(int *Aarr, int Len);
private:
int *P;
int N;
};
class SelectSortN : public SortProduct
{
public:
void SelectSort(int *Arr, int Len);
SelectSortN(int *Aarr, int Len);
private:
int *P;
int N;
};
class InsertSortN : public SortProduct
{
public:
void InsertSort(int *Arr, int Len);
InsertSortN(int *Aarr, int Len);
private:
int *P;
int N;
};
class ShellSortN : public SortProduct
{
public:
void ShellSort(int *Arr, int Len);
ShellSortN(int *Aarr, int Len);
private:
int *P;
int N;
};
class HeapSortN : public SortProduct
{
public:
static void HeapSort(int *Arr, int Len);
HeapSortN(int *Aarr, int Len);
private:
int *P;
int N;
};
#endif
//SortFactory.cpp
#include "Factory.h"
#include "SortProduct.h"
#include
using namespace std;
Factory::Factory(){}
Factory::~Factory(){}
void ConcreteFactory::Print(int *Arr, int Len)
{
for (int i = 0; i < Len; ++i)
{
cout << Arr[i] << " ";
}
cout << endl;
}
ConcreteFactory::ConcreteFactory(int *Arr, int Len):P(Arr),N(Len)
{
cout<<"原始 数据:";
Print(Arr, Len);
}
ConcreteFactory::~ConcreteFactory(){}
SortProduct* ConcreteFactory::QuickSort(int *Arr, int Len, char s)
{
return new QuickSortN(Arr,Len,s);
}
SortProduct* ConcreteFactory::MergSort(int *Arr,int Len)
{
return new MergSortN(Arr,0,Len-1);
}
SortProduct* ConcreteFactory::SwapSort(int *Arr, int Len)
{
return new SwapSortN(Arr,Len);
}
SortProduct* ConcreteFactory::BubbleSort(int *Arr, int Len)
{
return new BubbleSortN(Arr, Len);
}
SortProduct* ConcreteFactory::SelectSort(int *Arr, int Len)
{
return new SelectSortN(Arr, Len);
}
SortProduct* ConcreteFactory::InsertSort(int *Arr, int Len)
{
return new InsertSortN(Arr, Len);
}
SortProduct* ConcreteFactory::ShellSort(int *Arr, int Len)
{
return new ShellSortN(Arr, Len);
}
SortProduct* ConcreteFactory::HeapSort(int *Arr, int Len)
{
return new HeapSortN(Arr, Len);
}
//SortProduct.cpp
#include "SortProduct.h"
#include "Factory.h"
#include
#include
#include
using namespace std;
SortProduct::SortProduct(){}
SortProduct::~SortProduct(){}
/*
*递归&&非递归快排
*/
int QuickSortN::Partation(int *Arr, int Low, int Hight)
{
if (Low >= Hight)
{
return 0;
}
int Temp = Arr[Low];
while (Low < Hight)
{
while (Low < Hight && Arr[Hight] >= Temp) { --Hight; }
Arr[Low] = Arr[Hight];
while (Low < Hight && Arr[Low] <= Temp) { ++Low; }
Arr[Hight] = Arr[Low];
}
Arr[Low] = Temp;
return Low;
}
void QuickSortN::RecusiveQuickSortN(int *Arr, int Low, int Hight)
{
if (Low < Hight)
{
int Mid = QuickSortN::Partation(Arr, Low, Hight);
RecusiveQuickSortN(Arr, Low, Mid - 1);
RecusiveQuickSortN(Arr, Mid + 1, Hight);
}
}
void QuickSortN::NoRecusiveQuickSortN(int *Arr, int Low, int Hight)
{
if (Low < Hight)
{
stack s;
int Mid = QuickSortN::Partation(Arr, Low, Hight);
if (Low < Mid - 1)
{
s.push(Low);
s.push(Mid - 1);
}
if (Mid + 1 < Hight)
{
s.push(Mid + 1);
s.push(Hight);
}
//其实就是用栈保存每一个待排序子串的首尾元素下标,
//下一次while循环时取出这个范围,对这段子序列进行partition操作
while (!s.empty())
{
int q = s.top(); s.pop();
int p = s.top(); s.pop();
int Mid = QuickSortN::Partation(Arr, p, q);
if (p < Mid - 1)
{
s.push(p);
s.push(Mid - 1);
}
if (q < Hight)
{
s.push(q);
s.push(Hight);
}
}
}
}
QuickSortN::QuickSortN(int *Arr, int Len, char s ) : P(Arr),N(Len),S(s)
{
if (s == 'N')
{
NoRecusiveQuickSort(Arr, Len);
}
if (s == 'R')
{
RecusiveQuickSort(Arr, Len);
}
}
void QuickSortN::RecusiveQuickSort(int *Arr, int Len)
{
QuickSortN::RecusiveQuickSortN(Arr, 0, Len - 1);
}
void QuickSortN::NoRecusiveQuickSort(int *Arr, int Len)
{
QuickSortN::NoRecusiveQuickSortN(Arr, 0, Len - 1);
}
//归并排序
void MergSortN::Mymerge(int *arr, int start, int midd, int end)
{
assert(NULL != arr);
int *brr = (int *)malloc(sizeof(int*)*(end - start + 1));
int k = 0;
int s = start;
int m = midd + 1;
while (s <= midd && m <= end)
{
if (arr[s] < arr[m])
{
brr[k++] = arr[s++];
}
else
{
brr[k++] = arr[m++];
}
}
while (s <= midd)
{
brr[k++] = arr[s++];
}
while (m <= end)
{
brr[k++] = arr[m++];
}
for (int i = 0; i < k; ++i)
{
arr[start + i] = brr[i];
}
free(brr);
}
MergSortN::MergSortN(int *Arr, int Start, int End) : P(Arr), N(End)
{
assert(NULL != Arr);
if (Start < End)
{
int midd = (Start + End) / 2;
MergSortN(Arr, Start, midd);
MergSortN(Arr, midd + 1, End);
Mymerge(Arr, Start, midd, End);
}
}
//交换排序
SwapSortN::SwapSortN(int *Arr, int Len):P(Arr),N(Len)
{
SwapSort(Arr, Len);
}
void SwapSortN::SwapSort(int *Arr, int Len)
{
for (int i = 0; i < Len - 1; ++i)//最后一个不用再向后比较
{
for (int j = i + 1; j < Len; ++j)
{
if (Arr[i] > Arr[j])//不相邻交换,较大值向后移动,较小值向前移动
{
int tmp = Arr[i];
Arr[i] = Arr[j];
Arr[j] = tmp;
}
}
}
cout << "交换排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//冒泡排序
BubbleSortN::BubbleSortN(int *Arr, int Len) :P(Arr), N(Len)
{
BubbleSort(Arr, Len);
}
void BubbleSortN::BubbleSort(int *Arr, int Len)
{
bool flag = true;
for (int i = 0; i < Len - 1 && flag; ++i)//最后一个不用再向后比较
{
flag = false;
for (int j = 0; j < Len - 1 - i; ++j)//排除最后已经排序好的
{
if (Arr[j] > Arr[j + 1])//相邻交换
{
flag = true;
int tmp = Arr[j];
Arr[j] = Arr[j + 1];
Arr[j + 1] = tmp;
}
}
}
cout << "冒泡排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//选择排序
SelectSortN::SelectSortN(int *Arr, int Len) :P(Arr), N(Len)
{
SelectSort(Arr, Len);
}
void SelectSortN::SelectSort(int *Arr, int Len)
{
int min = Arr[0];
int min_index = 0;
int i = 0;
int j = 0;
for (i = 0; i < Len - 1; ++i)
{
min = Arr[i];
min_index = i;
for (j = i + 1; j < Len; ++j)//一次循环找出最小数的数值和下标且和arr[i]交换
{
if (min > Arr[j])
{
min = Arr[j];
min_index = j;
}
}
if (i != min_index)
{
int tmp = Arr[i];
Arr[i] = Arr[min_index];
Arr[min_index] = tmp;
}
}
cout << "选择排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//插入排序
InsertSortN::InsertSortN(int *Arr, int Len) :P(Arr), N(Len)
{
InsertSort(Arr, Len);
}
void InsertSortN::InsertSort(int *Arr, int Len)
{
int j = 0;
int tmp = 0;
for (int i = 1; i < Len; ++i)
{
tmp = Arr[i];
for (j = i - 1; j >= 0; --j)//arr[i]从arr[i-1]开始逆向比较
{
if (Arr[j] < tmp)//遇到比自己小的为止
{
break;
}
Arr[j + 1] = Arr[j];//比自己大的值后移
}
Arr[j + 1] = tmp;//插入合适位置
}
cout << "插入排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//希尔排序
void Shell(int *Arr, int Len, int Gap)//insert_sort()的变形,insert_sort()一数一组
{
int j = 0;
int Tmp = 0;
for (int i = Gap; i < Len; ++i)//gap个为一组
{
Tmp = Arr[i];
for (j = i - Gap; j >= 0; j -= Gap)
{
if (Arr[j] < Tmp)
{
break;
}
Arr[j + Gap] = Arr[j];
}
Arr[j + Gap] = Tmp;
}
}
ShellSortN::ShellSortN(int *Arr, int Len) :P(Arr), N(Len)
{
ShellSort(Arr, Len);
}
void ShellSortN::ShellSort(int *Arr, int Len)
{
Shell(Arr, Len, 3);//多次分组。提高效率,使得数逐渐基本有序
Shell(Arr, Len, 1);
cout << "希尔排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//堆排序
void AdjustHeap(int *Arr, int root, int Len)
{
int temproot = Arr[root];
//因为根是从零开始,所以左子树的索引为child=2*root+1;
int child = 2 * root + 1;
while (child <= Len)
{
if (child < Len&&Arr[child] < Arr[child + 1])
//判断左右字数大小,从中选择大的作为堆的当前位置
{
child++;
}
if (temproot > Arr[child])
break;
else
{
Arr[(child - 1) / 2] = Arr[child];
}
child = 2 * child + 1;
}
Arr[(child - 1) / 2] = temproot;
}
HeapSortN::HeapSortN(int *Arr, int Len) :P(Arr), N(Len)
{
HeapSort(Arr, Len);
}
void HeapSortN::HeapSort(int *Arr, int Len)
{
for (int i = Len / 2 - 1; i >= 0; i--)
{
AdjustHeap(Arr, i, Len - 1);
}
for (int i = Len - 1; i >= 0; i--)
{
int temp = Arr[0];
Arr[0] = Arr[i];
Arr[i] = temp;
if (i > 0)
AdjustHeap(Arr, 0, i - 1);
}
cout << "堆排后数据:";
ConcreteFactory::Print(Arr, Len);
}
//main.cpp
#include "Factory.h"
#include "SortProduct.h"
#include
using namespace std;
int main(int argc,char* argv[])
{
int Arr[] = { 1,4,2,9,5,7,3,8,6,46,32 };
int Len = sizeof(Arr) / sizeof(Arr[0]);
Factory* fac = new ConcreteFactory(Arr,Len);
SortProduct* p;
p = fac->QuickSort(Arr, Len, 'R');//R代表递归快排,N代表非递快排
p = fac->MergSort(Arr,Len);
p = fac->SwapSort(Arr,Len);
p = fac->BubbleSort(Arr, Len);
p = fac->InsertSort(Arr, Len);
p = fac->SelectSort(Arr, Len);
p = fac->ShellSort(Arr, Len);
p = fac->HeapSort(Arr, Len);
HeapSortN::HeapSort(Arr, Len);
return 0;
}