用冒泡排序实现快速排序

  • 题目:实现bubble_sort,可以排序各种类型数据。
  • 程序如下:
  • #include
    #include
    #include
    typedef struct Stu
    {
               char name[10];
               int age;
               float score;
    }Stu,* pstu;
    int Compare_int(const void * elem1, const void * elem2)
    {
               return *(int *)elem1 - *( int*)elem2 ;
    }
    int Compare_Str(const void * elem1, const void * elem2)
    {
               return strcmp(*(char **)elem1 ,*( char**)elem2 );
    }
    int Compare_Stu(const void * elem1, const void * elem2)
    {
               return ((pstu )elem1)->age - ((( pstu)elem2 )->age);
    }
    void swap(char *buf1, char*buf2 , size_t width)
    {
               int i = 0;
               for (i = 0; i < (int )width; i++)
              {
                        int tmp = buf1 [i];
                        buf1[i] = buf2 [i];
                        buf2[i] = tmp;
              }
    }
    void bubble_sort(void *base, size_t sz , size_t width, int (*cmp)( const void *elem1, const void *elem2))
    {
               int i = 0;
               for (i = 0; i < (int )sz - 1; i++)
              {
                        int j = 0;
                        for (j = 0; j < (int )sz - 1 - i; j++)
                       {
                                  if (cmp ((char*) base + j*width , (char*) base + (j + 1)*width)>0)
                                 {
                                          swap(( char*)base + j*width, ( char*)base + (j + 1)*width, width );
                                 }
                       }
              }
    }
    void Printf_int()
    {
               int arr[] = { 2, 4, 6, 8, 1, 3, 5, 7 };
               int i = 0;
               int sz = sizeof (arr) / sizeof(arr[0]);
              bubble_sort(arr, sz, sizeof(int ), Compare_int);
               for (i = 0; i < sz; i++)
              {
                       printf( "%d\n", arr[i]);
              }
    }
    void Printf_Str()
    {
               int i = 0;
               char *arr[] = { "aaaa" , "dddd", "bbbb", "cccc" };
               int sz = sizeof (arr) / sizeof(arr[0]);
              bubble_sort(arr, sz, sizeof(char *), Compare_Str);
               for (i = 0; i < sz; i++)
              {
                       printf( "%s\n", arr[i]);
              }
    }
    void Printf_Stu()
    {
               int i = 0;
               struct Stu stu[] = { { "zhangsan", 18, 78.0f }, { "lisi", 19, 65.0f }, { "wangwu", 18, 99.8f } };
               int sz = sizeof (stu) / sizeof(stu[0]);
              bubble_sort(stu, sz, sizeof(Stu ), Compare_Stu);
               for (i = 0; i < sz; i++)
              {
                       printf( "name:%s age:%d score:%f\n" , stu[i].name, stu[i].age, stu[i].score);
              }
    }
    int main()
    {
              Printf_int();
              printf( "-------------\n");
              Printf_Str();
              printf( "-------------\n");
              Printf_Stu();
              system( "pause");
               return 0;
    }
    
    运行结果如下:
  • 用冒泡排序实现快速排序_第1张图片

你可能感兴趣的:(C语言基础,C)