Sort.h
#pragma once
class Sort
{
public:
int Mod(int x, int y);
void StraightInsertionSort(int data[], int length);
void BinaryInsertionSort(int data[], int length);
void TwoWayInsertionSort(int data[], int length);
void ShellSort(int data[], int length);
void BubbleSort(int data[], int length);
void QuickSort(int data[], int length);
void SelectionSort(int data[], int length);
void TreeSelectionSort(int data[], int length);
void HeapSort(int data[], int length);
void MergingSort(int data[], int length);
void RadixSort(int data[], int length);
void ShowReasult(int data[], int length);
};
Sort.cpp
#include "stdafx.h"
#include "Sort.h"
#include "iostream"
#include "Stack.h"
#include "Queue.h"
using namespace std;
int Sort::Mod(int x, int y)
{
if (x < 0)
{
return (x % y) + y;
}
else
{
return x % y;
}
}
void Sort::ShowReasult(int data[], int length)
{
int i;
for (i = 0;i < length;i++)
{
cout << " " << data[i] << " ";
}
cout << endl;
}
void Sort::StraightInsertionSort(int data[], int length)
{
int temp;
int i, j;
for (i = 1;i < length;i++)
{
if (data[i] < data[i - 1])
{
temp = data[i];
data[i] = data[i - 1];
for (j = i - 1;temp < data[j] && j >= 0;j--)
{
data[j] = data[j - 1];
}
data[j + 1] = temp;
}
}
ShowReasult(data, length);
}
void Sort::BinaryInsertionSort(int data[], int length)
{
int temp;
int low = 0;
int high;
int middle;
int i, j;
for (i = 1;i < length;i++)
{
temp = data[i];
low = 0;
high = i - 1;
while (low <= high)
{
middle = (low + high) / 2;
if (temp < data[middle])
{
high = middle - 1;
}
else
{
low = middle + 1;
}
}
for (j = i;j > low;j--)
{
data[j] = data[j - 1];
}
data[low] = temp;
}
ShowReasult(data, length);
}
void Sort::TwoWayInsertionSort(int data[], int length)
{
int * link = new int[length];
link[0] = data[0];
int head = 0;
int tail = 0;
int i, j, k;
for (i = 1;i < length;i++)
{
if (data[i] <= link[head])
{
head = Mod(head - 1, length);
link[head] = data[i];
}
else if (data[i] >= link[tail])
{
tail = Mod(tail + 1, length);
link[tail] = data[i];
}
else
{
for (j = tail;data[i] < link[j];j = (j - 1) % length)
{
link[Mod(j + 1, length)] = link[Mod(j, length)];
}
link[j + 1] = data[i];
tail = Mod(tail + 1, length);
}
}
ShowReasult(link, length);
}
void Sort::ShellSort(int data[], int length)
{
int temp;
int i, j;
for (i = length / 2;i > 0;i--)
{
for (j = i;j < length;j++)
{
if (data[j] < data[j - i])
{
temp = data[j];
data[j] = data[j - i];
data[j - i] = temp;
}
}
}
ShowReasult(data, length);
}
void Sort::BubbleSort(int data[], int length)
{
int temp;
int i, j;
for (i = 0;i < length;i++)
{
for (j = i + 1;j < length;j++)
{
if (data[j] < data[i])
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
ShowReasult(data, length);
}
/*
递归实现快速排序法
*/
void Sort::QuickSort(int data[], int length,int low,int high)
{
int key = data[low];
int left = low;
int right = high;
int temp;
if (left < right)
{
while (left < right)
{
while (left < right&&key <= data[right])
{
right--;
}
temp = data[right];
data[right] = data[left];
data[left] = temp;
while (left < right&&data[left] <= key)
{
left++;
}
temp = data[right];
data[right] = data[left];
data[left] = temp;
}
QuickSort(data, length, low, left - 1);
QuickSort(data, length, left + 1, high);
}
ShowReasult(data, length);
}
/*
在大顶堆基础上进行调整
*/
void Sort::AdjustHeap(int data[], int length, int start)
{
int temp = data[start];
int i;
for (i = start * 2 + 1;i < length;i = i * 2 + 1)
{
if (i + 1 < length&&data[i + 1] > data[i])
{
i++;
}
if (data[i] > temp)
{
data[start] = data[i];
start = i;
}
else
{
break;
}
}
data[start] = temp;
}
void Sort::HeapSort(int data[], int length)
{
/*
从第一个非叶子节点开始自下而上,自右到左,构造大顶堆
*/
int i;
for (i = length / 2 - 1;i >= 0;i--)
{
AdjustHeap(data, length, i);
}
ShowReasult(data, length);
/*
取出堆顶元素并进行调整
*/
int temp;
for (i = 1;i < length;i++)
{
temp = data[0];
data[0] = data[length - i];
data[length - i] = temp;
AdjustHeap(data, length - i, 0);
}
ShowReasult(data, length);
}