Just checked back, thanks for info. I am new to C coding but I understand what you mean. Even though the result was okay, what's more important is that the coding is efficient. Thanks again.
Printable View
Just checked back, thanks for info. I am new to C coding but I understand what you mean. Even though the result was okay, what's more important is that the coding is efficient. Thanks again.
For 25 strings, bubble sort is fine.
Be sure to dump it in favor of a better sorting algorithm, if your quantity of strings gets above 100, however.
Bubble sort is notoriously inefficient sorting a medium or large quantity of anything.
What do you say about:-
about this pseudocode? Won't it be efficient?Code:string sort(L)
{
//make list of pairs (i,str[i])
//bucket sort pairs by str[i]
//bucket sort pairs by i (giving lists chars[i])
//bucket sort strings by length
// i = max length
// L = empty
while (i > 0)
{
//concatenate strings of length = i before start of L
//distribute into buckets by chars in position i
//coalesce by concatenating buckets in chars[i]
// i--
}
//concatenate list of empty strings at start of L
//return L
}
"Bucket sort" can be used effectively in sorting, but the details here seem to suggest multiple bucket sorts, and string concatenates. That concatenation raises a real doubt, but no sorting algorithm should be fairly judged without a trial on the same kind of data you intend for it to sort.Code:string sort(L)
{
//make list of pairs (i,str[i])
//bucket sort pairs by str[i]
//bucket sort pairs by i (giving lists chars[i])
//bucket sort strings by length
// i = max length
// L = empty
while (i > 0)
{
//concatenate strings of length = i before start of L
//distribute into buckets by chars in position i
//coalesce by concatenating buckets in chars[i]
// i--
}
//concatenate list of empty strings at start of L
//return L
}
IMO, it's hard to beat a good implementation of either of these three algorithms, for any kind of sorting **:
1) Quicksort, where only pointers or indices are moved, optimized with Insertion sort for the smaller sub arrays. (If the data is in nearly sorted order, then Insertion sort or merge sort is preferred as well.)
2) Multi-merge sort.
3) Radix sort.
I'm more familiar with Quicksort and Merge sort, (minus the multi-part) :D
Any version of these three algorithms will surely be faster than Bubble sort, on any job with 100 or more "things" that need to be sorted. Oddly, for small jobs, less than 15 say), Bubble sort is quite competitive. In general though, my small and quick sorter preference is Substitution sort:
It's in the same class as Bubble sort, (and thus not good for a large amount of anything, but for less than 100 things, it's good, and always faster than Bubble sort. Also, it's easier (in my mind, at least), so memorizing it has come in very handy, many times.Code:for(i=0;i<MAX-1;i++) {
for(j=i+1;j<MAX;j++) {
if(A[i] > A[j]) {
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
For a relatively easy, but also powerful sorter, Shell sort is good, as is Flash Sort, and Comb 11, Flash and Comb are long and too difficult to be practical, imo. Shell is good, but not up with the three I mentioned above, and you have to get the offset sequence just right - a bit of a pain if you don't work with it a lot.
I tend to use these three exclusively:
1) Substitution sort, for small quantities
2) Quicksort for huge quantities
3) Mergesort for data that has lots of sorted sub sequences, in it.
Here's a funny for you, but true: The most common sorting job, is to sort data that is already sorted. :p :p
** Except "counting sort", which is insanely fast, and leaves everything else in the dust, IF (big if), it can be used with your data. (Usually, it can't be).
Quite right :-0Quote:
Here's a funny for you, but true: The most common sorting job, is to sort data that is already sorted.