目录
包含有多个内部排序算法代码实现:
直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序、简单选择排序、堆排序、二路归并排序
代码实现(C++):
排序函数声明sort.h:
#pragma once
#define ARRAY_LENTH 10
typedef int ElemType;
class Sort {
public:
Sort() = default;
~Sort() = default;
static void Direct_Insert_Sort(ElemType A[], int n);
static void Binary_Insert_Sort(ElemType A[], int n);
static void Shell_Sort(ElemType A[], int n);
static void Swap(ElemType &i, ElemType &j);
static void Bubble_Sort(ElemType A[], int n);
static void Quick_Sort(ElemType A[], int low, int high);
static void Select_Sort(ElemType A[], int n);
static void Heap_MaxAdjust(ElemType A[], int len, int i);
static void Heap_Sort(ElemType A[], int len);
static void Merge(ElemType A[], int low, int mid, int high);
static void MergeSort(ElemType A[], int low, int high);
};
排序函数实现sort.cpp:
#include "sort.h"
//stable algorithm
void Sort::Direct_Insert_Sort(ElemType A[], int n) {
int i,j;
ElemType temp;
for(i = 1;i < n;++i) { //from 2nd to the end
if(A[i] < A[i-1]) {
temp = A[i];
for(j = i-1;j >= 0 && A[j] > temp;--j) //search insert position
A[j+1] = A[j]; //copy datas to back
A[j+1] = temp;
}
}
}
//stable algorithm
void Sort::Binary_Insert_Sort(ElemType A[], int n) {
int i,j,low,high,mid;
ElemType temp; //use bi-search when sorting
for(i = 1;i < n;++i) { //from 2nd to the end
temp = A[i];
low = 0; high = i - 1;
while(low <= high) { //bisearch
mid = (low + high) / 2;
if(A[mid] > temp) high = mid - 1;
else low = mid + 1;
}
for(j = i-1;j >= low;--j)
A[j+1] = A[j]; //copy datas to back
A[low] = temp; //insert temp
}
}
//stable algorithm
void Sort::Shell_Sort(ElemType A[], int n) {
int i,j,dk;
ElemType temp;
for(dk = n/2;dk >= 1;dk /= 2) //set the dk equal n/2
for(i = dk;i < n;++i)
if(A[i] < A[i-dk]) {
temp = A[i];
for(j = i - dk;j >= 0 && A[j] > temp;j -= dk)
A[j+dk] = A[j]; //search position to insert
A[j+dk] = temp; //insert temp
}//if
}
//swap element
void Sort::Swap(ElemType &i, ElemType &j) {
ElemType temp = i;
i = j;
j = temp;
}
//stable algorithm
void Sort::Bubble_Sort(ElemType A[], int n) {
//from end to start
for(int i = 0;i < n-1;++i) {
bool flag = false; //sign exchange
for(int j = n-1;j > i;--j)
if(A[j-1] > A[j]) {
Sort::Swap(A[j-1], A[j]);
flag = true;
}
if(flag == false)
return; //means no exchange -> sort ok!
}
// //from start to end
// for(int i = n-1;i > 0;--i) {
// bool flag = false;
// for(int j = 0;j < i;++j)
// if(A[j] > A[j+1]) {
// Sort::Swap(A[j+1], A[j]);
// flag = true;
// }
// if(flag == false)
// return;
// }
}
//unstable algorithm
void Sort::Quick_Sort(ElemType A[], int low, int high) {
if(low >= high)
return;
ElemType pivot = A[low]; //set init pivot
int i = low,j = high;
while (i < j) {
while (i < j && A[j] >= pivot)
j--; //when A[j]<pivot,it break
A[i] = A[j]; //move small element
while (i < j && A[i] <= pivot)
i++; //when A[i]>pivot,it break
A[j] = A[i]; //move big element
}
A[i] = pivot; //set pivot position
Sort::Quick_Sort(A, low, i-1);//recursive left
Sort::Quick_Sort(A, i+1, high);//recursive right
}
//unstable algorithm
void Sort::Select_Sort(ElemType A[], int n) {
for(int i = 0;i < n-1;i++) {
int min = i; //sign the minimum data index
for(int j = i+1;j < n;j++) {
if(A[j] < A[min])
min = j; //change min index
}
if(min != i) { //swap data
Sort::Swap(A[i], A[min]);
}
}
}
//unstable algorithm
void Sort::Heap_Sort(ElemType A[], int len) {
int parent = (len - 1) / 2;//set first parent node is the 1/2 length of array
for(int i = parent;i >= 0;i--) { //set big root heap
Sort::Heap_MaxAdjust(A, len, i);
}
for(int i = len-1;i >= 0;i--) {
Sort::Swap(A[i], A[0]); //exchange last element and first element
Sort::Heap_MaxAdjust(A, i, 0); //re-adjust heap
}
}
void Sort::Heap_MaxAdjust(ElemType A[], int len, int i) {
int max = i; //assume i is the max node
int lchild = 2*i + 1; //set left child
int rchild = 2*i + 2; //set right child
if(lchild < len && A[lchild] > A[max]) {
max = lchild;
}
if(rchild < len && A[rchild] > A[max]) {
max = rchild;
}
if(max != i) { //max node have been changed
Sort::Swap(A[max], A[i]);
Sort::Heap_MaxAdjust(A, len, max); //recursive adjust heap node
}
}
//stable algorithm
ElemType *B = new ElemType[ARRAY_LENTH]; //assistant array
void Sort::Merge(ElemType A[], int low, int mid, int high) {
int i,j,k;
for(k = low;k <= high;k++) {
B[k] = A[k]; //copy datas to B
}
for(i=low,j=mid+1,k=low;i<=mid && j<=high;k++) { //2 path merge
if(B[i] <= B[j])
A[k] = B[i++];
else
A[k] = B[j++];
}
while (i <= mid) A[k++] = B[i++];
while (j <= high) A[k++] = B[j++];
}
void Sort::MergeSort(ElemType A[], int low, int high) {
if(low < high) {
int mid = (low + high) / 2;
Sort::MergeSort(A, low, mid);
Sort::MergeSort(A, mid+1, high); //recursive merge
Sort::Merge(A, low, mid, high);
}
}
主函数测试main.cpp:
#include <iostream>
#include "sort.h"
int main() {
ElemType array[ARRAY_LENTH] = {49,38,65,97,76,13,27,49,55,4};//initial array
std::cout << "before sort:\n";
for (int i = 0; i < ARRAY_LENTH; ++i) {
std::cout << array[i] << "--";
}
std::cout << std::endl;
// Sort::Direct_Insert_Sort(array, ARRAY_LENTH);
// Sort::Binary_Insert_Sort(array, ARRAY_LENTH);
// Sort::Shell_Sort(array, ARRAY_LENTH);
// Sort::Bubble_Sort(array, ARRAY_LENTH);
// Sort::Quick_Sort(array, 0, ARRAY_LENTH-1);
// Sort::Select_Sort(array, ARRAY_LENTH);
// Sort::Heap_Sort(array, ARRAY_LENTH);
Sort::MergeSort(array, 0, ARRAY_LENTH-1);
std::cout << "after sort:\n";
for (int i = 0; i < ARRAY_LENTH; ++i) {
std::cout << array[i] << "--";
}
std::cout << std::endl;
return 0;
}
测试结果:
以上均为个人学习心得,如有错误,请不吝赐教~
THE END