Skip to content

Commit

Permalink
Update InsertionSort.cpp (Asiatik#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanu-29 authored and tstreamDOTh committed Oct 31, 2018
1 parent 724a3ba commit 761dc79
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions Sorting/Insertion Sort/C++/InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
// Insertion sort algorithm implemented in C++

#include <iostream>
#include<conio.h>
#include<stdlib.h>

#define MAX_SIZE 5

using namespace std;

// Insertion sort function

void insertionSort(int array[],int size){
int current;
int i,j;
for(i=0;i<size;i++){
current=array[i];
for(j=i;j>0 && array[j-1] > current;j--){
array[j]=array[j-1];
}
array[j]=current;
}
void insertion(int[]);

int main() {
int arr_sort[MAX_SIZE], i;

cout << "Simple C++ Insertion Sort Example - Array and Functions\n";
cout << "\nEnter " << MAX_SIZE << " Elements for Sorting : " << endl;
for (i = 0; i < MAX_SIZE; i++)
cin >> arr_sort[i];

cout << "\nYour Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << arr_sort[i];
}

insertion(arr_sort);
getch();
}

// Main function to perform sorting

int main(){
int i;
int array_size;
cout<< "Enter the size of the array to be sorted: ";
cin>> array_size;
int array[array_size];
cout<<"Enter the elements of the array to be sorted: ";
for(i=0;i<array_size;i++){
cin>>array[i];
void insertion(int fn_arr[]) {
int i, j, a, t;
for (i = 1; i < MAX_SIZE; i++) {
t = fn_arr[i];
j = i - 1;

while (j >= 0 && fn_arr[j] > t) {
fn_arr[j + 1] = fn_arr[j];
j = j - 1;
}
fn_arr[j + 1] = t;

cout << "\nIteration : " << i;
for (a = 0; a < MAX_SIZE; a++) {
cout << "\t" << fn_arr[a];
}
}
insertionSort(array,array_size);
cout<<"Sorted array is:\n";
for(i=0;i<array_size;i++){
cout<<array[i]<<"\n";

cout << "\n\nSorted Data :";
for (i = 0; i < MAX_SIZE; i++) {
cout << "\t" << fn_arr[i];
}
return 0;
}
}

0 comments on commit 761dc79

Please sign in to comment.