diff --git a/Algorithms/Insertion_sort.cpp b/Algorithms/Insertion_sort.cpp new file mode 100644 index 0000000..47beca6 --- /dev/null +++ b/Algorithms/Insertion_sort.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +void display(int *array, int size) { + for(int i = 0; i 0 && array[j-1]>key) { + array[j] = array[j-1]; + j--; + } + array[j] = key; //insert in right place + } +} +int main() { + int n; + cout << "Enter the number of elements: "; + cin >> n; + int arr[n]; //create an array with given number of elements + cout << "Enter elements:" << endl; + for(int i = 0; i> arr[i]; + } + cout << "Array before Sorting: "; + display(arr, n); + insertionSort(arr, n); + cout << "Array after Sorting: "; + display(arr, n); +}