Previous questions:
I'm currently learning about different sorting algorithms and after reading on the concept of the insertion sort of how it's done I've tried to implement it by myself before seeing how it was implemented in the source from which I'm learning.
This is my implementation:
void insertionSort(int arr[]) { for(int i = 1; i < SIZE; i++) { int value = arr[i]; int j = i; for(; j > 0; j--) { if (value < arr[j - 1]) { arr[j] = arr[j - 1]; } else { break; } } arr[j] = value; } }
Now I think I've implemented it correctly (not talking about efficient or anything just the algorithm itself), but when I saw the way it was implemented in the course I'm seeing I had a bit of concerns about my implementation because they're really different and I just wanted to make sure my implementation is really an insertion sort algorithm implementation and I didn't done anything else.
Here's the implementation by the source I'm learning from:
void insertionSort(int arr[]) { for(int i = 1;, i < SIZE; i++) { int value = arr[i]; int j = i - 1; int done = 0; do { if (arr[j] > value) { arr[j + 1] = arr[j]; j--; if (j < 0) done = 1; } else { done = 1; } } while(!done) arr[j + 1] = value; } }
Is my implementation of insertion sort correct ?
Also I would appreciate if you could compare my implementation to the one made by the source I'm learning from, any efficiency differences or any other thing you think would worth mentioning.