Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
633 B
2
Indexable
Never
void insertionSort(int arr[], int size) {
    for (int i = 1; i < size; ++i) {
        int key = arr[i];
        int j = i - 1;
        
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        
        arr[j + 1] = key;
    }
}


#include <iostream>

int main() {
    int numbers[] = {5, 2, 8, 9, 1, 3};
    int size = sizeof(numbers) / sizeof(numbers[0]);
    
    insertionSort(numbers, size);
    
    std::cout << "Posortowane liczby: ";
    for (int i = 0; i < size; ++i) {
        std::cout << numbers[i] << " ";
    }
    
    return 0;
}