Home Download Help Forum Resources Extensions FAQ NetLogo Publications Contact Us Donate Models: Library Community Modeling Commons Beginners Interactive NetLogo Dictionary (BIND) NetLogo Dictionary User Manuals: Web Printable Chinese Czech Farsi / Persian Japanese Spanish
|
NetLogo Models Library: |
If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web |
This model is a visual demonstration of a standard sort algorithm called merge sort. The algorithm reorders, or permutes, n numbers into ascending order. This is accomplished by dividing the numbers into groups and then merging smaller groups to form larger groups. Order is maintained as the lists are merged so when the algorithm finishes there is only one sorted list containing all n items.
Note that it is possible to express merge sort in NetLogo much more concisely than is done in this model. Since this model aims to demonstrate the sort algorithm visually, the code is more complex than would be needed if the model only needed to sort the numbers.
We start out with as many independent groups as we have elements. As the algorithm progresses through the list, it merges each adjacent pair of groups; thus, after each pass, the number of groups is halved.
To merge two groups: 1. Compare the first elements of the two groups to each other 2. Place the smallest/largest element (depending on the increasing-order? switch) of the two in a third group 3. Remove that element from its source group 4. Repeat until one of the source groups is empty 5. Place all of the remaining elements from the non-empty source group onto the end of the third group 6. Substitute, in place, the third group for the two source groups
We do this merge repeatedly for each set of two groups until there is only one group left. This final group is the original set of numbers in sorted order.
The number of steps required to sort n items using this algorithm is the ceiling of logarithm (base 2) of n. Each step requires at most n comparisons between the numbers. Therefore, the time it takes for the algorithm to run is about n log n. Computer scientists often write this as O(n log n) where n is how many numbers are to be sorted.
Change the value of the NUMBER-OF-ELEMENTS slider to modify how many numbers to sort.
Pressing SETUP creates NUMBER-OF-ELEMENTS random values to be sorted.
STEP (1 ITEM) merges one number into its new group.
STEP (1 ROW) does one full round of group merges.
Groups are represented by color. Numbers in the same group have the same color. When two groups merge, the numbers take the color of the smallest/largest element in the new group. Can you predict what would be the final color of all elements before starting?
Would merging more than two groups at a time lead to the elements getting sorted in fewer steps? Would this change make the algorithm any faster?
We stated above that the algorithm will take at most a constant factor times n log n time to execute. Can you figure out why the constant factor is needed to make this statement accurate?
Can you make the elements draw their paths across the view?
There are many different sorting algorithms. You can find a few described at https://en.wikipedia.org/wiki/Sorting_algorithm. Try implementing the different sorts in NetLogo and use BehaviorSpace to compare them. Do different sorts perform better with different input sets (uniformly random, nearly sorted, reverse sorted, etc.)?
This model uses lists extensively.
Note that NetLogo includes SORT and SORT-BY primitives; normally, you would just use one of these, rather than implementing a sort algorithm yourself. SORT arranges items in ascending order; SORT-BY lets you specify how items are to be ordered.
If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
For the model itself:
Please cite the NetLogo software as:
Copyright 2005 Uri Wilensky.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
(back to the NetLogo Models Library)