fork download
  1. //insertion sort
  2. #include<iostream>
  3. #include <ctime>
  4. using namespace std;
  5. class insertion{
  6. public:
  7. insertion(){} //constructor
  8. void sort(int a[], int n) { //insertion sort function
  9. for (int i = 1; i < n; i++) {
  10. int value = a[i];
  11. int index = i;
  12. while (index > 0 && a[index - 1] > value) {
  13. a[index] = a[index - 1];
  14. index=index-1;
  15. }
  16. a[index] = value;
  17. }
  18. }
  19.  
  20. //display function
  21. void display(int a[], int n) {
  22. for (int i = 0; i < n; i++) {
  23. cout << a[i] << endl;
  24. }
  25. }
  26. };
  27. int main(){
  28. insertion ins;
  29. int a[10];
  30. int n = 10;
  31. for (int i = 0; i < n; i++) {
  32. a[i] = 10 - i;
  33. }
  34.  
  35. ins.sort(a, n);
  36. ins.display(a, n);
  37.  
  38. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1
2
3
4
5
6
7
8
9
10