Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created May 26, 2017 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muhammedeminoglu/8d613baa3a9a9b79befe63c7593a8815 to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/8d613baa3a9a9b79befe63c7593a8815 to your computer and use it in GitHub Desktop.
Insertion Sort C Code with an Array
#include <stdio.h>
#define SIZE 10000
int myArray[SIZE - 1];
void insertionSort(int x[])
{
int i, j;
int key;
for(i = 1; i < SIZE; i++)
{
key = myArray[i];
j = i - 1;
while(j >= 0 && myArray[j] > key)
{
myArray[j+1] = myArray[j];
j--;
}
myArray[j+1] = key;
}
}
void printSorted()
{
int i;
for( i = 0; i < SIZE - 1; i++)
{
printf("%d\n", myArray[i]);
}
}
void swapf(int x, int y)
{
int temp;
temp = myArray[x];
myArray[x] = myArray[y];
myArray[y] = temp;
}
void init()
{
int i;
for( i = 0; i < SIZE - 1; i++)
{
myArray[i] = rand()%10000;
}
}
int main()
{
init();
insertionSort(myArray);
printSorted();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment