views:

1235

answers:

6

What is the best/fastest way to sort Alphanumeric fields?

A: 

You will find that most development libraries ship with an implementation of the quicksort algorithm, which is often the fastest sorting algorithm. Check out the Wikipedia link here.

senfo
+1  A: 

You don't specify your target language, but whatever it is, it should have reliable, built-in sorting methods, so use one of them! For PHP...

Load into an array and sort($array);

php sort...

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);

foreach ($fruits as $key => $val)
{
    echo "fruits[" . $key . "] = " . $val . "\n";
}

Output:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
Ciaran
+1  A: 

Bubble sort! Just kidding :)

Probably your best bet would be quicksort or mergesort.

Both are O(nlogn) as opposed to bubble sort's O(n^2)

Brian R. Bondy
A: 

In C#, List has .Sort().

In general QuickSort is very fast on many situations but it always depend of the size of the array,

Here is the link

Daok
+1  A: 

The answer to your question is intimately related to some details you haven't provided. The "best/fastest" way depends on how long the fields are, how many you have to sort, how much memory you have available, the relative speeds of disk and memory, the details of what's in the strings, ..., ad nauseam.

Knuth Vol 3 has the details on a wide variety of approaches. I don't recall if he discusses Radix Sorting, but he probably does. If he doesn't, you should look up some references on Radix Sorting. It's only useful in a narrow set of circumstances, but positively flies there. If you've got a small set of short strings, Bubble Sort will perform better than complex sorts on some architectures, due to lower overhead. The C Run Time Library includes a version of Quick Sort because that can be a very efficient algorithm for larger data sets in some circumstances.

Net-net, the answer is "It depends".

+1  A: 

The "best" way depends on a lot of factors:

  1. Do you need to support more than language?
  2. Do you need to support more than one language simultaniously?
  3. Do you need to support languages other than the current Operating System or user language? (ex, web applications)
  4. Do you need to support more than one encoding? (unicode, utf-16le/utf-8, ansi code pages, etc)
  5. Do you need to support long or highly redundant inputs? (where precomputation or compression may speed up sorting operations)
  6. Do you need to support a large number of inputs, ex: million, or billion inputs?
Aaron