diff --git a/Sorting/Insertion Sort.cpp b/Sorting/Insertion Sort.cpp new file mode 100644 index 0000000..457c280 --- /dev/null +++ b/Sorting/Insertion Sort.cpp @@ -0,0 +1,36 @@ +/* Insertion Sort is a sorting algorithm. +Time Complexity of Insertion Sort in worst and average cases is O(n^2) and in best case is O(n). */ +#include +using namespace std; +void iSort(int b[], int n) +{ + int i,v,j; + for (i = 1; i < n; i++) + { + v = b[i]; + j = i - 1; + while (j >= 0 && b[j] > v) + { + b[j + 1] = b[j]; + j = j - 1; + } + b[j + 1] = v; + } +} +void ans(int b[], int n) +{ + int i; + for (i = 0; i < n; i++) + { + cout << b[i] << " "; + } + cout <<"\n"; +} +int main() +{ + int b[] = { 7, 3, 5, 1, 9, 2 }; + int n = sizeof(b) / sizeof(b[0]); + iSort(b, n); + ans(b, n); + return 0; +}