Good question!! Sorting is probably the most important concept for learning as a promising computer scientist.
In fact, there are many different algorithms for sorting a list.
When you destroy all these algorithms, the most fundamental operation is to compare the two elements in the list that define their “natural order”.
For example, to sort a list of integers, I need a function that tells me, given any two integers X and Y, if X is less than, equal to or greater than Y.
For your strings you will need the same thing: a function that tells you which of the strings has a value of "lesser" or "greater", or whether they are equal.
Traditionally, these comparator functions look something like this:
int CompareStrings(String a, String b) { if (a < b) return -1; else if (a > b) return 1; else return 0; }
I did not consider some details (for example, as you calculate, less or more than b? Clue: iterating over characters), but this is the main skeleton of any comparison function. It returns a value less than zero if the first element is less, and a value greater than zero if the first element is greater, returns zero if the elements have the same value.
But what does this have to do with sorting?
Sort routing will call this function for pairs of items in your list, using the result of the function to figure out how to reorder items in the sorted list. The comparison function defines the "natural order", and the "sorting algorithm" determines the logic of the call and response to the results of the comparison function.
Each algorithm is similar to the strategy of a large picture, guaranteeing the correct sorting of ANY input. Here are a few algorithms that you probably want to know about:
Bubble Sort:
Go through the list by calling the comparison function for all adjacent pairs of elements. Whenever you get a result greater than zero (this means that the first element is larger than the second), replace the two values. Then move on to the next pair. When you get to the end of the list, if you did not need to replace ANY couples, then congratulations, the list is sorted! If you need to perform any swaps, go back to the beginning and start. Repeat this process until there are no more swaps.
NOTE. This is usually not a very efficient way to sort the list, because in the worst case scenario, you may need to scan the entire list as many as N times, for a list with N items.
Merge Sort:
This is one of the most popular separation and control algorithms for sorting a list. The basic idea is that if you have two lists already sorted, it's easy to merge them. Just start at the beginning of each list and remove the first element in which the list has the smallest initial value. Repeat this process until you use all the items from both lists, and then you're done!
1 4 8 10 2 5 7 9 ------------ becomes ------------> 1 2 4 5 7 8 9 10
But what if you don't have two sorted lists? What if you have only one list, and its elements in random order?
This is the smart thing about merge sorting. You can break any single list into smaller fragments, each of which is either an unsorted list, or a sorted list, or one element (which, if you know this, is actually a sorted list with length = 1).
So, the first step in the merge sort algorithm is to split your general list into smaller and smaller sub-lists. At the smallest levels (where each list contains only one or two elements), they are very easy to sort. And after sorting, it is easy to combine any two adjacent sorted lists into a larger sorted list containing all the elements of two subscriptions.
NOTE. This algorithm is much better than the bubble sorting method described above in terms of the efficiency of its worst case scenario. I won’t go into a detailed explanation (which includes a pretty trivial math, but it will take some time to explain), but a quick reason to increase the efficiency is that this algorithm breaks its problem into pieces of perfect size and then combines the results of these pieces. The bubble sorting algorithm solves everything at once, so it does not get the divide and win benefits.
These are just two algorithms for sorting the list, but there are many other interesting techniques, each of which has its advantages and disadvantages: Quick Sort, Radix Sort, Sort Selection, Sort Heap, Sort Angles and Sort Bucket.
The Internet is full of interesting sorting information. Here is a good place to start:
http://en.wikipedia.org/wiki/Sorting_algorithms