一 直接插入排序
1、算法思想
每次将一个待排序的记录按其关键码的大小插入到一个已经排好序的队列中,直到全部记录排好序。
2、算法思想图解
3、算法排序过程示例
4、代码实现
算法实现时间复杂度位O(n * n),该算法是一种稳定的算法。
// 直接插入排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void InsertSort(int arr[], int arrSize)
{
int i, j;
int key;
for (i = 1; i < arrSize; i++) {
key = arr[i];
j = i - 1;
while ((j > (-1)) && (arr[j] > key)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main()
{
int i;
int arr[6] = {5, 2, 4, 6, 1, 3};
cout << "Before sort: ";
for (i = 0; i < 6; i++) {
cout << arr[i] << ' ';
}
cout << endl;
InsertSort(arr, 6);
cout << "After sort: ";
for (i = 0; i < 6; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
二 希尔排序
1、算法思想
先将整个待排序的记录序列分隔成若干个子序列,在子序列内分别进行直接插入排序,待整个序列基本有序时,再对全体记录进行一次直接插入排序。
2、排序过程步骤
假设待排序的记录为n个,先取整数d < n,例如, 取d = ⌊n / 2⌋ ,将所有相距为d的记录构成一组,从而将整个待排序记录序列分割成为d个子序列。对每个子序列分别进行直接插入排序,然后再缩小间隔d。例如,取d = ⌊d / 2⌋,重复上述分割,在对每个子序列分别进行直接插入排序,直到最后取d = 1,即将所有记录放在一组进行一次直接插入排序,最终将所有记录重新排列成按关键码有序的序列。
3、排序过程示例
4、代码实现
算法实现时间复杂度位O(n * log2n),该算法是一种不稳定的算法。
// 希尔排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void ShellSort(int arr[], int arrSize)
{
int d, i, j;
int key;
for (d = arrSize / 2; d >= 1; d = d / 2) {
for (i = d; i < arrSize; i++) {
key = arr[i];
for (j = i - d; j > (-1) && key < arr[j]; j = j - d) {
arr[j + d] = arr[j];
}
arr[j + d] = key;
}
}
}
int main()
{
int arr[9] = {59, 20, 17, 36, 98, 14, 23, 83, 13};
int i;
cout << "Before sort: ";
for (i = 0; i < 9; i++) {
cout << arr[i] << ' ';
}
cout << endl;
ShellSort(arr, 9);
cout << "After sort: ";
for (i = 0; i < 9; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
三 冒泡排序
1、算法思想
两两比较相邻记录的关键码,如果反序则交换,直到没有反序为止。
2、排序过程
(1) 将整个待排序的记录序列划分成有序区和无序区,初始时有序区为空,无序区包括所有待排序的记录。
(2) 对无序区从前向后依次将相邻记录的关键码进行比较,若反序则交换,从而使得关键码小的记录向前移动,关键码大的记录向后移动(像水中的气泡,体积大的先浮上来)。
(3) 重复执行(2),直到无序区没有反序的记录。
3、排序示例
4、代码实现
算法实现时间复杂度位O(n * n),该算法是一种稳定的算法。
// 冒泡排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void BubbleSort(int arr[], int arrSize)
{
int exchange = arrSize - 1;
int bound;
int i;
int tmp;
while (exchange != 0) {
bound = exchange;
exchange = 0;
for (i = 0; i < bound; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
exchange = i;
}
}
}
}
int main()
{
int arr[8] = {50, 13, 55, 97, 27, 38, 49, 65};
int i;
cout << "Before Sort: ";
for(i = 0; i < 8; i++) {
cout << arr[i] << ' ';
}
cout << endl;
BubbleSort(arr, 8);
cout << "After Sort: ";
for(i = 0; i < 8; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
四 快速排序
1、排序思想
首先选一个轴值,将待排序的记录划分成独立的两个部分,左侧记录的关键码均小于或等于轴值,右侧记录的关键码均大于或等于轴值,然后分别对这两部分重复上述过程,知道真个序列有序。
2、算法执行示例
3、代码实现
算法实现时间复杂度位O(n * log2n),该算法是一种不稳定的算法。
// 快速排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//一次划分代码实现
int Partition(int arr[], int first, int end)
{
int i = first;
int j = end;
int tmp;
while (i < j) {
while(i < j && arr[i] <= arr[j]) {
j--;
}
if (i < j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
}
while(i < j && arr[i] <= arr[j]) {
i++;
}
if (i < j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
j--;
}
}
return i;
}
//递归实现快速排序
void QuickSort(int arr[], int first, int end)
{
if (first < end) {
int pivot;
pivot = Partition(arr, first, end);
QuickSort(arr, first, pivot - 1);
QuickSort(arr, pivot + 1, end);
}
}
int main()
{
int arr[6] = {23, 13, 6, 31, 19, 28};
int i;
cout << "Before sort: ";
for (i = 0; i < 6; i++) {
cout << arr[i] << ' ';
}
cout << endl;
QuickSort(arr, 0, 5);
cout << "After sort: ";
for (i = 0; i < 6; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
五 简单选择排序
1、算法思想
第 i 趟排序在待排序序列arr[i] ~ arr[n](1 <= i<= arrSize - 1)中选取关键码最小的记录,并和第i个记录交换作为有序序列的第i个记录。
2、算法执行过程
(1)将这个记录序列划分为有序区和无序区,初始时有序区为空,无序区含有待排序的所有记录
(2)在无序区中选取关键码最小的记录,将它与无序区中的第一个记录交换,使得有序区扩展了一个记录,同时无序区减少了一个记录。
(3)不断重复(2),直到无序区只剩下一个记录为止。
3、算法执行示例
4、代码实现
算法实现时间复杂度位O(n * n),该算法是一种稳定的算法。
// 简单选择排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void SelectSort(int arr[], int arrSize)
{
int i, j;
int index;
for (i = 0; i < arrSize; i++) {
index = i;
for (j = i + 1; j < arrSize; j++) {
if (arr[j] < arr[index]) {
index = j;
}
}
if (index != i) {
int tmp = arr[i];
arr[i] = arr[index];
arr[index] = tmp;
}
}
}
int main()
{
int arr[7] = {49, 27, 65, 97, 76, 13, 38};
int i;
cout << "Before sort: ";
for (i = 0; i < 7; i++) {
cout << arr[i] << ' ';
}
cout << endl;
SelectSort(arr, 7);
cout << "After sort: ";
for (i = 0; i < 7; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}
六 归并排序
1、算法思想
利用分治法的策略,将若干个有序序列进行两两归并,直至所有待排序记录都在一个有序序列为止
2、算法执行步骤
将具有n个待排序的记录序列看成是n个长度为1的有序序列,然后进行两两归并,得到 ⌈n / 2⌉个长度为2的有序序列,再进行两两归并,得到 ⌈n / 4⌉个长度为4的有序序列,…直至得到一个长度为n的有序序列。
3、算法执行示例
4、算法实现
算法实现时间复杂度位O(n * log2n),该算法是一种稳定的算法。
// 归并排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void Merge(int arr[], int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
int* Left = (int*)malloc(sizeof(int) * (n1 + 2));
if (Left == NULL) {
return;
}
memset(Left, 0, sizeof(int) * (n1 + 2));
int* Right = (int*)malloc(sizeof(int) * (n2 + 2));
if (Right == NULL) {
return;
}
memset(Right, 0, sizeof(int) * (n2 + 2));
int i, j, k;
for (i = 1; i < n1 + 1; i++) {
Left[i] = arr[p + i - 1];
}
for (j = 1; j < n2 + 1; j++) {
Right[j] = arr[q + j];
}
Left[n1 + 1] = 65535;
Right[n2 + 1] = 65535;
i = 1;
j = 1;
for (k = p; k < r + 1; k++) {
if (Left[i] <= Right[j]) {
arr[k] = Left[i];
i++;
} else {
arr[k] = Right[j];
j++;
}
}
free(Left);
Left = NULL;
free(Right);
Right = NULL;
}
void MergeSort(int arr[], int p, int r)
{
if (p < r) {
int q = (p + r) / 2;
MergeSort(arr, p, q);
MergeSort(arr, q + 1, r);
Merge(arr, p, q, r);
}
}
int main()
{
int arr[9] = {8, 3, 9, 2, 6, 7, 1, 5, 4};
int i;
cout << "Before Sort: ";
for (i = 0; i < 9; i++) {
cout << arr[i] << ' ';
}
cout << endl;
MergeSort(arr, 0, 8);
cout << "After Sort: ";
for (i = 0; i < 9; i++) {
cout << arr[i] << ' ';
}
cout << endl;
system("pause");
return 0;
}