I need to sort the content (alphabetically using strcmp()
of an array of strings, but I am not allowed to use the function qsort()
. With this code I managed to sort numerical values, but I am having a hard time adapting it in order to sort strings.
/* A utility function to swap two elements */
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap_string(char c[63], char d[63]){
char temp[63];
strcpy(temp, c);
strcpy(c, d);
strcpy(d, temp);
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; /* pivot */
int i = (low - 1); /* Index of smaller element */
for (j = low; j <= high- 1; j++)
{
/* If current element is smaller than the pivot */
if (arr[j] < pivot)
{
i++; /* increment index of smaller element */
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
/* Separately sort elements before */
/* partition and after partition */
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
First, you just need to create a swap function for string, not need for int
void swap(char ** a, char ** b) {
char * t = *a;
*a = *b;
*b = t;
}
Because, you sort an array of string, so using input ** arr
instead of arr[]
Here, the partition function:
int partition (char ** arr, int low, int high) {
char * pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++) {
// If current element is smaller than the pivot
if (strcmp(arr[j],pivot) < 0) {
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
And, quickSort function:
void quickSort(char **arr, int low, int high) {
if (low < high) {
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
Then the main function for test:
int main(){
char *a = "abc";
char *b = "cdf";
swap(&a, &b);
printf("a = %s, b= %s\n", a, b);
char * data[10]={"something2", "something0", "something1", "something6", "something8", "something4", "something5","something7", "something3", "something9"};
quickSort(data, 0, 9);
for(int i = 0; i < 10; i++) {
printf("%s, ", data[i]);
}
return 0;
}
If you want exactly an array producer[100][63]
. You can change the declaration of the function.
In this example, i use an array data[10][63]
for testing. In fact, you can use 100 instead of 10.
Swap function:
void swap(char a[63], char b[63]) {
char t[63];
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}
Then, partition function:
int partition (char arr[10][63], int low, int high) {
char * pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++) {
// If current element is smaller than the pivot
if (strcmp(arr[j],pivot) < 0) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
Finally, the quickSort function:
void quickSort(char arr[10][63], int low, int high) {
if (low < high) {
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
The main for test:
int main(){
char data[10][63]={"something2", "something0", "something1", "something6", "something8", "something4", "something5","something7", "something3", "something9"};
quickSort(data, 0, 9);
for(int i = 0; i < 10; i++) {
printf("%s, ", data[i]);
}
return 0;
}