You are on page 1of 4

C:\Users\Little_Chick\Desktop\Desktop\sort UTF8.

cpp

Saturday, June 18, 2011 11:08 PM

#include #include #include #include

<stdio.h> <stdlib.h> <string.h> <math.h>

/*-------------------------------------------------------*/ void quicksort(void* a, int n, int recsize, int (*compare)(const void*, const void*)); void mergesort(void* a, int n, int recsize, int (*compare)(const void*, const void*)); void* binarysearch(const void* key, const void* a, int n, int recsize, int (*compare)(const void *, const void*)); void swap(void* a, void* b, int recsize); /*-------------------------------------------------------*/

//Comparison Functions /*-------------------------------------------------------*/ int int_decrease(const void* a, const void* b); int abs_int_increase(const void* a, const void* b); /*-------------------------------------------------------*/

int main(void) { printf("Nhap so luong phan tu: "); int n, i; scanf("%d", &n); printf("Nhap cac phan tu:\n"); int* a = (int*)malloc(n * sizeof(int)); for(i = 0; i < n; i++) scanf("%d", &a[i]); quicksort(a, n, sizeof(a[0]), int_decrease); printf("Sau khi sap xep:\n"); for(i = 0; i < n; i++) printf("%6d", a[i]); putchar('\n'); printf("Nhap phan tu de tim: "); int k; scanf("%d", &k); void* b = bsearch(&k, a, n, sizeof(a[0]), int_decrease); if(b != NULL) printf("Tim thay du lieu o vi tri %d\n", (int*)b - a); else printf("Khong tim thay du lieu.\n"); free(a); return 0; } //Comparison Functions cung cp cho quicksort(), mergesort() v binarysearch(). //Gi tr tr v. // > 0: a > b. // = 0: a = b.
-1-

C:\Users\Little_Chick\Desktop\Desktop\sort UTF8.cpp

Saturday, June 18, 2011 11:08 PM

// < 0: a < b. /*-------------------------------------------------------*/ int int_decrease(const void* a, const void* b) { return (*(int*)b - *(int*)a); } //Nu a < b => Gi tr tr v > 0 => Sp xp gim dn. int abs_int_increase(const void* a, const void* b) { return (abs(*(int*)a) - abs(*(int*)b)); } //Nu |a| > |b| => Gi tr tr v > 0 => Sp xp tng dn theo tr tuyt i. /*-------------------------------------------------------*/

//Sp xp mng cc phn t thuc 1 kiu d liu bt k (int, char**, struct, v.v...) bng thut ton Quick Sort. //a: con tr n phn t u tin ca mng (Base Address). //n: s lng phn t trong mng (Number of Elements). //recsize: kch thc tng phn t, tnh bng byte (Element Width). //compare: con tr n hm cha cch so snh (Comparison Function). void quicksort(void* a, int n, int recsize, int (*compare)(const void*, const void*)) { if(n <= 1) return; int left(0), right(n-1); int mid = left + (right - left) / 2; void* pivot = malloc(recsize); memcpy(pivot, (char*)a + mid * recsize, recsize); while(left <= right) { while(compare(pivot, (void*)((char*)a + left * recsize)) > 0) ++left; while(compare((void*)((char*)a + right * recsize), pivot) > 0) --right; if(left <= right) { if(left < right) swap((void*)((char*)a + left*recsize), (void*)((char*)a + right*recsize), recsize); ++left; --right; } } free(pivot); quicksort(a, right + 1, recsize, compare); quicksort((void*)((char*)a + left*recsize), n - left, recsize, compare); }

//Sp xp mng cc phn t thuc 1 kiu d liu bt k (int, char**, struct, v.v...) bng thut ton Merge Sort. //a: con tr n phn t u tin ca mng (Base Address).
-2-

C:\Users\Little_Chick\Desktop\Desktop\sort UTF8.cpp

Saturday, June 18, 2011 11:08 PM

//n: s lng phn t trong mng (Number of Elements). //recsize: kch thc tng phn t, tnh bng byte (Element Width). //compare: con tr n hm cha cch so snh (Comparison Function). void mergesort(void* a, int n, int recsize, int (*compare)(const void*, const void*)) { if(n == 1) return; int l = n / 2; int r = n - l; void* b = malloc(l * recsize); void* c = malloc(r * recsize); memcpy(b, a, l * recsize); memcpy(c, (void*)((char*)a + l * recsize), r * recsize); mergesort(b, l, recsize, compare); mergesort(c, r, recsize, compare); int i(0), j(0); n = 0; while(i < l && j < r) { if(compare((void*)((char*)b + i * recsize), (void*)((char*)c + j * recsize)) > 0) memcpy((void*)((char*)a + n++ * recsize), (void*)((char*)c + j++ * recsize), recsize ); else memcpy((void*)((char*)a + n++ * recsize), (void*)((char*)b + i++ * recsize), recsize ); } while(i < l) memcpy((void*)((char*)a + n++ * recsize), (void*)((char*)b + i++ * recsize), recsize); while(j < r) memcpy((void*)((char*)a + n++ * recsize), (void*)((char*)c + j++ * recsize), recsize); free(b); free(c); } //Tm kim d liu trong 1 mng c kiu d liu bt k ( c sp xp trc ) bng thut ton Binary Search (Tm kim nh phn). //Khi tm kim thnh cng, hm tr v con tr n phn t trong mng. //Nu tht bi, tr v NULL. // //key: con tr n d liu cn tm (Object to Search for). //a: con tr n phn t u tin ca mng (Base Address) //n: s lng phn t trong mng (Number of Elements) //recsize: kch thc tng phn t, tnh bng byte (Element Width) //compare: con tr n hm cha cch so snh (Comparison Function) void* binarysearch(const void* key, const void* a, int n, int recsize, int (*compare)(const void *, const void*)) { int left(0), mid(0), right(n-1);
-3-

C:\Users\Little_Chick\Desktop\Desktop\sort UTF8.cpp

Saturday, June 18, 2011 11:08 PM

while(left <= right) { mid = left + (right - left) / 2; if(!compare(key, (void*)((char*)a + mid * recsize))) return (void*)((char*)a + mid * recsize); if(compare(key, (void*)((char*)a + mid * recsize)) > 0) left = mid + 1; else if(compare(key, (void*)((char*)a + mid * recsize)) < 0) right = mid - 1; } return NULL; } //Swap 2 phn t, mi phn t c kch thc "recsize" byte. void swap(void* a, void* b, int recsize) { void* temp = malloc(recsize); memcpy(temp, a, recsize); memcpy(a, b, recsize); memcpy(b, temp, recsize); free(temp); }

-4-

You might also like