Skip to content

Instantly share code, notes, and snippets.

Created November 21, 2010 16:01
Show Gist options
  • Save anonymous/708844 to your computer and use it in GitHub Desktop.
Save anonymous/708844 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
int laenge(int *A) {
int i;
int j;
for(i=49, j=0;i>=0;i=i-1) {
if (A[i]!=0) break;
else j=i;
}
return j;
}
int kmedian(int k, int *M) {
int i;
int A[50];
int B[50];
int P[1];
int a;
int b;
int l;
int m;
P[0]=M[0];
if (laenge(M) == 1) {
return P[0];
}
else
for(i=0, l=0, m=0; i<laenge(M); i++){
if (M[i]<P[0]){
A[l]=M[i];
l=l+1;
}
if (M[i]>P[0]){
B[m]=M[i];
m=m+1;
}
}
a = laenge(A);
b = laenge(B);
if (a==k-1){
return P[0];
}
if (a>k-1) {
for(i=0;i<50;i++) {
M[i]=A[i];
}
return kmedian(k, M);
}
else {
return kmedian((k-a-1),B);
}
}
int main(){
int M[50] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
printf("%i", kmedian(7, M));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment