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
|
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stddef.h>
#define INIT_ARR_SIZE 100
int char_comparator(void *p1, void *p2);
void char_swap(void *p1, void *p2);
void insertionSort(void *a, int start, int end, int (*comparator)(void *, void *), void (*swap)(void *, void *), size_t atomic);
int binarySearch(void *arr, void *item, int left, int right, int (*comparator)(void *, void *), void (*swap)(void *, void *), size_t atomic);
void print_char_array(char **a, int size);
int main(int argc, char const *argv[])
{
int char_index = 0;
char **char_arr = (char **)malloc(INIT_ARR_SIZE * sizeof(char *));
char *line = (char *)malloc(100 * sizeof(char));
while (fscanf(stdin, "%[^\n]", line) != EOF)
{
getc(stdin);
if (strcmp(line, ".END.") == 0) break;
char_arr[char_index] = strcpy((char *)calloc(strlen(line) + 1, sizeof(char)), line);
char_index++;
}
printf("\n------------BEFORE----------------\n");
print_char_array(char_arr, char_index);
insertionSort(char_arr, 0, char_index, char_comparator, char_swap, sizeof(char *));
printf("\n------------AFTER----------------\n");
print_char_array(char_arr, char_index);
for (int i = 0; i < char_index; i++){
free(char_arr[i]);
}
free(char_arr);
// Now do it again with an array of integers
void print_int_array(int *array, int size);
int int_comparator(void *p1, void *p2);
void int_swap(void *p1, void *p2);
int arr[100];
int sz{0};
while (scanf("%d", &arr[sz]) == 1) {
++sz;
}
printf("\n------------BEFORE----------------\n");
print_int_array(arr, sz);
insertionSort(arr, 0, sz, int_comparator, int_swap, sizeof(int));
printf("\n------------AFTER----------------\n");
print_int_array(arr, sz);
return 0;
}
void print_int_array(int *array, int size)
{
for (int i=0; i<size; ++i) {
printf("%d\n", array[i]);
}
}
int int_comparator(void *p1, void *p2)
{
int *i1 = (int*)p1;
int *i2 = (int*)p2;
if (*i1 < *i2) return -1;
if (*i1 > *i2) return 1;
return 0;
}
void int_swap(void *p1, void *p2)
{
int *i1 = (int*)p1;
int *i2 = (int*)p2;
int tmp = *i1;
*i1 = *i2;
*i2 = tmp;
}
// INSETION SORT DEFINITION
void insertionSort(void *p, int start, int end, int (*comparator)(void *, void *), void (*swap)(void *, void *), size_t atomic)
{
int i, loc, j;
char *a = (char*)p; // for pointer arithmentic
for (i = (start + 1); i < end; ++i)
{
j = i - 1;
void *selected = a + (i * atomic);
loc = binarySearch(a, selected, start, i, comparator, swap, atomic);
while (j >= loc)
{
swap(a + ((j + 1) * atomic), a + (j * atomic));
j--;
}
}
}
// Search for item in p between left and right. "Right" is one index
// past the last valid item. Returns the location of "item" if found,
// or the location just past where it should go.
int binarySearch(void *p, void *item, int left, int right, int (*comparator)(void *, void *), void (*swap)(void *, void *), size_t atomic)
{
char *arr = (char*)p; // for pointer arithmetic
fflush(stdout);
if (right <= left)
return left;
int mid = (left + right) / 2;
int comp = comparator(item, arr + (mid * atomic));
if (comp == 0) {
return mid;
} else if (comp > 0) {
return binarySearch(arr, item, mid + 1, right, comparator, swap, atomic);
} else {
return binarySearch(arr, item, left, mid, comparator, swap, atomic);
}
}
//COMPARATOR
int char_comparator(void *p1, void *p2)
{
return strcmp(((char **)p1)[0], ((char **)p2)[0]);
}
//SWAP FUNCTION
void char_swap(void *p1, void *p2)
{
char **s1 = (char**)p1;
char **s2 = (char**)p2;
char *t = *s1;
*s1 = *s2;
*s2 = t;
}
/* PRINTING FUNCTIONS */
void print_char_array(char **a, int size)
{
int i;
for (i = 0; i < size; i++)
printf("pointer: %p, word: %s\n", &a[i], a[i]);
printf("\n");
}
|