Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
- void swap(void* a, void* b, size_t size) {
- void *tmp = malloc(size);
- if (tmp == NULL) {
- perror("");
- exit(1);
- }
- memcpy(tmp, a, size);
- memcpy(a, b, size);
- memcpy(b, tmp, size);
- free(tmp);
- }
- void bSort(void *arr, size_t n, size_t size, short (*cmp)(const void*, const void*)) {
- bool swapped;
- do {
- swapped = false;
- for (size_t i = 0; i < n - 1; i++) {
- void* a = (char*)arr + i * size;
- void* b = (char*)arr + (i + 1) * size;
- if (cmp(a, b) > 0) {
- swap(a, b, size);
- swapped = true;
- }
- }
- } while (swapped);
- }
- short cmpInts(const void* a, const void* b) {
- const int ai = *(const int*)a;
- const int bi = *(const int*)b;
- //printf("%d %d\n", ai, bi);
- return ai > bi ? 1 : -1;
- }
- #define NMAX 101
- int main(void) {
- int n;
- assert(scanf_s("%d", &n));
- static int arr[NMAX];
- for (int i = 0; i < n; i++) {
- assert(scanf_s("%d", &arr[i]));
- }
- bSort(arr, n, sizeof(int), cmpInts);
- for (int i = 0; i < n; i++) {
- printf("%d ", arr[i]);
- }
- putchar('\n');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement