qsort / bsearch

qsort bsearch

include <stdlib.h>

void qsort (void* base, size_t num, size_t size,
            int (*compar)(const void*,const void*));

//第一个参数	数组
//第二个参数 数组大小
//第三个参数 元素大小
//第四个参数 函数
#include <stdlib.h>
#include <stdio.h>

typedef struct {
    char name[30];
    int Chinese;
    int Math;
    int English;
} Student;

Student students[7];

void readData() {
    FILE* file = fopen("mark.txt", "r");
    int i;
    for (i = 0; i < 7; i++) {
        fscanf(file, "%s", students[i].name);
        fscanf(file, "%d", &students[i].Chinese);
        fscanf(file, "%d", &students[i].Math);
        fscanf(file, "%d", &students[i].English);
    }
    fclose(file);
}

void displayData() {
    int i;
    for (i = 0; i < 7; i++) {
        printf("%s\t", students[i].name);
        printf("%d\t", students[i].Chinese);
        printf("%d\t", students[i].Math);
        printf("%d\n", students[i].English);
    }
}
int compare(const void* a, const void* b) {
    Student* pa = (Student *)a;
    Student* pb = (Student *)b;
    int num1 = pa->Chinese;
    int num2 = pb->Chinese;
    return num2 - num1;
}

int main(void) {

    readData();
    qsort(students, 7, sizeof(Student), compare);
    displayData();
    return 0;
}
//mark.txt
Tom 97 68 45
Jerry 100 32 88
Harry 78 88 78
Lucy 87 90 89
Mickey 88 77 66
Peter 59 68 98
Alice 62 73 89
/* !< output */

//按语文成绩排序
Jerry   100     32      88
Tom     97      68      45
Mickey  88      77      66
Lucy    87      90      89
Harry   78      88      78
Alice   62      73      89
Peter   59      68      98

Process returned 0 (0x0)   execution time : 0.006 s
Press any key to continue.
/* qsort example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}

bsearch

void* bsearch (const void* key, const void* base,
               size_t num, size_t size,
               int (*compar)(const void*,const void*));
#include <stdlib.h>
#include <stdio.h>

typedef struct {
    char name[30];
    int Chinese;
    int Math;
    int English;
} Student;

Student students[7];

void readData() {
    FILE* file = fopen("mark.txt", "r");
    int i;
    for (i = 0; i < 7; i++) {
        fscanf(file, "%s", students[i].name);
        fscanf(file, "%d", &students[i].Chinese);
        fscanf(file, "%d", &students[i].Math);
        fscanf(file, "%d", &students[i].English);
    }
    fclose(file);
}

void displayData() {
    int i;
    for (i = 0; i < 7; i++) {
        printf("%s\t", students[i].name);
        printf("%d\t", students[i].Chinese);
        printf("%d\t", students[i].Math);
        printf("%d\t", students[i].English);
        printf("%d\n", students[i].Chinese + students[i].Math + students[i].English);
    }
}
int compare(const void* a, const void* b) {
    Student* pa = (Student *)a;
    Student* pb = (Student *)b;
    int num1 = pa->Chinese + pa->Math + pa->English;
    int num2 = pb->Chinese + pb->Math + pb->English;
    return num2 - num1;
}

int compare2(const void* key, const void* e) {
    int* pNum1 = (int *)key;
    Student* pS = (Student *)e;
    int num1 = *pNum1;
    int num2 = pS->Math + pS->Chinese + pS->English;
    return num2 - num1;
}

int main(void) {

    readData();
    qsort(students, 7, sizeof(Student), compare);
    int key = 224;
    Student* s = bsearch(&key, students, 7, sizeof(Student), compare2);
    printf("%s\n", s->name);
    displayData();
    return 0;
}
/* output */
/* !< 寻找总分为224的人 */
Alice
Lucy    87      90      89      266
Harry   78      88      78      244
Mickey  88      77      66      231
Peter   59      68      98      225
Alice   62      73      89      224
Jerry   100     32      88      220
Tom     97      68      45      210

Process returned 0 (0x0)   execution time : 0.005 s
Press any key to continue.

/* bsearch example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort, bsearch, NULL */

int compareints (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int values[] = { 50, 20, 60, 40, 10, 30 };

int main ()
{
  int * pItem;
  int key = 40;
  qsort (values, 6, sizeof (int), compareints);
  pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
  if (pItem!=NULL)
    printf ("%d is in the array.\n",*pItem);
  else
    printf ("%d is not in the array.\n",key);
  return 0;
}
/* bsearch example with strings */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort, bsearch, NULL */
#include <string.h>     /* strcmp */

char strvalues[][20] = {"some","example","strings","here"};

int main ()
{
  char * pItem;
  char key[20] = "example";

  /* sort elements in array: */
  qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);

  /* search for the key: */
  pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);

  if (pItem!=NULL)
    printf ("%s is in the array.\n",pItem);
  else
    printf ("%s is not in the array.\n",key);
  return 0;
}

reference

http://www.cplusplus.com/info/

posted @ 2019-09-09 21:06  电院院长  阅读(238)  评论(0编辑  收藏  举报