Insertion
Insertion sort an array of values.
Controller: CodeCogs
Contents
Interface
C++
Insertion
template<class T> voidinsertion( | T* | array | |
unsigned int | n | ) |
Example 1
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <codecogs/computing/sort/insertion.h> int main() { double vals[25]; int n=25; srand((unsigned int) time(NULL)); for (int i=0; i<n; i++) vals[i]=((double) n*rand())/RAND_MAX; printf("\nArray to be sorted:\n"); for (int i=0; i<n; i++) printf("%3.0f ", vals[i]); Computing::Sort::insertion<double>(vals, n); printf("\n Sorted array:\n"); for (int i=0; i<n; i++) printf("%3.0f ", vals[i]); printf("\n"); return 0; }
Output:Array to be sorted: 21 10 20 20 23 5 8 19 7 14 12 16 9 13 24 23 16 18 4 15 0 6 3 20 4 Sorted array: 0 3 4 4 5 6 7 8 9 10 12 13 14 15 16 16 18 19 20 20 20 21 23 23 24
Note
- array indexes start at 0
Parameters
array the array of values to be sorted n the number of items in the array
Authors
- James Warren (July 2005)
Source Code
Source code is available when you agree to a GP Licence or buy a Commercial Licence.
Not a member, then Register with CodeCogs. Already a Member, then Login.