Insertion sort Check:

Viewed 15

Please go through this code and let me know if this solution is correct for insertion sort algorithm. Do suggest some optimizations too, if possible.

void insertionSorting(vector<int> &v, int n) {
    for(int i=0; i<n-1; i++) {
        if(v[i] > v[i+1])  {
            swap(v[i], v[i+1]);
            for(int j=i; j>=1; j--) {
                if(v[j] < v[j-1]) swap(v[j], v[j-1]);
            }
        } 
    }
    printArray(v);
    return;
}
0 Answers
Related