对于普通的递增排序,使用常见的排序算法即可解决,对于较为复杂的排序,可以先重载结构体的 < 运算符,然后使用sort函数进行排序。
sort会根据<的规则进行递增排序,要得到递减序列,只需要将<重载成相反的返回值。
用法:
①引入<algorithm>头文件。
②对于内置类型,直接使用sort(<start address>,<end address>)即可得到递增序列,对于自定义类型或者复杂类型,重载<运算符。
需要注意的是sort函数的入口的end address是不包含的,例如一个数组table有5个元素,应该传入table和table+5
下面附上学习数据结构排序一章所收获的排序算法,他们的统一接口为X_sort(int* A , int N);
#include<iostream> #include<algorithm> using namespace std; #define cutoff 100 void Bubble_Sort(int* A, int N){ bool flag = false; for (int P = N - 1; P >= 0; P--){ flag = false; for (int i = 0; i < P; i++){ if (*(A + i)>*(A + i + 1)){ flag = true; int temp = *(A + i); *(A + i) = *(A + i + 1); *(A + i + 1) = temp; } } if (flag == false) break; } } void Insert_Sort(int*A, int N){ for (int P = 1; P < N; P++){ int temp = *(A + P); int i = 0; for (i = P; i >= 1 && *(A + i - 1)>temp; i--){ *(A + i) = *(A + i - 1); } *(A + i) = temp; } } void Shell_Sort(int*A, int N){ for (int D = N / 2; D > 0; D /= 2){ for (int P = D; P < N; P += D){ int temp = *(A + P); int i = 0; for (i = P; i >= D&&*(A + i - D)>temp; i -= D){ *(A + i) = *(A + i - D); } *(A + i) = temp; } } } //Heap Sort use the maxHeap,so it needs the PercDown(bigger up version) void PercDown(int*A, int i, int N){ int Child; int temp; for (temp = *(A + i); (2 * i + 1) < N; i = Child){ Child = 2 * i + 1; if ((Child != N - 1) && (*(A + Child + 1) > *(A + Child))){ Child += 1; } if (temp < *(A + Child)){ *(A + i) = *(A + Child); }else break; } *(A + i) = temp; } void Heap_Sort(int*A, int N){ for (int i = N / 2; i >= 0; i--){ PercDown(A, i, N); } for (int i = N - 1; i > 0; i--){ int temp = *(A + 0); *(A + 0) = *(A + i); *(A + i) = temp; PercDown(A, 0, i); } } void Merge(int* A, int* TmpA, int L, int R, int RightEnd){ int LeftEnd = R - 1; int Tmp = L; int NumElements = RightEnd - L + 1; while (L <= LeftEnd&&R <= RightEnd){ if (*(A + L) < *(A + R)){ *(TmpA + Tmp++) = *(A + L++); } else{ *(TmpA + Tmp++) = *(A + R++); } } while (L <= LeftEnd) *(TmpA + Tmp++) = *(A + L++); while (R <= RightEnd) *(TmpA + Tmp++) = *(A + R++); for (int i = 0; i < NumElements; i++, RightEnd--){ *(A + RightEnd) = *(TmpA + RightEnd); } } void M_Sort(int* A, int* TmpA, int L, int RightEnd){ int Center; if (L < RightEnd){ Center = (L + RightEnd) / 2; M_Sort(A, TmpA, L, Center); M_Sort(A, TmpA, Center + 1, RightEnd); Merge(A, TmpA, L, Center + 1, RightEnd); } } void Merge_Sort(int* A, int N){ int* TmpA = (int*)malloc(N*sizeof(int)); if (TmpA != NULL){ M_Sort(A, TmpA, 0, N - 1); } } void Swap(int* p1, int* p2){ int temp = *p2; *p2 = *p1; *p1 = temp; } int Median3(int* A, int left, int right){ int center = (left + right) / 2; if (*(A + left) > *(A + center)) Swap(A + left, A + center); if (*(A + left) > *(A + right)) Swap(A + left, A + right); if (*(A + center) > *(A + right)) Swap(A + center, A + right); Swap(A + center, A + right - 1); return *(A + right - 1); } void Quicksort(int* A, int left, int right){ if (cutoff <= right - left){ int Pivot = Median3(A, left, right); int i = left; int j = right - 1; while (1){ while (A[++i] < Pivot); while (A[--j] > Pivot); if (i < j) Swap(A + i, A + j); else break; } Swap(A + i, A + right - 1); Quicksort(A, left, i - 1); Quicksort(A, i + 1, right); } else{ Insert_Sort(A + left, right - left + 1); } } void Quick_sort(int* A, int N){ Quicksort(A, 0, N - 1); } void Print_table(int* A, int N){ for (int i = 0; i < N; i++){ if (i == N - 1){ printf("%d ", *(A + i)); } else{ printf("%d ", *(A + i)); } } }其中有的算法:
Bubble_Sort
Insert_Sort
Shell_Sort
Heap_Sort
Merge_Sort
Quick_Sort