From this introduction, you should understand that you need to learn how to sort first and then search the information given. In this section, we will cover some of the most well-known sorting algorithms in Computer Science. We will start with the slowest one, and then we will cover some better algorithms.
Before we get started with the sorting algorithms, let's create an array
(list) to represent the data structure we want to sort and search:
function ArrayList(){ var array = []; //{1} this.insert = function(item){ //{2} array.push(item); }; this.toString= function(){ //{3} return array.join(); }; }
As you can see, the ArrayList
is a simple data structure that will store the items in an array ({1}
). We only have an insert
method to add elements to our data structure ({2}
), which simply uses the native push
method of the JavaScript Array
class that we covered in Chapter 2, Arrays. Finally, to help us verify the result, the toString
method...