Insertion Sort - Part 1

  • + 0 comments

    Here is my c++ solution, you can watch the explanatoin here : https://youtu.be/xFqneyHR96g

    void printArr(vector<int> arr){
        for(int i = 0; i < arr.size(); i++){cout << arr[i] << " ";}
        cout << endl;
    }
    void insertionSort1(int n, vector<int> arr) {
        int last = arr[arr.size()-1];
        for(int i = arr.size() - 1; i >= 0; i--){
            if(last < arr[i-1]) {
                arr[i] = arr[i-1];
                printArr(arr);
            }
            else{
                arr[i] = last;
                printArr(arr);
                break;
            }
        }
    }