In: Computer Science
i have an array of strings, how do i sort them into a binary tree? C Program
#include <stdio.h> #include <stdlib.h> #include <string.h> static int myCompareString(const void* s, const void* g) { return strcmp(*(const char**)s, *(const char**)g); } void sort(const char* a[], int n) { // calling qsort function to sort the array // with the help of Comparator qsort(a, n, sizeof(const char*),myCompareString); }
int main() { const char* a[] = { "hello ", "everyone", "there" };
int n = sizeof(a) / sizeof(a[0]); int i;
// Printing the given names printf("Given array is\n"); for (i = 0; i < n; i++) printf("%d: %s \n", i, a[i]);
// Sorting the given names sort(a, n);
// Printing the sorted names printf("\nSorted array is\n"); for (i = 0; i < n; i++) printf("%d: %s \n", i, a[i]);
return 0; } |