We are going to start working with lists! Lists are a type of data structure, a particular way of storing data. Many objects can be easily stored in a list, and so lists have become a very useful and ubiquitous data structure in computer science. For example, if we are developing a game, and we want to keep track of the people that have played our game, we might keep their names in a list.
Sorting, Processing, and Lists in Action
Hit the red button to reset the lists
The A Button alphabetizes the lists
The Arrow Button Sorts the list into single names
The A Button alphabetizes the lists
The Arrow Button Sorts the list into single names
Sorting a List
While experimenting with the starting program given, I first had to add a method for inputing data into the list. After that was completed, I messed around with the sorting algorithm that was given to us. The beginning program sorted the list in alphabetical order and numerical order (1-...). In order to have the sorting algorithm sort in reverse alphabetical order and reverse numerical order, all you had to do was switch the less than symbol into a greater than symbol inside the repeat until. What this does is instead of looking for the letter with the highest value and swapping, it uses the lowest value letter and swaps it instead. At first i failed by switching all the greater than and less than symbols to their opposite, but that created an infinite loop within the repeat until because given "checkhere" would never be below the length of words causing an infinite loop. After this I messed around on how to order the list by the length, all you had to do here was replace the "item indexofbestsofar of words" with length of word, what this does is instead of sorting the first letter's value it sorts the length of the word, and to create the reverse just do the same as above where you switch the less than symbol to a greater than symbol.
While experimenting with the starting program given, I first had to add a method for inputing data into the list. After that was completed, I messed around with the sorting algorithm that was given to us. The beginning program sorted the list in alphabetical order and numerical order (1-...). In order to have the sorting algorithm sort in reverse alphabetical order and reverse numerical order, all you had to do was switch the less than symbol into a greater than symbol inside the repeat until. What this does is instead of looking for the letter with the highest value and swapping, it uses the lowest value letter and swaps it instead. At first i failed by switching all the greater than and less than symbols to their opposite, but that created an infinite loop within the repeat until because given "checkhere" would never be below the length of words causing an infinite loop. After this I messed around on how to order the list by the length, all you had to do here was replace the "item indexofbestsofar of words" with length of word, what this does is instead of sorting the first letter's value it sorts the length of the word, and to create the reverse just do the same as above where you switch the less than symbol to a greater than symbol.
Sometimes, we care about the order of a list (e.g., dictionary) and sometimes it doesn't matter (e.g., shopping list). A very common task for lists is reordering them, either to impose an order that did not exist before (such as sorting a list — alphabetizing a list of names) or changing the ordering (such as reversing a list — taking the alphabetized list of names and making it a reverse-alphabetized list).
At the heart of the matter, reordering a list involves swapping the positions of two items repeatedly:
At the heart of the matter, reordering a list involves swapping the positions of two items repeatedly:
- In the case of a reversal, this might entail taking the first item and swapping it with the last, then the second item with the second to last, etc.
- In the case of sorting a list of names, it might involve taking what should be first (alphabetically) and swapping it with what actually is first, then continuing down the line.