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