思路仅供参考(参考浙大陈越姥姥数据结构)
#include
#include
#include
using namespace std;
// 冒泡排序
void bubble_sort(int arr[], int n) {
for(int i=n-1; i>=0; i--) {
bool flag = true;
for(int j=0; j<i; j++) {
if(arr[j]>arr[j+1]) {
flag = false;
swap(arr[j], arr[j+1]);
}
}
if(flag)
break;
}
}
// 插入排序
void insertion_sort(int arr[], int n) {
for(int i=1; i<n; i++) {
int tmp = arr[i];
int j;
for(j=i; j>=1 && tmp<arr[j-1]; j--)
arr[j] = arr[j-1];
arr[j] = tmp;
}
}
// 选择排序
int find_min(int arr[], int i, int right) {
int ans_pos;
int min_elem = INT_MAX;
for(; i<=right; i++) {
if(arr[i] < min_elem) {
min_elem = arr[i];
ans_pos = i;
}
}
return ans_pos;
}
void select_sort(int arr[], int n) {
for(int i=0; i<n-1; i++) {
int min_pos = find_min(arr, i, n-1);
swap(arr[i], arr[min_pos]);
}
}
// 希尔排序
void shell_sort(int arr[], int n) {
int h = 1;
while(h <= n/3)
h = h*3 + 1;
while(h>=1) {
for(int i=h; i<n; i++) {
int tmp = arr[i];
int j;
for(j=i; j>=h && tmp<arr[j-h]; j-=h)
arr[j] = arr[j-h];
arr[j] = tmp;
}
h /=3;
}
}
// ***************************************************
// 堆排序
void adjust(int arr[], int n, int i) { // n是元素个数!!,非下标
int child, parent;
int max_elem = arr[i];
for(parent=i; parent*2+1 <= n-1; parent=child) {
child = parent*2+1;
if(child+1 <= n-1 && arr[child+1] > arr[child])
child++;
if(max_elem > arr[child])
break;
else
arr[parent] = arr[child];
}
arr[parent] = max_elem;
}
void heap_sort(int arr[], int n) {
for(int i=n/2; i>=0; i--)
adjust(arr, n, i);
for(int i=n-1; i>0; i--) {
swap(arr[0], arr[i]); // 把最大[0号]的交换到数组最后
adjust(arr, i, 0); // 再以[0]为根节点,进行heap的调整(此时i=n-1,剩余n-1个元素)
}
}
// 快速排序
int median3(int arr[], int L, int R) {
int mid = L + (R - L)/2;
if(arr[L] > arr[mid])
swap(arr[L], arr[mid]);
if(arr[L] > arr[R])
swap(arr[L], arr[R]);
if(arr[mid] > arr[R])
swap(arr[mid], arr[R]);
swap(arr[mid], arr[R - 1]);
return arr[R - 1];
}
void quicksort(int arr[], int L, int R) {
if(L > R)
return;
// int mid = L + (R-L)/2;
int pivot = median3(arr, L, R);
int i = L;
int j = R-1;
while(i<j) {
while(arr[++i] < pivot) {}
while(arr[--j] > pivot) {}
if(i < j)
swap(arr[i], arr[j]);
}
swap(arr[i], arr[R-1]);
quicksort(arr, L, i-1);
quicksort(arr, i+1, R);
}
void quick_sort(int arr[], int n) {
quicksort(arr, 0, n-1);
}
// 归并排序
void merge(int arr[], int tmp[], int L, int R, int rightEnd) {
int leftEnd = R - 1;
int num_elem = rightEnd - L + 1;
int left = L;
while(L <= leftEnd && R <= rightEnd) {
if(arr[L] < arr[R])
tmp[left++] = arr[L++];
if(arr[L] > arr[R])
tmp[left++] = arr[R++];
}
while(L <= leftEnd)
tmp[left++] = arr[L++];
while(R <= rightEnd)
tmp[left++] = arr[R++];
for(int i=0; i<num_elem; i++) {
arr[rightEnd] = tmp[rightEnd];
rightEnd--;
}
}
void msort(int arr[], int tmp[], int L, int rightEnd) {
if(L < rightEnd) {
int mid = L + (rightEnd - L)/2;
msort(arr, tmp, L, mid);
msort(arr, tmp, mid+1, rightEnd);
merge(arr, tmp, L, mid+1, rightEnd);
}
}
void merge_sort(int arr[], int n) {
int tmp[n] = {};
msort(arr, tmp, 0, n-1);
}
// 基数排序
#define radix 10
#define maxdigit 4
typedef struct node* Node;
struct node{
int val;
Node next;
};
struct BU{
Node head;
Node tail;
};
int get_digit(int num, int D) {
int ans;
for(int i=1; i<=D; i++) {
ans = num %10;
num = num/10;
}
return ans;
}
void radix_sort(int arr[], int n) {
// 初始化List
Node List = NULL;
for(int i=0; i<n; i++) {
Node tmp = new node;
tmp->val = arr[i];
tmp->next = List;
List = tmp;
}
// 初始化桶
BU B[radix];
for(int i=0; i<radix; i++) {
B[i].head = NULL;
B[i].tail = NULL;
}
// digit循环开始
for(int D = 1; D<=maxdigit; D++) {
// 入桶
for(int i=0; i<n; i++) {
int num = List->val;
int bu_pos = get_digit(num, D);
Node tmp = List;
List = List->next;
tmp->next = NULL;
if(B[bu_pos].head==NULL) {
B[bu_pos].head = tmp;
B[bu_pos].tail = tmp;
}
else {
B[bu_pos].tail->next = tmp;
B[bu_pos].tail = tmp;
}
}
cout << "for debug" << endl;
// 出桶->List
List = NULL;
for(int i=radix-1; i>=0; i--) {
if(B[i].head) {
B[i].tail->next = List;
List = B[i].head;
B[i].head = NULL;
B[i].tail = NULL;
}
}
}
for(int i=0; i<n; i++) {
arr[i] = List->val;
List = List->next;
}
}
int main()
{
int arr[] = {46,63,88,24,17,9,721,32,55};
// bubble_sort(arr, 9);
// insertion_sort(arr, 9);
// select_sort(arr, 9);
// shell_sort(arr, 9);
// heap_sort(arr ,9);
// quick_sort(arr, 9);
// merge_sort(arr, 9);
radix_sort(arr, 9);
for(int i=0; i<9; i++)
cout << arr[i] << ' ';
return 0;
}