#include <iostream>
#include <vector>
using namespace std;
//BulleSort
void BubbleSort(vector<int>&Array){
bool flag = true; //a flag to record whether the swap does;
for (int i = 0; i < Array.size()&&flag; i++){
flag = false;
for (int j = Array.size() - 1; j > i; j--){
if (Array[j-1]>Array[j]){
swap(Array[j-1], Array[j]);
flag = true;
}
}
}
}
//SelectSort
void SelectSort(vector<int>&Array){
int min;
for (int i = 0; i < Array.size(); i++){
min = i;
for (int j = i + 1; j < Array.size(); j++){
if (Array[min]>Array[j]){
min = j;
}
}
if (i != min){
swap(Array[i], Array[min]);
}
}
}
//InsertSort
void InsertSort(vector<int>&Array){
int j;
for (int i = 1; i < Array.size(); i++){
if (Array[i] < Array[i - 1]){
int pivot = Array[i];
for (j = i - 1; j >= 0 && Array[j] > pivot; j--){ //first judge j?0, otherwise j=-1, the Array[j] is wrong!!
Array[j + 1] = Array[j];
}
Array[j + 1] = pivot;
}
}
}
//ShellSort
void ShellSort(vector<int>&Array){
int increment = Array.size() - 1;
int j;
do
{
increment = increment / 3 + 1;
for (int i = increment; i < Array.size(); i++){
if (Array[i] < Array[i - increment]){
int temp = Array[i];
for (j = i - increment; j >= 0 && temp < Array[j]; j -= increment){
Array[j + increment] = Array[j];
}
Array[j + increment] = temp;
}
}
} while (increment>1);
}
//HeapSort
void HeapAdjust(vector<int>&Array, int s, int m) {
int temp;
temp = Array[s];
for (int j = 2 * s; j < m; j *= 2) {
if (j <= m - 1 && Array[j] < Array[j + 1]) {
j++;
}
if (temp >= Array[j]) {
break;
}
Array[s] = Array[j];
s = j;
}
Array[s] = temp;
}
void HeapSort(vector<int>&Array) {
int length = Array.size();
for (int i = length / 2; i >= 0; i--) {
HeapAdjust(Array, i, length - 1);
}
for (int i = 0; i < Array.size(); i++) {
cout << Array[i] << ' ';
}
cout <<endl<< "lllllllllllll" << endl;
for (int i = length - 1; i >= 1; i--) { //注意下标
swap(Array[0], Array[i]);
HeapAdjust(Array, 0, i - 1);
}
}
//MergingSort
void Merge(vector<int>SR, vector<int>&TR, int i, int m, int n){
int j, k, l;
for (j = m + 1, k = i; i <= m&&j <= n; k++){
if (SR[i] < SR[j]){
TR[k] = SR[i++];
}
else{
TR[k] = SR[j++];
}
}
if (i <= m){
for (l = 0; l <= m - i; l++){
TR[k + l] = SR[i + l];
}
}
if (j <= n){
for (l = 0; l <= n - j; l++){
TR[k + l] = SR[j + l];
}
}
}
void MSort(vector<int>SR, vector<int>&TR1, int s, int t){
int middle;
vector<int>TR2(TR1.size(), 0);
if (s == t){
TR1[s] = SR[s];
}
else{
middle = s + (t - s) / 2;
MSort(SR, TR2, s, middle);
MSort(SR, TR2, middle + 1, t);
Merge(TR2, TR1, s, middle, t);
}
}
void MergeSort(vector<int>&Array){
MSort(Array, Array, 0, Array.size() - 1);
}
//QuickSort
int Partition(vector<int>&Array, int l, int r){
int pivotkey;
pivotkey = Array[l];
while (l < r){
while (l<r&&Array[r]>pivotkey){
r--;
}
swap(Array[l], Array[r]);
while (l<r&&Array[l]<pivotkey)
{
l++;
}
swap(Array[l], Array[r]);
}
return l;
}
void QSort(vector<int>&Array, int l, int r){
if (l < r){
int pivot;
pivot = Partition(Array, l, r);
QSort(Array, l, pivot-1);
QSort(Array, pivot + 1, r);
}
}
void QuickSort(vector<int>&Array){
QSort(Array, 0, Array.size() - 1);
}
//test
void main(){
vector<int>array{9,1,5,8,3,7,4,6,2};
cout << "the original array is:" << endl;
for (vector<int>::iterator t = array.begin(); t != array.end(); t++){
cout << *t << " ";
}
BubbleSort(array);
cout << endl << "the ranked array by BubbleSort is:" << endl;
for (vector<int>::iterator t = array.begin(); t != array.end(); t++){
cout << *t << " ";
}
cout << endl;
//SelectSort
vector<int>array2{9, 1, 5, 8, 3, 7, 4, 6, 2};
SelectSort(array2);
cout << endl << "the ranked array by SelectSort is:" << endl;
for (vector<int>::iterator t = array2.begin(); t != array2.end(); t++){
cout << *t << " ";
}
cout << endl;
//InsertSort
vector<int>array3{ 9, 1, 5, 8, 3, 7, 4, 6, 2 };
InsertSort(array3);
cout << endl << "the ranked array by InsertSort is:" << endl;
for (vector<int>::iterator t = array3.begin(); t != array3.end(); t++){
cout << *t << " ";
}
cout << endl;
//HeapSort
vector<int>array4{ 9, 1, 5, 8, 3, 7, 4, 6, 2 };
InsertSort(array4);
cout << endl << "the ranked array by HeapSort is:" << endl;
for (vector<int>::iterator t = array4.begin(); t != array4.end(); t++){
cout << *t << " ";
}
cout << endl;
//merging sort
vector<int>array5{ 5, 1, 9, 3, 7, 4, 8, 6, 2 };
cout << endl << "the original array is:" << endl;
for (vector<int>::iterator t = array5.begin(); t != array5.end(); t++){
*t *= 10;
cout << *t << " ";
}
cout << endl;
cout << endl << "the ranked array by MergingSort is:" << endl;
MergeSort(array5);
for (vector<int>::iterator t = array5.begin(); t != array5.end(); t++){
cout << *t << " ";
}
cout << endl;
//shell sort
vector<int>array9{ 9, 1, 5, 8, 3, 7, 4, 6, 2 };
cout << endl << "the ranked array by ShellSort is:" << endl;
ShellSort(array9);
for (vector<int>::iterator t = array9.begin(); t != array9.end(); t++){
cout << *t << " ";
}
cout << endl;
//QuickSort
vector<int>array6{ 5, 1, 9, 3, 7, 4, 8, 6, 2 };
for (vector<int>::iterator t = array6.begin(); t != array6.end(); t++){
*t *= 10;
}
QuickSort(array6);
cout << endl << "the ranked array by QuickSort is:" << endl;
for (vector<int>::iterator t = array6.begin(); t != array6.end(); t++){
cout << *t << " ";
}
cout << endl;
system("pause");
}