Created
May 26, 2017 19:38
-
-
Save muhammedeminoglu/8d613baa3a9a9b79befe63c7593a8815 to your computer and use it in GitHub Desktop.
Insertion Sort C Code with an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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