Insertion Sort Visualizer

Insertion Sort

Insertion Sort builds the sorted array one element at a time. it takes each element and inserts it into its correct position in the already sorted part.

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




        

Practice

Level Up