Skip to content

Instantly share code, notes, and snippets.

@muhammedeminoglu
Created May 26, 2017 19:01
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/3684af48214c909eda8d56597f04b3d1 to your computer and use it in GitHub Desktop.
Save muhammedeminoglu/3684af48214c909eda8d56597f04b3d1 to your computer and use it in GitHub Desktop.
Selection Sort C Code. I think it is clean and understandable...
#include <stdio.h>
#define SIZE 10000
int myArray[SIZE - 1];
void selectionSort(int x[])
{
int i, j;
int key;
for( i = 0; i < SIZE; i++)
{
key = i;
for(j = i + 1; j < SIZE; j++)
{
if(myArray[key] > myArray[j])
{
key = j;
}
}
swapf(i, 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();
selectionSort(myArray);
printSorted();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment