# 【Algo】排序算法

## 1、冒泡

#include <iostream>

#include "sort.h"

using namespace std;

int BubbleSort(int *data, int size)
{
int step = 0;
bool swapped = false;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

for (int i = 0; i < size - 1; ++i) {
swapped = false;
int temp = 0;
for (int j = 0; j < size - 1 - i; ++j) {
++step;
if (data[j] > data[j + 1]) {
swapped = true;
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
if (!swapped) {
return step;
}
}

return step;
}

## 2、选择

#include <iostream>

#include "sort.h"

using namespace std;

int SelectionSort(int *data, int size)
{
int step = 0;
int min = 0;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

for (int i = 0; i < size - 1; ++i) {
min = i;
for (int j = i + 1; j < size; ++j) {
++step;
if (data[min] > data[j]) {
min = j;
}
}
if (min != i) {
int temp = data[i];
data[i] = data[min];
data[min] = temp;
}

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}

return step;
}

int SelectionSort2(int *data, int size)
{
int step = 0;
int min = 0;
int max = 0;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

for (int i = 0; i < size / 2; ++i) {
min = i;
max = i;
for (int j = i + 1; j < size - i; ++j) {
++step;
if (data[min] > data[j]) {
min = j;
}
if (data[max] < data[j]) {
max = j;
}
}
if (min != i) {
int temp = data[i];
data[i] = data[min];
data[min] = temp;
}
if (max != size - i - 1) {
if (max == i) {
max = min;
}
int temp = data[size - i - 1];
data[size - i - 1] = data[max];
data[max] = temp;
}

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}

return step;
}

## 3、插入

#include <iostream>

#include "sort.h"

using namespace std;

int InsertSort(int *data, int size)
{
int step = 0;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

for (int i = 1; i < size; ++i) {
int temp = data[i];
int index = i - 1;
while (index >= 0) {
++step;
if (temp >= data[index]) {
break;
}
data[index + 1] = data[index];
--index;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
data[index + 1] = temp;

cout << "step:" << step << " data:";
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}

return step;
}

## 4、快排

#include <iostream>

#include "sort.h"

using namespace std;

int QuickSort(int *data, int left, int right)
{
int index = left;
int pivot = data[index];

for (int i = left + 1; i <= right; ++i) {
if (data[i] < pivot) {
++index;
if (index != i) {
int temp = data[index];
data[index] = data[i];
data[i] = temp;
}
}
}
if (index != left) {
data[left] = data[index];
data[index] = pivot;
}

if (index -1 > left) {
QuickSort(data, left, index -1);
}
if (index + 1 < right) {
QuickSort(data, index + 1, right);
}

static int step = 0;
++step;
cout << "step:" << step << " data:";
for (int i = left; i <= right; ++i) {
cout << data[i] << " ";
}
cout << endl;

return 0;
}

int QuickSort2(int *data, int left, int right)
{
int begin = left;
int end = right;
int pivot = data[begin];

while (begin < end) {
while (begin < end && data[end] >= pivot) {
--end;
}
data[begin] = data[end];
while (begin < end && data[begin] <= pivot) {
++begin;
}
data[end] = data[begin];
}
data[begin] = pivot;

if (begin - 1 > left) {
QuickSort2(data, left, begin - 1);
}
if (begin + 1 < right) {
QuickSort2(data, begin + 1, right);
}

static int step = 0;
++step;
cout << "step:" << step << " data:";
for (int i = left; i <= right; ++i) {
cout << data[i] << " ";
}
cout << endl;

return 0;
}

## 5、堆

#include <iostream>

#include "sort.h"

using namespace std;

void HeapAdjust(int *data, int size, int root)
{
int child = root * 2 + 1;
while (child < size) {
if (child + 1 < size && data[child + 1] > data[child]) {
++child;
}
if (data[child] > data[root]) {
data[root] = data[root] ^ data[child];
data[child] = data[root] ^ data[child];
data[root] = data[root] ^ data[child];
root = child;
child = root * 2 + 1;
}
else {
break;
}
}
}

int HeapSort(int *data, int size)
{
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

for (int i = size / 2 - 1; i >= 0; --i) {
}

for (int i = 0; i < size - 1; ++i) {
{
data[size -1 -i] = data[size -1 -i] ^ data[0];
data[0] = data[size -1 -i] ^ data[0];
data[size -1 -i] = data[size -1 -i] ^ data[0];
}
HeapAdjust(data, size - 1 - i, 0);
}

for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

return 0;
}

## 6、希尔

#include <iostream>

#include "sort.h"

using namespace std;

void ShellSortInsert(int *data, int size, int inc)
{
cout << inc << endl;

for (int i = inc; i < size; ++i) {
if (data[i] < data[i - inc]) {
int j = i - inc;
int t = data[i];
while (j >=0 && t < data[j]) {
data[j + inc] = data[j];
j -= inc;
}
data[j + inc] = t;

for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
}
}

int ShellSort(int *data, int size)
{
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

int inc = size / 2;
while (inc >= 1) {
ShellSortInsert(data, size, inc);
inc = inc / 2;
}

for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

return 0;
}

## 7、归并

#include <iostream>

#include "sort.h"

using namespace std;

void MergeSortConquer(int *data, int first, int mid, int last, int *temp)
{
int lfirst = first;
int rfirst = mid + 1;
int llast = mid;
int rlast = last;
int index = 0;

while (lfirst <= llast && rfirst <= rlast) {
if (data[lfirst] < data[rfirst]) {
temp[index++] = data[lfirst++];
}
else {
temp[index++] = data[rfirst++];
}
}

while (lfirst <= llast) {
temp[index++] = data[lfirst++];
}
while (rfirst <= rlast) {
temp[index++] = data[rfirst++];
}

for (int i = 0; i < index; ++i) {
data[first + i] = temp[i];
}
}

void MergeSortDivide(int *data, int first, int last, int *temp)
{
if (first < last) {
int mid = (first + last) / 2;
MergeSortDivide(data, first, mid, temp);
MergeSortDivide(data, mid + 1, last, temp);
MergeSortConquer(data, first, mid, last, temp);
}
}

int MergeSort(int *data, int size)
{
for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

int *temp = new int[size];
MergeSortDivide(data, 0, size - 1, temp);
delete[] temp;

for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;

return 0;
}