Skip to content

Instantly share code, notes, and snippets.

@mutahirqureshi
Created April 8, 2014 00:25
Show Gist options
  • Save mutahirqureshi/10076886 to your computer and use it in GitHub Desktop.
Save mutahirqureshi/10076886 to your computer and use it in GitHub Desktop.
Sorting puzzle
#include <stdio.h>
/**
* Compile: gcc -o sort sort.c
*
* Usage: ./sort integer1, integer2 ...
*/
int main (int argc, char *argv[]) {
// array that will store sorted input
int copy[argc - 1];
int i;
int negIndex = 0; // index to put current negative int
int posIndex = argc - 2; // last index of copy array
int curNum;
for (i = 1; i < argc; i++) {
curNum = atoi(argv[i]);
if (curNum < 0) {
copy[negIndex] = curNum;
negIndex++;
} else if (curNum > 0) {
copy[posIndex] = curNum;
posIndex--;
}
}
for (i = negIndex; i <= posIndex; i++) {
copy[i] = 0;
}
for (i = 0; i < argc - 1; i++) {
curNum = copy[i];
printf("%d\n", curNum);
}
return 0;
}
@mutahirqureshi
Copy link
Author

this solution is for integer input but we could easily modify this to accept double values. Same concept applies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment