#include <iostream>
using namespace std;
void bubbleSort(int list[], int length) {
for (int i = length; i > 0; i--) {
for (int j = 1; j < i; j++) {
if (list[j] < list[j - 1]) {
int tmp = list[j];
list[j] = list[j - 1];
list[j - 1] = tmp;
}
}
}
}
void insertSort(int list[], int length) {
for (int i = 0; i < length; i++) {
int current = list[i];
int position = i;
for (int j = i - 1; j >= 0; j--) {
if (current < list[j]) {
list[position] = list[j];
position = j;
}
}
list[position] = current;
}
}
void selectSort(int list[], int length) {
for (int i = 0; i < length; i++) {
int min = list[i];
int position = i;
for (int j = i; j < length; j++) {
if (min > list[j]) {
min = list[j];
position = j;
}
}
list[position] = list[i];
list[i] = min;
}
}
void shellSort(int list[], int lenght) {
for (int d = lenght / 2; d > 0; d = d / 2) {
for (int i = 0; i < lenght; i++) {
int current = list[i];
int position = i;
for (int j = i; j >= 0; j -= d) {
if (current < list[j]) {
list[position] = list[j];
position = j;
}
}
list[position] = current;
}
}
}
int quickSortHelper(int list[], int front, int end) {
int current = list[front];
bool isFromEnd = true;
while (front < end) {
if (isFromEnd) {
if (current < list[end]) {
end--;
} else {
list[front] = list[end];
list[end] = current;
front++;
isFromEnd = false;
}
} else {
if (current > list[front]) {
front++;
} else {
list[end] = list[front];
list[front] = current;
end--;
isFromEnd = true;
}
}
}
return front;
}
void quickSort(int list[], int front, int end) {
if (front < end) {
int mid = quickSortHelper(list, front, end);
quickSort(list, front, mid - 1);
quickSort(list, mid + 1, end);
}
}
void mergeSortHelper(int list[], int front, int end) {
int mid = (front + end) / 2;
int leftLenght = mid - front + 1;
int rightLenght = end - mid;
int leftList[leftLenght];
int rightList[rightLenght];
int leftPosition = 0;
int rightPosition = 0;
for (int i = 0; i < leftLenght; i++) {
leftList[i] = list[front + i];
}
for (int j = 0; j < rightLenght; j++) {
rightList[j] = list[mid + 1 + j];
}
for (int k = front; k <= end; k++) {
if (leftPosition < leftLenght && rightPosition < rightLenght) {
if (leftList[leftPosition] < rightList[rightPosition]) {
list[k] = leftList[leftPosition];
leftPosition++;
} else {
list[k] = rightList[rightPosition];
rightPosition++;
}
} else if (leftPosition < leftLenght) {
list[k] = leftList[leftPosition];
leftPosition++;
} else if (rightPosition < rightLenght) {
list[k] = rightList[rightPosition];
rightPosition++;
}
}
}
void mergeSort(int list[], int front, int end) {
if (front < end) {
int mid = (front + end) / 2;
mergeSort(list, front, mid);
mergeSort(list, mid + 1, end);
mergeSortHelper(list, front, end);
}
}
void heapAdjust(int list[], int node, int end) {
int leftchild = 2 * node + 1;
int rightchild = 2 * node + 2;
int maxPosition = node;
if(node <= (end - 1) / 2) {
if(leftchild <= end) {
maxPosition = list[leftchild] > list[maxPosition] ? leftchild : maxPosition;
}
if(rightchild <= end) {
maxPosition = list[rightchild] > list[maxPosition] ? rightchild : maxPosition;
}
if(maxPosition != node) {
int tmp = list[node];
list[node] = list[maxPosition];
list[maxPosition] = tmp;
heapAdjust(list, maxPosition, end);
}
}
}
void heapBuild(int list[], int end) {
for(int i = (end - 1) / 2; i >= 0; i--) {
heapAdjust(list, i, end);
}
}
void heapSort(int list[], int end) {
heapBuild(list, end);
for(int i = end; i >= 0; i--) {
int tmp = list[i];
list[i] = list[0];
list[0] = tmp;
heapAdjust(list, 0, i - 1);
}
}
int main(int argc, char **argv) {
int array[] = { 9, 5, 6, 3, 1, 4, 8, 2, 0, 7 };
int len = sizeof array / sizeof array[0];
// bubbleSort(array, len);
// insertSort(array, len);
// selectSort(array, len);
// shellSort(array, len);
// quickSort(array, 0, len - 1);
// mergeSort(array, 0, len - 1);
heapSort(array, len - 1);
for (int i = 0; i < 10; i++) {
cout << array[i] << " ";
}
}
代码系列1:c++实现的7种排序算法
最新推荐文章于 2024-04-29 11:56:38 发布