I've been asked to create an insertion sort for an array. My program ended differently from the teacher's, but it sorts the array. However, I'm curious if it truly counts as a proper insertion sort. Also, at the end of the sorting, some random numbers appear. I would appreciate some help with this.
#include <iostream>
#define size 6
using namespace std;
void insertion(int v[size]){
int i,temp;
for(i=0;i<size;i++){
while(v[i+1] < v[i]){
temp = v[i+1];
v[i+1] = v[i];
v[i] = temp;
printArr(v);
i = 0;
}
}
}
int main(){
int vet[size] = {34,12,15,21,5,88};
printArr(vet);
insertion(vet);
return 0;
}
Here is the output, one line at a time:
34,12,15,21,5,88,
12,34,15,21,5,88,
12,15,34,21,5,88,
12,15,21,34,5,88,
12,15,21,5,34,88,
12,15,5,21,34,88,
12,5,15,21,34,88,
5,12,15,21,34,88,
5,12,15,21,34,44,
Notice the 44 at the end there. I don't know why it's there since the code works nicely up until the end.
Edit: Fixed a damn typo. My PC turns any lowercase i into uppercase, just forgot to adjust it, but it's not wrong in code.