Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int comp_3(const void* a, const void* b)
- {
- if ((*(float*)a ) > (*(float*)b))
- return 1;
- else if ((*(float*)a ) == (*(float*)b))
- return 0;
- else
- return -1;
- }
- int comp_4(const void* a, const void* b)
- {
- if ((*(double*)a ) > (*(double*)b))
- return 1;
- else if ((*(double*)a ) == (*(double*)b))
- return 0;
- else
- return -1;
- }
- int comp_1(const void* a, const void* b)
- {
- if ((*(int*)a ) > (*(int*)b))
- return 1;
- else if ((*(int*)a ) == (*(int*)b))
- return 0;
- else
- return -1;
- }
- int comp_2(const void* a, const void* b)
- {
- return strcmp(*(char**)a, *(char**)b);
- }
- int qsort_1(void* array, size_t size, int N, void* search, int (*cmp)(const void*, const void*))
- {
- int left = 0;
- int right = N;
- int mid = 0;
- while(left < right)
- {
- mid = (right + left) / 2;
- if(cmp ((char*)array + size*mid, search) < 0 )
- {
- left = mid + 1;
- }
- else if (cmp ((char*)array + size*mid, search) == 0)
- {
- return mid;
- }
- else
- right = mid;
- }
- if (left > right)
- return -1;
- }
- int main()
- {
- int arr[7] = {1, 2, 3, 4, 5, 6, 7};
- float arr2[7] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.1};
- double arr3[7] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.1};
- char* Sarr[7] = {"aaa","bbb","ccc","ddd","eee","fff","ggg"};
- int key = 3;
- char key1 = '1';
- float key4 = 5.5;
- double key5 = 7.2;
- printf("(%d)", qsort_1(arr3, sizeof(double), 7, &key5, comp_4));
- //printf("(%d)", qsort_1(arr2, sizeof(float), 7, &key4, comp_3));
- //printf("%d", qsort_1(Sarr, sizeof(char*), 7, &key1, comp_2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement