Advertisement
vencinachev

BubbleSortUni

Mar 23rd, 2022
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <time.h>
  6.  
  7. void swap(int* num1, int* num2){
  8.     int temp = *num1;
  9.     *num1 = *num2;
  10.     *num2 = temp;
  11. }
  12.  
  13. void swapuni(void *ptr1, void *ptr2, size_t size){
  14.     uint8_t temp[size];
  15.     memmove(temp, ptr2, size);
  16.     memmove(ptr2, ptr1, size);
  17.     memmove(ptr1, temp, size);
  18. }
  19.  
  20. void bubbleSort(void* arr, size_t n, size_t size, int(*cmp)(const void*,const void*)){
  21.    for (int i = 0; i < n - 1; i++){  
  22.        for (int j = 0; j < (n-i-1) * size; j += size){
  23.            if (cmp(arr + j , arr + j + size) > 0){
  24.                swapuni(arr + j, arr + j + size, size);
  25.            }
  26.        }
  27.    }
  28. }
  29.  
  30. #define COUNT 10
  31.  
  32. int compare(const void* a, const void* b){
  33.     return *(int*)a - *(int*)b;
  34. }
  35.  
  36. int cmpInt(int a, int b){
  37.     return b - a;
  38. }
  39.  
  40. int main(){
  41.    /* char str[100];
  42.     strcpy(str, "Strypes is software company!");
  43.     memset(str + 5, 'Z', 10);
  44.     printf("String: %s\n", str);
  45.     int nums[] = { 10, 20, 30, 50 };
  46.     memset(nums, 0b11111111, 8);
  47.     printf("%u\n", nums[1]);*/
  48.     srand(time(NULL));
  49.     int nums[COUNT];
  50.     for (int i = 0; i < COUNT; i++){
  51.         nums[i] = rand() % 100;
  52.     }
  53.     bubbleSort(nums, COUNT, sizeof(int), compare);
  54.     //qsort(nums, COUNT, sizeof(int), compare);
  55.     for (int i = 0; i < COUNT; i++){
  56.         printf("%d ", nums[i]);
  57.     }
  58.     putchar('\n');
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement