The following is an ascending and descending sort of inserting sort in C + +
Algorithm from the introduction of algorithms
#include <iostream>using namespace std; void Insertion_sort (int *a, int N) {for (int j = 1; j < N; ++j) {int key = A[j]; int i = j-1; while (i >= 0 && key < A[i]) {a[i + 1] = A[i]; --I; } a[i + 1] = key; }} int main () {int b[] = {4,3,2,1}; Insertion_sort (b,4); for (int i = 0; i < 4; ++i) cout << b[i] << Endl; return 0;} ----------------------------------------------------------------------------
#include <iostream>using namespace std; void Insertion_descsort (int *a, int N) {for (int j = 1; j < N; + + j) {int key = A[j]; int i = j-1; while (i >= 0 && a[i] < key) {a[i + 1] = A[i]; --I; } a[i + 1] = key; }} int main () {int b[] = {1,2,3,4,5}; Insertion_descsort (b, 5); for (int i = 0; i < 5; ++i) cout << b[i] << Endl; return 0;}
Insertion_sort Insert Sort C + + implementation