Commit 14bf6ec5 authored by saeed's avatar saeed
Browse files

sort is added

parent 4dcef063
......@@ -6,6 +6,7 @@
#include"strcomp.h"
#include"splitstring.h"
#include"strtof.h"
#include"sort.h"
#define N (4096)
char response[N] = {0};
......@@ -137,28 +138,41 @@ int main(int argc,char *argv[])
//for (int i=(ir+3); i < (numc-1);++i){
// printf("%c",content[i]);
//}
printf("\n");
//printf("\n");
printf("%ld\n",numc - ir - 2);
//printf("%ld\n",numc - ir - 2);
char *st = &content[ir+3];
int icom = 0, iicom = 0;
char **cout =split(st, ',', &icom);
char **ccout = NULL;
int *atime = (int *)malloc(sizeof(int)*icom);
float *adata = (float *)malloc(sizeof(float)*icom);
for (int i=0; i < (icom); ++i){
ccout = split(cout[i], ':', &iicom);
rtrim(ccout[2],'}');
ltrim(ccout[0],'\"');
rtrim(ccout[0],'\"');
//printf("%d\t%s\t%f\t%f\n",stoi(ccout[0]),ccout[2],atof(ccout[2]),stof(ccout[2]));
printf("%d\t%f\n",stoi(ccout[0]),stof(ccout[2]));
//printf("%d\t%f\n",stoi(ccout[0]) ,stof(ccout[2]));
atime[i] = stoi(ccout[0]);
adata[i] = stof(ccout[2]);
free(ccout);
}
free(content);
free(cout);
sortfull(atime,adata,icom);
for (int i=0; i < (icom); ++i){
printf("%d\t%f\n",atime[i] ,adata[i]);
}
free(atime);
free(adata);
//printf("%s",content);
......
#ifndef SORT_H
#define SORT_H
void sortpart(int *x, float *z, int ia, int ib){
int N = ib -ia +1;
int *ind = malloc(sizeof(int) *N);
int *save = malloc(sizeof(int) *N);
int *y = malloc(sizeof(int) *N);
float *yz = malloc(sizeof(float)*N);
for (int i=0; i < N; ++i){
ind[i] = -1;
save[i] = -1;
}
int zm = 0;
int im = 0;
int ni = 0;
zm = x[0];
for(int i=ia; i <= ib;++i){
if (x[i] <= zm){
zm = x[i];
im = i;
}
}
ind[im] = im;
save[ni] = im;
++ni;
for (int j=(ia+1); j <= ib; ++j){
for (int i=ia; i <= ib; ++i){
if (ind[i-ia] == -1){
zm = x[i];
im = i;
break;
}
}
for(int i=ia; i <= ib;++i){
if ((x[i] <= zm) && (ind[i-ia] == -1)){
zm = x[i];
im = i;
}
}
ind[im] = im;
save[ni] = im;
++ni;
}
for (int i=ia; i <= ib; ++i){
y[i-ia] = x[save[i-ia]];
yz[i-ia] = z[save[i-ia]];
}
for (int i=ia; i <= ib; ++i){
x[i] = y[i-ia];
z[i] = yz[i-ia];
}
free(ind);
free(save);
free(y);
free(yz);
}
void sortfull(int *x, float *z, int N){
int *ind = malloc(sizeof(int) *N);
int *save = malloc(sizeof(int) *N);
int *y = malloc(sizeof(int) *N);
float *yz = malloc(sizeof(float)*N);
for (int i=0; i < N; ++i){
ind[i] = -1;
save[i] = -1;
}
int zm = 0;
int im = 0;
int ni = 0;
zm = x[0];
for(int i=0; i < N;++i){
if (x[i] <= zm){
zm = x[i];
im = i;
}
}
ind[im] = im;
save[ni] = im;
++ni;
for (int j=1; j < N; ++j){
for (int i=0; i < N; ++i){
if (ind[i] == -1){
zm = x[i];
im = i;
break;
}
}
for(int i=0; i < N;++i){
if ((x[i] <= zm) && (ind[i] == -1)){
zm = x[i];
im = i;
}
}
ind[im] = im;
save[ni] = im;
++ni;
}
for (int i=0; i < N; ++i){
y[i] = x[save[i]];
yz[i] = z[save[i]];
}
for (int i=0; i < N; ++i){
x[i] = y[i];
z[i] = yz[i];
}
free(ind);
free(save);
free(y);
free(yz);
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment