Skip to content

Instantly share code, notes, and snippets.

@poying
Created September 26, 2012 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poying/3788268 to your computer and use it in GitHub Desktop.
Save poying/3788268 to your computer and use it in GitHub Desktop.
3.27
#include <stdio.h>
#define SIZE 10
#define ANS 2
int in(int i, const int *h){
int c = SIZE;
while(c--){
//printf("> %d\n", *h);
if(*h == i){
return 1;
}
h++;
}
return 0;
}
void sort(int *h, int size){
int counter = 0, i, tmp, index;
while(size - counter){
for(i = counter; i > 0; i--){
if(h[i] > h[i-1]){
//printf("$ %d\t%d\t%d\n", i, h[i], h[i-1]);
tmp = h[i-1];
h[i-1] = h[i];
h[i] = tmp;
}else{
break;
}
}
//printf("====\n");
counter++;
}
}
int main(void){
int input, history[SIZE] = {0}, tmp, counter = SIZE;
while(counter--){
printf(" %d\n> ", SIZE - counter);
scanf("%d", &input);
if(in(input, history)){
counter++;
}else{
history[SIZE - counter - 1] = input;
}
}
printf("\nBefore Sort \n------------\n");
for(tmp = 0; tmp < SIZE; tmp++){
printf("%d\n", history[tmp]);
}
sort(history, SIZE);
printf("\nAfter Sort \n------------\n");
for(tmp = 0; tmp < SIZE; tmp++){
printf("%d\n", history[tmp]);
}
printf("==================\n");
for(tmp = 0; tmp < ANS; tmp++){
printf("%d\n", history[tmp]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment