#include <iostream>using namespace Std;template <class t>void Swap (T & Left, T & right) {T temp = Left left = right; right = temp;} /* Simple selection sort */template <class t>void selectsort (T * arr, int n) {for (int i = 0; i < n-1; ++i) {int S Mall = i; for (int j = i + 1; j < n; ++j) if (Arr[j] < Arr[small]) small = j; Swap (Arr[i], Arr[small]); }}/* Direct Insert Sort */template <class t>void insertsort (t * arr, int n) {for (int i = 1; i < n; ++i) {T val = Arr[i]; int j = i; while (J > 0 && val < arr[j-1]) {arr[j] = arr[j-1]; --j; } Arr[j] = val; }}/* Bubble sort */template <class t>void bubblesort (T * arr, int n) {int i = n-1; while (i > 0) {int last = 0; for (int j = 0; J < i; ++j) {if (Arr[j] > arr[j + 1]) {Swap (Arr[j], arr[j + 1]); last = j; }} i = last; }}/* Quick Sort */template <class t>void qSort (T * arr, int left, int.) {if (left < right) {int i = Le FT, J = right + 1; do {does ++i; while (Arr[i] < arr[left]); Do--j; while (Arr[j] > Arr[left]); if (i < j) Swap (Arr[i], arr[j]); } while (I < j); Swap (Arr[left], arr[j]); QSort (arr, left, j-1); QSort (arr, j + 1, right); }}template <class t>void QuickSort (T * arr, int n) {qSort (arr, 0, n-1);} /* Two-way merge sort */template <class t>void merge (T * arr, int ls, int le, int rs, int re) {T * temp = new T[re-ls + 1] ; int PL = ls, PR = rs, pt = 0; while (PL <= le && pr <= re) {if (ARR[PL] <= ARR[PR]) temp[pt++] = arr[pl++]; else temp[pt++] = arr[pr++]; } while (pl <= le) temp[pt++] = arr[pl++]; while (PR <= re) temp[pt++] = arr[pr++]; for (int i = 0; i < pt; ++i) Arr[ls + i] = temp[i]; delete [] temp;} Template <class t>void mergesort (T * arr, int n) {int ls, le, RS, re; int size = 1; while (size < n) {ls = 0; while (ls + size < n) {le = ls + size-1; rs = ls + size; if (rs + size <= N) re = rs + size-1; else re = n-1; Merge (arr, ls, le, RS, re); ls = re + 1; } size *= 2; }}/* Heap Sort */template <class t>void adjustdown (T * arr, int start, int end) {int child = 2 * start + 1; int val = Arr[start]; while (child <= end) {if (Child < end && Arr[child] < Arr[child + 1]) ++child; if (Val >= arr[child]) break; arr[(CHILD-1)/2] = Arr[child]; Child = 2 * child + 1; } arr[(Child-1)/2] = val;} Template <class t>void heapsort (T * arr, int n) {for (int i = (n-2)/2; I >= 0; -I.) Adjustdown (arr, I, n-1); for (int i = n-1; i > 0; i.) {Swap (arr[0], arr[i]); Adjustdown (arr, 0, i-1); }}int Main () {int arr[10] = {5, 4, 6, 5, 2, 5, 7, 5, 5, 1}; int b = 0; int c = 1; QuickSort (arr, 10); for (int i = 0; i < ++i) cout << arr[i] << ""; cout << Endl;}
sorting algorithm (c + + implementation)