#include "iostream"
#include<stack>
using namespace std;
template<class T>
void ShellBubbleSort(T a[], int Length){
for (int gap = Length / 2; gap > 0; gap/=2){
for (int i = 0; i < Length - 1; i += gap){
for (int j = 0; j < Length - i - gap; j += gap){
if (a[j]>a[j + gap]){
T tmp = a[j + gap];
a[j + gap] = a[j];
a[j] = tmp;
}
}
}
}
}
template<class T>
void test(T arr[], int Length){
for (int i = 1; i < Length; i++)
{
T val = arr[i];
int j = i;
while (j>0 && arr[j - 1] > val&&j - 1 >= 0)
{
arr[j] = arr[j - 1];
j = j - 1;
}
arr[j] = val;
}
}
template<class T>
void BubbleSort(T arr[],int Length){
for (int i = 0; i < Length - 1; i++)
{
for (int j = 0; j < Length - i - 1; j++)
{
if (arr[j]>arr[j + 1])
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
template<class T>
void print(T arr[], int Length){
for (int i = 0; i <Length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
template<class T>
void ShellSort(T arr[], int Length)
{
for (int gap = Length / 2; gap > 0; gap /= 2){
for (int i = gap; i < Length; i++)
{
T val = arr[i];
int j = i;
while (j>0 && arr[j-gap] > val&&j-gap>=0)
{
arr[j] = arr[j - gap];
j = j - gap;
}
arr[j] = val;
}
}
}
template <class T>
int QuickSort(T arr[], int begin, int end){
if (begin < end){
T temp = arr[begin];
while (begin < end){
while (begin < end&&arr[end] >temp)
{
end--;
}
arr[begin] = arr[end];
while (begin < end&&arr[begin] <= temp)
{
begin++;
}
arr[end] = arr[begin];
}
arr[begin] = temp;
return begin;
}
else
return 0;
}
template <class T>
void QuickSort1(T arr[], int begin, int end)
{
if (begin < end){
int mid = QuickSort(arr, begin, end);
QuickSort1(arr, begin, mid - 1);
QuickSort1(arr, mid + 1, end);
}
}
template<class T>
void QuickSort2(T arr[], int begin, int end){
stack<int> st;
if (begin < end){
int mid = QuickSort(arr, begin, end);
if (begin < mid - 1){
st.push(begin);
st.push(mid - 1);
}
if (mid + 1 < end){
st.push(mid + 1);
st.push(end);
}
while (!st.empty()){
int q = st.top();
st.pop();
int p = st.top();
st.pop();
mid = QuickSort(arr, p, q);
if (p < mid - 1){
st.push(p);
st.push(mid - 1);
}
if (mid + 1 < q){
st.push(mid + 1);
st.push(q);
}
}
}
}
template<class T>
void QuickSort_Fix1(T arr[], int begin, int end){
swap(arr[begin], arr[rand() % (end-begin)+begin]);
if (begin < end){
T temp = arr[begin];
int i = begin;
int j = end;
while (i < j){
while (i < j&&arr[j] >temp)
{
j--;
}
arr[i] = arr[j];
while (i < j&&arr[i] <= temp)
{
i++;
}
arr[j] = arr[i];
}
arr[i] = temp;
QuickSort1(arr, begin, i - 1);
QuickSort1(arr, i + 1, end);
}
else
return ;
}
template<class T>
int GetMiddleValue(T arr[], int begin, int end)
{
int mid = (begin + (end - begin) / 2);
int y1 = arr[begin] > arr[mid] ? begin : mid;
int y2 = arr[begin] > arr[end] ? begin : end;
int y3 = arr[mid] > arr[end] ? mid : end;
if (y1 == y2)
{
return y3;
}
else
{
return arr[y1] > arr[y2] ? y2:y1;
}
}
template<class T>
void QuickSort_Fix2(T arr[], int begin, int end){
if (begin < end){
int mid = GetMiddleValue(arr, begin, end);
if (mid != begin)
{
T temp = arr[mid];
arr[mid] = arr[begin];
arr[begin] = temp;
}
T temp = arr[begin];
int i = begin;
int j = end;
while (i < j){
while (i < j&&arr[j] >temp)
{
j--;
}
arr[i] = arr[j];
while (i < j&&arr[i] <= temp)
{
i++;
}
arr[j] = arr[i];
}
arr[i] = temp;
QuickSort_Fix2(arr, begin, i - 1);
QuickSort_Fix2(arr, i + 1, end);
}
else
return;
}
int main()
{
int arr[8] = { 9, 4, 6, 2,9,5,4,1};
int arr1[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int arr2[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int arr3[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int arr4[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int arr5[8] = { 1, 4, 6, 9, 9, 10, 12, 2 };
int arr6[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int arr7[8] = { 9, 4, 6, 2, 9, 5, 4, 1 };
int length;
length = sizeof(arr) / sizeof(int);
cout << "测试数据为:" << endl;
print<int>(arr, length);
cout << "使用冒泡排序:" << endl;
BubbleSort<int >(arr, length);
print<int>(arr, length);
cout << "重新测试:" << endl;
print<int>(arr1, length);
cout << "使用希尔排序(内部为冒泡排序):" << endl;
ShellBubbleSort<int>(arr1, length);
print<int>(arr1, length);
cout << "重新测试:" << endl;
print<int>(arr2, length);
cout << "使用希尔排序(内部为插入排序):" << endl;
ShellSort<int>(arr2, length);
print<int>(arr2, length);
cout << "重新测试:" << endl;
print<int>(arr3, length);
cout << "使用快速排序(递归算法):" << endl;
QuickSort1<int>(arr3, 0, length - 1);
print<int>(arr3, length);
cout << "重新测试:" << endl;
print<int>(arr4, length);
cout << "使用快速排序(非递归算法(栈)):" << endl;
QuickSort2<int>(arr4, 0,length -1);
print<int>(arr4, length);
cout << "重新测试:" << endl;
print<int>(arr5, length);
cout << "使用快速排序(改进(当数组近乎有序时)):" << endl;
QuickSort_Fix1<int>(arr5, 0, length - 1);
print<int>(arr5, length);
cout << "重新测试:" << endl;
print<int>(arr6, length);
cout << "使用快速排序(改进(三个数取中)):" << endl;
QuickSort_Fix2<int>(arr6,0,length - 1);
print<int>(arr6, length);
cout << "重新测试:" << endl;
print<int>(arr7, length);
cout << "使用插入排序:" << endl;
test<int>(arr7,length);
print<int>(arr7, length);
system("pause");
return 0;
}
