💩

programierds

algorithms / sorting

Insertion Sort
Step by Step

Build the sorted array one item at a time by inserting each element into its correct position.

C Implementation Line: -
1int arr[] = {12, 11, 13, 5, 6};
2int n = 5;
3for (int i = 1; i < n; i++) {
4  int key = arr[i];
5  int j = i - 1;
6  while (j >= 0 && arr[j] > key) {
7    arr[j + 1] = arr[j];
8    j = j - 1;
9  }
10  arr[j + 1] = key;
11}
i -
key -
j -
Array Visualization
Click "Start" to begin.
Paso 0 / 0
← Back to all presentations