From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Sort array-like structures

Sort array-like structures

- Sometimes, we'll want to search for multiple elements in a list, within a short period of time. If we run a linear search every single time we want to search, this can take quite a while. If we know we're going to search for many different items, it might be worth sorting the list first, in order to make each search faster. Now how do we sort items? Well, usually there's a natural order to our data. For example, if we had a list of numbers, a sorted version of this list could have the numbers in increasing or decreasing order. With characters and strings, we can sort in alphabetical order. To sort items, we can implement a sorting function ourselves, or usually this behavior is built into the programming language, through a sort function. Let's see how it works in code. In Python, we can use the built-in sorted function. We just pass in the list we want to sort and it will return the list sorted. Let's run it.…

Contents