0% found this document useful (0 votes)
55 views

Sorting Array of Strings: Void String - Sort (Char Arr, Const Int CNT, Int ( CMP - Func) (Const Char A, Const Char B) )

The document discusses sorting an array of strings according to different comparison strategies. It notes that passing a flag indicating the strategy to the sorting function would require defining new codes/flags for each new strategy. A better approach is to write the sorting function to accept a pointer to the comparison function, allowing different strategies by passing different comparison functions without rewriting the sorting code. It provides an example function signature for a string_sort function that takes the string array, length, and pointer to a comparison function as arguments to implement different sorting criteria. It also lists 4 string comparison functions that implement different sorting orders.

Uploaded by

Sooraj Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Sorting Array of Strings: Void String - Sort (Char Arr, Const Int CNT, Int ( CMP - Func) (Const Char A, Const Char B) )

The document discusses sorting an array of strings according to different comparison strategies. It notes that passing a flag indicating the strategy to the sorting function would require defining new codes/flags for each new strategy. A better approach is to write the sorting function to accept a pointer to the comparison function, allowing different strategies by passing different comparison functions without rewriting the sorting code. It provides an example function signature for a string_sort function that takes the string array, length, and pointer to a comparison function as arguments to implement different sorting criteria. It also lists 4 string comparison functions that implement different sorting orders.

Uploaded by

Sooraj Jha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Sorting Array of

Strings
Depending on certain circumstances, we may want to sort a given array of strings into lexicographically
increasing order or into an order in which the string with the lowest length appears first.
We could make a sorting function and pass the function a flag telling it which comparison strategy to use,
but that would mean that whenever we invented a new comparison strategy, we would have to define a
new code or flag value and rewrite the sorting function.
But, if we observe that the final ordering depends entirely on the behaviour of the comparison function, a
better implementation would be if we write our sorting function to accept a pointer to the function which
we want it to use to compare each pair of strings.
Making it sort in a different order, according to a different comparison strategy, will then not require
rewriting sorting at all, but instead will just involve passing it a pointer to a different comparison function.

Given an array of strings, you need to implement a string_sort function which sorts the strings according
to the following different criterion.

In this problem, you need to implement the function :

void string_sort(char **arr,const int cnt, int (*cmp_func)(const char* a, const char* b)){

To this function, the arguments passed are:

an array of strings :

length of string array:

pointer to the string comparison function:

You also need to implement the four string comparison functions:

1. to sort the strings in lexicographically


non-decreasing order.

2. to sort the strings in


lexicographically non-increasing order.

3. to sort the
strings in non-decreasing order of the number of distinct characters present in them.If two strings
have the same number of distinct characters present in them, then the lexicographically smaller
string should appear first.

4. to sort the strings in non-decreasing order


of their lengths.If two strings have the same length, then the lexicographically smaller string should
appear first.

Input Format

You just need to complete the function and implement the four string comparison
functions.

Constraints

The locked code-stub will check the logic of your code.

Sample Input 0
4
wkue
qoi
sbv
fekls

Sample Output 0

fekls
qoi
sbv
wkue

wkue
sbv
qoi
fekls

qoi
sbv
wkue
fekls

qoi
sbv
wkue
fekls

You might also like