--quicksort implementation of C-language generic programming

Source: Internet
Author: User

1 #ifndef _genericquicksort_h_2 #define_genericquicksort_h_3 voidGeneric_swap (void* PA,void* PB,inttypesize);4 voidGeneric_qsort (void* PA,intElemsize,inttypesize,5                             int(*CMP) (void*,void*));6 #endif
generic_quick_sort.h
1#include <stdlib.h>2#include <string.h>3#include <stdio.h>4#include"generic_quick_sort.h"5 6 Static voidQuick_sort ();7 voidGeneric_swap (void* PA,void* PB,inttypesize)8 {9     void* Ptmp =malloc(typesize);Ten memcpy (ptmp,pa,typesize); One memcpy (pa,pb,typesize); A memcpy (pb,ptmp,typesize); -      Free(ptmp); - } the  - voidGeneric_qsort (void*PA,intElemsize,inttypesize, -                             int(*CMP) (void*,void*)) - { +     Char*PAA = (Char*) PA; -Quick_sort (PAA,0, elemsize-1, typesize,cmp); + } A  at Static voidQuick_sort (Char*PAA,intBegintEndinttypesize, -                             int(*CMP) (void*,void*)) - {     -     if(Beg >=end) -         return; -     intMid = (beg+end)/2; inG_swap (PAA + end * typesize, PAA + mid *typesize, typesize); -     Char*border = paa + END *typesize; to     inti =Beg; +     intj =Beg; -      for(; J < End; J + +) the     { *         if(CMP (paa+j*typesize, border) <0){ $G_swap (Paa+j*typesize, paa+i*typesize,typesize);Panax Notoginsengi++; -         } the     } +G_swap (Paa+j*typesize, paa+i*typesize,typesize); AQuick_sort (paa,beg,i-1, typesize,cmp); theQuick_sort (paa,i+1, end,typesize,cmp); +}
generic_quick_sort.c
1#include <stdlib.h>2#include <stdio.h>3#include <string.h>4#include"generic_quick_sort.h"5 #definePrint_student (Stu)6printf"name =%-10s, score =%-.2f\n", Stu.name, Stu.score)7 8 enum{len=Ten};9typedefstructstudent{Ten     CharName[len]; One     Doublescore; A }student; -  - intCmp_stu (void*a,void*b); the  - intMain () - {     -Student stu[5] = { +{"Linda",76.14}, -{"Angel",84.41}, +{"Jim",98.25}, A{"Trump",34.98}, at{"Walle", -} -     }; -Generic_qsort (&stu,5,sizeof(student), cmp_stu); -      for(inti =0; I <5; i++){ - print_student (Stu[i]); -     } in     return 0; - } to  + intCmp_stu (void*a,void*b) - { theStudent * PA = (Student *) A; *Student * PB = (Student *) b; $     if(Pa->score < pb->score)Panax Notoginseng         return-1; -     Else if(Pa->score > pb->score) the         return 1; +     Else return 0; A}
Main

--quicksort implementation of C-language generic programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.