insertion sort

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; } int main(void) { printf("Hello World!\n"); int k,key, arr[10] = {2,4,393,4433,55,67,986}; for(int i = 0 ; i < 6; i++){ k = i+1; key = arr[k]; while(k >= 0){ if (key<arr[k]) swap(&arr[k],&arr[k+1]); k--; } } for(int i=0;i<=7-1;i++) { printf("\t %d ",arr[i]); } return 0; } /* #include <stdio.h> void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; // Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; // Print the current state of the array printf("Pass %d: ", i); for (int k = 0; k < n; k++) { printf("%d ", arr[k]); } printf("\n"); } } int main() { int arr[] = {7, 2, 4, 1, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("Original Array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n\n"); insertionSort(arr, n); printf("\nSorted Array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } */
Editor Settings
Theme
Key bindings
Full width
Lines