Geometry Nodes: Procedural generation (great for forests/cities).
Practice consistently—each project builds muscle memory! 🎉
User
explain insertion sorting to me and give me c code to implement it
Assistant
Insertion Sort Explained
Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time. It works by iterating through an array and inserting each element into its correct position within the already-sorted portion. Here's a step-by-step breakdown:
How It Works
Start with the second element (index 1).
Compare it with elements to its left.
Shift larger elements to the right to make space.
Insert the current element into its correct position.
Repeat for all elements until the array is sorted.
Example (Sorting [5, 2, 4, 6, 1]):
Pass 1: [2, 5, 4, 6, 1]
Pass 2: [2, 4, 5, 6, 1]
Pass 3: [2, 4, 5, 6, 1] (no change)
Pass 4: [1, 2, 4, 5, 6]
Key Characteristics
Time Complexity:
Best Case: O(n) (already sorted)
Average/Worst Case: O(n²)
Space Complexity: O(1) (in-place sorting).
Stable: Maintains relative order of equal elements.
Efficient for small datasets or nearly-sorted data.
C Implementation
c
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) { // Start from the second element
int key = arr[i]; // Element to insert
int j = i - 1;
// Shift elements larger than key to the right
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // Shift element right
j--;
}
arr[j + 1] = key; // Insert key into correct position
}
}
// Utility function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Driver code
int main() {
int arr[] = {5, 2, 4, 6, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
User
explain insertion sorting to me and give me c code to implement it
Assistant
Insertion Sort Explained Simply
Core Idea: Insertion sort builds a sorted array one element at a time by inserting each new element into its correct position within the already-sorted section.
How It Works:
Start at index 1: Treat the first element as already sorted.
Take next element: Compare it with elements in the sorted section (to its left).
Shift and insert: Shift larger elements right to make space, then insert the current element.
Repeat: Continue until all elements are processed.
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i]; // Current element to insert
int j = i - 1; // Start comparing with left neighbor
// Shift elements greater than key to the right
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key; // Insert key in correct position
}
}
int main() {
int arr[] = {9, 5, 1, 4, 3};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
insertionSort(arr, n);
printf("\nSorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
code
Original array: 9 5 1 4 3
Sorted array: 1 3 4 5 9
Key Characteristics
Time Complexity:
Best Case: O(n) (already sorted)
Worst Case: O(n²) (reverse sorted)
Average Case: O(n²)
Space Complexity: O(1) (in-place sorting)
Stable: Preserves order of equal elements
Adaptive: Faster when input is partially sorted
Use Cases: Small datasets, nearly-sorted data, or as a base for hybrid algorithms (e.g., Timsort). Not ideal for large unsorted arrays due to O(n²) worst-case time.
Output
code
Original array: 5 2 4 6 1
Sorted array: 1 2 4 5 6
Explanation of the Code
Outer Loop (i from 1 to n-1):
Iterates through each unsorted element.
Key Selection:
key = arr[i] is the element to insert into the sorted subarray arr[0..i-1].
Inner Loop (j from i-1 backwards):
Shifts elements > key right until the correct position is found.