2.InsertionSort - C++

less than 1 minute read

2.InsertionSort - C++





#include <iostream>

void printArr (int arr[],int size){
    int i;
    for (i=0; i<size; i++){
        std::cout <<  arr[i] << ", ";
    }
    std::cout << "\n";
}

void insertionSort(int arr[], int size) {
    int i, j,key;
    for (i = 1; i < size; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0&&arr[j]>key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}



int main(int argc, const char * argv[]) {
    int size = 10;
    int arr[10] = { 3,2,3,1,5,6,7,8,9,10 };
    
    printArr(arr, size);
    insertionSort(arr, size);
    printArr(arr, size);
   
    
}





Leave a comment