From 3ee6cd72d7755837231b901596ff56163bbb24eb Mon Sep 17 00:00:00 2001 From: Mayukh Sharma <86524409+mayukhsharma@users.noreply.github.com> Date: Sat, 2 Oct 2021 14:54:57 +0530 Subject: [PATCH] added insertion sort code file. I have provided the code of INSERTION SORT in the C++ language. --- insertionsort_code.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 insertionsort_code.cpp diff --git a/insertionsort_code.cpp b/insertionsort_code.cpp new file mode 100644 index 000000000..f3bde96f6 --- /dev/null +++ b/insertionsort_code.cpp @@ -0,0 +1,34 @@ +//INSERTION SORT=insert an element from unsorted array to its correct position in sorted array +//eg: think that 1st element is in sorted array so start checking from the 2nd element as unsorted array, then compare 2nd and 1st element, so on and if they are in correct order we'll shift the larger one to right otherwise we'll put the smaller one to its correct position. + + +#include +using namespace std; + +int main(){ + int n; + cout<<"enter the size of array: "; + cin>>n; + int arr[n]; + cout<<"enter the values of array: "; + for(int i=0; i>arr[i]; + } + for(int i=0;icurrent && j>=0){ + arr[j+1]=arr[j]; + j--; + } + arr[j+1]=current; + } + for(int i=0;i