Search
 
SCRIPT & CODE EXAMPLE
 

C

insertion sort c

// C program for insertion sort
#include <math.h>
#include <stdio.h>
 
/* Function to sort an array using insertion sort*/
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;
    }
}
 
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
    int i;
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("
");
}
 
/* Driver program to test insertion sort */
int main()
{
    int arr[] = { 12, 11, 13, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    insertionSort(arr, n);
    printArray(arr, n);
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: turn a char into an int in c 
C :: c# for loop decrement 
C :: c syntax 
C :: downgrade chrome to previous stable version in linux 
C :: multiplication table in c using array 
C :: initialize array in c with 0 
C :: Counting Sort C 
C :: gcc option to show rules of makefile 
C :: extract substring after certain character in flutter 
C :: c code to add two numbers 
C :: multiplication table in c 
C :: epoch time in c 
C :: search in gz file 
C :: fwrite in c 
C :: simple bootstrap form example 
C :: check if pid exists c 
C :: c bubble sort 
C :: converting strings to numbers in c 
C :: c median of an array 
C :: terraform fargate cpu 
C :: chevront de vlavier 
C :: c structure with pointer 
C :: string in c and how it works 
C :: finding characters in string 
C :: loops questions on c 
C :: c read file from command line 
C :: setw in c 
C :: C++ How to use enums for flags? 
C :: c %d 
C :: convert c to python online 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =