diff --git a/Insertion Sort.cpp b/Insertion Sort.cpp new file mode 100644 index 00000000..cc53a4fc --- /dev/null +++ b/Insertion Sort.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +//Insertion Sort in the ascending order + +/*Performing Insertion Sort - We take first element in the array as sorted sublist +others are unsorted sublist and first element in the unsorted sublist, +we take it as temporary value and we compare it with all elements in the sorted sublist +if temporary value is less than last element in the sorted list we move that last element to +the temporary value with in that we take it as sorted sublist and we insert temp value in the sorted sublist*/ + +int Insertion_Sort(int arr[],int n) +{ + for(int i=1;i=0 && arr[j]>temp) + { + //To move the value which is greater than temp value + arr[j+1] = arr[j]; + + //Decrementing to check all the elements in the sorted sublist + j--; + } + + //If while loop condition(arr[j] > temp) fails then it take till temp as sorted sublist + arr[j+1] = temp; + } + + //Printing all the elements in the array After performing Insertion Sort + cout<<"\nAfter performing Insertion Sort\n"; + for(int j=0;j>size; + + //Array input + for(int i=0;i>arr[i]; + } + + //Printing all the elements in the array Before performing Insertion Sort + cout<<"\nBefore performing Insertion Sort\n"; + for(int j=0;j