Järgnevalt on välja toodud andmetüübist sõltumatu mullsordi lahendus. Koodinäite eesmärk on tuua välja seoseid qsort() funktsiooni väljakutsega ning näidata millal ja miks läheb vaja võrdlusfunktsiooni.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
/** * File: agnostic_bubble.c * Author: Risto Heinsar * Created: 02.03.2025 * Modified 06.03.2025 * * Description: Example shows data type agnostic bubble sort. Used too show * similarities with qsort() function parameters. */ #include <stdio.h> #include <stdbool.h> #include <string.h> #define STR_LEN 64 typedef unsigned char byte; struct Student { char name[STR_LEN]; int score; }; int ComparInt(const void *pA, const void *pB); int ComparStudentScore(const void *pA, const void *pB); void BubbleSort(void *data, size_t n, size_t size, int(*Compar)(const void *a, const void *b)); void PrintIntArr(int *data, int n); void PrintStudents(struct Student *data, int n); int main(void) { int vals[] = {1, 9, 2, 8, -5, 14}; int nVals = sizeof(vals) / sizeof(int); struct Student students[] = {{"Mari", 75}, {"Kati", 50}, {"Mihkel", 85}, {"Priit", 45}, {"Tiina", 68}}; int nStudents = sizeof(students) / sizeof(struct Student); BubbleSort(vals, (size_t)nVals, sizeof(int), ComparInt); BubbleSort(students, (size_t)nStudents, sizeof(struct Student), ComparStudentScore); PrintIntArr(vals, nVals); PrintStudents(students, nStudents); return 0; } /** * Description: Data type agnostic bubble sort * * Parameters: data - array with items to sort * n - number of items * size - size in bytes for each item * Compar - comparison function to identify order of items * * Return: none */ void BubbleSort(void *data, size_t n, size_t size, int(*Compar)(const void *a, const void *b)) { /* End correction to avoid overflow in comparison */ n = n - 1; while (1) { bool sorted = true; for (size_t i = 0; i < n; i++) { /* Calculate pointers for current and next member */ void *pA = data + i * size; void *pB = data + (i + 1) * size; /* Check if items are in the wrong order */ if (Compar(pA, pB) > 0) { /* Swap the two items */ byte temp[size]; memcpy(temp, pA, size); memcpy(pA, pB, size); memcpy(pB, temp, size); sorted = false; } } /* No swaps, array sorted */ if (sorted) return; n--; } } /** * Description: Compares integers based on value * * Parameters: pA - pointer to first integer * pB - pointer to second integer * * Return: < 0 pA has higher value, comes first * = 0 if order doesn't matter * > 0 pB has higher value, comes first */ int ComparInt(const void *pA, const void *pB) { return *(int *)pA - *(int *)pB; } /** * Description: Compares students based on score, descending order * * Parameters: pA - first student struct pointer * pB - pointer to the second student struct * * Return: < 0 pA has higher score, comes first * = 0 if order doesn't matter * > 0 pB has higher score, comes first */ int ComparStudentScore(const void *pA, const void *pB) { return ((struct Student *)pB)->score - ((struct Student *)pA)->score; } /** * Description: Prints the integer array on screen * * Parameters: data - integer array * n - number of integers * * Return: none */ void PrintIntArr(int *data, int n) { printf("Sorted array (asc):\n"); for (int i = 0; i < n; i++) { printf("%d ", data[i]); } printf("\n\n"); } /** * Description: Prints the student struct array on screen * * Parameters: data - student struct array * n - number of students * * Return: none */ void PrintStudents(struct Student *data, int n) { printf("Top students:\n"); for (int i = 0; i < n; i++) { printf("%15s %3d\n", data[i].name, data[i].score); } printf("\n"); } |