Commit 5933b136 by shtaya

qsort implemented

parent 242afdaa
Showing with 11 additions and 23 deletions
...@@ -27,13 +27,12 @@ typedef struct sales ...@@ -27,13 +27,12 @@ typedef struct sales
void readFile(sales_t *); void readFile(sales_t *);
void dispData(sales_t *); void dispData(sales_t *);
void sortData(sales_t *); int cmp(const void *p, const void *q);
int main(void) int main(void)
{ {
int i; int i;
sales_t *data; sales_t *data;
sales_t **pArray;
data = (sales_t*)malloc(sizeof(sales_t)*NFIELD); data = (sales_t*)malloc(sizeof(sales_t)*NFIELD);
if (data == NULL) if (data == NULL)
...@@ -42,27 +41,22 @@ int main(void) ...@@ -42,27 +41,22 @@ int main(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pArray = (sales_t**)malloc(sizeof(sales_t*)*NFIELD); readFile(data);
if (pArray == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
for(i = 0; i < NFIELD; i++)
{
pArray[i] = &data[i];
}
readFile(*pArray); //dispData(data);
dispData(*pArray); qsort(data, NFIELD, sizeof(sales_t), cmp);
sortData(*pArray); //dispData(data);
return 0; return 0;
} }
int cmp(const void *p, const void *q)
{
return ((sales_t*)p)->book_id - ((sales_t*)q)->book_id;
}
void readFile(sales_t *sp) void readFile(sales_t *sp)
{ {
FILE *fp; FILE *fp;
...@@ -91,16 +85,10 @@ void readFile(sales_t *sp) ...@@ -91,16 +85,10 @@ void readFile(sales_t *sp)
void dispData(sales_t *sp) void dispData(sales_t *sp)
{ {
int i; int i;
for(i = 0; i < 100; i++) for(i = 0; i < 500; i++)
{ {
printf("%d,%d\n", (sp+i)->person_id,(sp+i)->book_id); printf("%d,%d\n", (sp+i)->person_id,(sp+i)->book_id);
} }
return; return;
} }
void sortData(sales_t *sp)
{
return;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment