Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added insertion sort, C++. #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions insertion_sorting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;

void insertion(int[],int);
void showData(int[],int);

main(){
int size = 40,counter = 0;
int array[size]; //Size can be changed, just to prove it works
cout << "************FILLING ARRAY************" << endl;
for(int i=size;i>0;i--){ //Worst case, as it is ordered backwards
array[counter]= i;
cout << "Array, position " << counter << ": " << array[counter] << endl;
counter++;
}
cout << "************FINISHED FILLING, STARTING SORTING************" << endl;
insertion(array,size);
}

void insertion(int data[], int size){
int aux=0,j=0;
for(int i=1;i<=size;i++){
j=i;
aux=data[i];
while(j>0 && aux<data[j-1]){
data[j] = data[j-1];
j--;
}
data[j] = aux;
}
showData(data,size);
}

void showData(int array[],int size){
for(int i=0;i<size;i++){
cout << "Element " << (i+1) << ": " << array[i] << endl;
}
}