C: A Fastest Array Sorting, faster than bubble sort ?
Hi,
On sorting an array of array, the simplest and well known one is bubble sort. Would you mind proposing a code that would do similar things but faster? I dont sort all, just the first chars, herewith: many thanks Code:
|
|
|
Quote:
"You could just swap pointers datalistfile[i] and datalistfile[i+1]" |
Quote:
Code:
char *datalistfile[datalistfile_max_numberitems], *t; EDIT: Looking at your original code, I see that you have written Code:
for(j=6 ; (j>=0) ; j--) Much better to use Code:
... |
Quicksort is my personal favorite. A concept of it has been discussed well on the book I bought which is A Book on C 4th Edition. I even made an implementation of it for Bash here which now I used in my scripts.
|
Quote:
Kevin Barry |
Quote:
---- Add ---- And seeing the comparison of it with mergesort, I see mergesort as a more conservative and less aggressive algorithm, which isn't my taste. I think it was also discussed in the book A Book on C along with Quicksort and if it was I must have had the same reasons why I picked Quicksort over it. About the problem of possibly having non-identical elements that compare as equal, the fault would already be about the comparer function and not really Quicksort itself. And thanks to this discussion I found this interesting algorithm: http://en.wikipedia.org/wiki/Introsort. |
Quote:
Kevin Barry |
Quote:
Bucket Sort or Counting Sort can be made to run in linear time since they don't rely on comparisons. Counting sort is also stable since it doesn't change the order of two elements with the same key. Radix Sort is a method of sorting large integers or strings by doing a counting sort digit by digit starting (usually) with the least significant digit first. http://www.jimloy.com/computer/radix.htm shows a simple demonstration of how a radix sort works. |
Quote:
If an initial mapping is still required to make that work then perhaps you're correct. Quote:
|
Quote:
Yet again if I would consider the balance between complexity of implementation and speed, with natural elements I would go for Quicksort. Mergesort seems to be something to be considered with objects and linked lists. |
Dr. Knuth wrote an entire book called Sorting and Searching.
|
Really? No one mentioned that QuickSort is O(n^2) yet?[1] In the worst case it degenerates to select sort. That's why I much prefer Heap Sort (if I don't care if sorting is stable, but even if I do, I would probably just number the elements and sort by double key).
[1] Unless one uses median of medians approach, but then it's just ugly for my taste. |
Quote:
He does a poor job of describing the methods. He focuses on the analysis of the performance. For a mathematician planning to invent a new sorting method, that focus on analysis is an important starting point. For a software engineer planning to select and code a sorting method, you want a clear description of the method and a clear description of the performance bottom line reached from all that analysis. Knuth's pseudo code implementations are all lame and his performance analysis math may be better than I would do, but it is just a distraction from algorithm plus bottom line performance that I would want. Before the internet, Knuth was the best you could easily find in an ordinary library. After the internet existed, it was time to forget Knuth. |
All times are GMT -5. The time now is 08:09 PM. |