Created
February 8, 2018 04:03
-
-
Save thinkphp/a9d847da9f7607973bac9deec86828bd to your computer and use it in GitHub Desktop.
CombSort Algorithm using pointers to functions.
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> | |
#include <malloc.h> | |
#define FIN "algsort.in" | |
typedef int (*Comparer)(int, int); | |
typedef void (*fnPtr)(int*, int, Comparer); | |
int sort_asc(int a, int b) { | |
if(a > b) return 1; | |
else return 0; | |
} | |
void swap(int *a, int *b) { | |
int tmp; | |
tmp = *a; | |
*a = *b; | |
*b = tmp; | |
} | |
void combsort(int *p, int n, Comparer comp) { | |
float shrinkFactor = 1.3; | |
int gap = n, swapped, i; | |
while( gap > 1 || swapped ) { | |
if(gap > 1) gap = gap / shrinkFactor; | |
swapped = 0; | |
for(i = 0; i + gap < n; ++i) { | |
if( comp(p[i], p[i+gap]) > 0 ) { | |
swap(p + i, p + i + gap); | |
swapped = 1; | |
} | |
} | |
} | |
}; | |
int main() { | |
int *v, n, i; | |
const char *FOUT = "algsort.out"; | |
fnPtr sort = combsort; | |
freopen(FIN, "r", stdin); | |
freopen(FOUT, "w", stdout); | |
scanf("%d", &n); | |
v = (int*)malloc( sizeof( int ) * n ); | |
for(i = 0; i < n; ++i) scanf("%d", &v[ i ]); | |
sort(v, n, sort_asc); | |
for(i = 0; i < n; ++i) printf("%d ", v[ i ]); | |
free( v ); | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment