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 - Python Tutorial
From the course: Programming Foundations: Data Structures (2023)
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
-
-
-
-
What is an array?3m 31s
-
(Locked)
Create a list in Python2m 27s
-
(Locked)
Retrieve data from a list in Python4m 26s
-
(Locked)
Mutate a list in Python2m 57s
-
(Locked)
Multidimensional lists7m 15s
-
(Locked)
Tuples in Python2m 45s
-
(Locked)
Search array-like structures4m 47s
-
(Locked)
Sort array-like structures5m 1s
-
(Locked)
Challenge: Find the second smallest item1m 35s
-
(Locked)
Solution: Find the second smallest item4m 26s
-
(Locked)
When to use array-like structures1m 15s
-
-
-
-
-
-
-