Created
February 4, 2018 16:22
-
-
Save thinkphp/a5c3657ca81ad7575f10c1152fb7c434 to your computer and use it in GitHub Desktop.
combSort using pointer to function for fun.
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" | |
#define FOUT "algsort.out" | |
//pointer to function | |
typedef void (*fnPtr)(int*,int); | |
void combsort(int *p, int n) { | |
float shrinkFactor = 1.3; | |
int i, | |
gap = n, | |
swapped = 0; | |
while(gap > 1 || swapped) { | |
if(gap > 1) gap = gap / shrinkFactor; | |
swapped = 0; | |
for(i = 0; i + gap < n; ++i) { | |
if(p[i] > p[i + gap]) { | |
int tmp = p[ i ]; | |
p[ i ] = p[ i + gap ]; | |
p[ i + gap ] = tmp; | |
swapped = 1; | |
} | |
} | |
} | |
}; | |
int main() { | |
int i, | |
n, *vec; | |
freopen(FIN, "r", stdin); | |
freopen(FOUT, "w", stdout); | |
scanf("%d", &n); | |
vec = (int*)malloc(sizeof(int) * n); | |
for(i = 0; i < n; ++i) scanf("%d", &vec[ i ]); | |
fnPtr sort = combsort; | |
sort(vec, n); | |
for(i = 0; i < n; ++i) printf("%d ", vec[ i ]); | |
free( vec ); | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment