Quickly finding and sorting data is a very important part for many developers as it allows users to search and find data quickly.
While many languages have built in sorting algorithms, sometimes we only have a template that we need to finish. This is especially true for complex data types which aren’t as easy to sort, because there might be multiple fields which we can sort on instead of just one as you would find in a native data type like an integer.
It should be noted that one of the fastest sorts to write, the bubble sort, is also one of the slowest to run. Just because a sort is simple to understand and write, doesn’t mean a sort is easy to process by the computer. Some simple to understand/write sorting algorithms are actually very slow because they require a lot of extra steps and tests.
The QuickSort, when compared to the Bubble Sort, would seem to take longer. It has more lines of code to run, however, because of how it runs, it performs way less operations than the Bubble Sort.
I’ve seen a change in sorting algorithms take a job which used to take hours, or even days, to take a short amount of time, like minutes – or less.
Why Different Sorting Algorithms
In which case, sometimes people ask, why not just use a single sorting method, pick the best and be done with it. Well, different methods are better, or worse, depending upon the situation.
The Merge Sort is similar to the Quick Sort, however, the Merge Sort runs slower because instead of working with a single array, it creates many arrays. That is until you find the special cases where it works better. For example, if there are a lot of potential duplicate values.
Consider a situation where you want to sort a list of students based upon the number of credit hours they’ve taken. Now it makes a lot of sense to use a merge sort since there are few possible answers, with lots of duplicates.
Another example, is the Merge Sort works with arrays and with some modifications works with data stored in a Linked List. The Quick Sort data must be in an array. So learning different types of sorts, based upon how and where they perform best, is necessary.
Presorted Data
Why can’t we sort data automatically? Well, there are two main reasons.
First, we don’t always know how the data is going to be entered. If we are loading names at a trade show, we can’t force all the “A’s” to come to our booth, then “B’s” then… well, you get the idea. More often than not, the data we get will be out of order in the method we want to sort it. There’s always an order (like order the people came by the booth), but sometimes that order is of no practical use to us.
Second, we may need it sorted by first name one day, and the next day we need to sort by birth date. Think about how you see an Excel spreadsheet and how you can sort by one or more columns at will. This is important to how data is managed and used. So being able to quickly sort data for a user is very important since they will expect that type of response.
How we store data, whether it’s in an array, a linked list, etc., is going to have a dramatic effect on which sorting algorithm we can use as well.
Quicksort works well for an array, but is nearly impossible for a linked list. The Merge Sort works well where there is lots of repeating data. As we go through these different types of sorting algorithms, we’ll look at how using different types of data can impact how we use algorithms, which is something to keep in mind, not just with sorting algorithms, but with all types of algorithms. The data, and it’s structure, is an immensely important process.
An Intro to Sorting Data was originally found on Access 2 Learn