Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <time.h>
- void swap(int* num1, int* num2){
- int temp = *num1;
- *num1 = *num2;
- *num2 = temp;
- }
- void swapuni(void *ptr1, void *ptr2, size_t size){
- uint8_t temp[size];
- memmove(temp, ptr2, size);
- memmove(ptr2, ptr1, size);
- memmove(ptr1, temp, size);
- }
- void bubbleSort(void* arr, size_t n, size_t size, int(*cmp)(const void*,const void*)){
- for (int i = 0; i < n - 1; i++){
- for (int j = 0; j < (n-i-1) * size; j += size){
- if (cmp(arr + j , arr + j + size) > 0){
- swapuni(arr + j, arr + j + size, size);
- }
- }
- }
- }
- #define COUNT 10
- int compare(const void* a, const void* b){
- return *(int*)a - *(int*)b;
- }
- int cmpInt(int a, int b){
- return b - a;
- }
- int main(){
- /* char str[100];
- strcpy(str, "Strypes is software company!");
- memset(str + 5, 'Z', 10);
- printf("String: %s\n", str);
- int nums[] = { 10, 20, 30, 50 };
- memset(nums, 0b11111111, 8);
- printf("%u\n", nums[1]);*/
- srand(time(NULL));
- int nums[COUNT];
- for (int i = 0; i < COUNT; i++){
- nums[i] = rand() % 100;
- }
- bubbleSort(nums, COUNT, sizeof(int), compare);
- //qsort(nums, COUNT, sizeof(int), compare);
- for (int i = 0; i < COUNT; i++){
- printf("%d ", nums[i]);
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement