Sorting #
This page contains information and coding exercises for sorting
, using both Bubble sort
and Selection Sort
.
Open up the pseudocode compiler in a new tab. You will be using this website to complete the exercises below.
Starter Code #
๐ป Copy/paste this starter code into your compiler//------ Sorting Algorithm -----------------------
NUMS = [15,30,85,25,40,90,50,65,20,60]
output "------Before sorting------"
loop C from 0 to 9
output NUMS[C]
end loop
output "------Before sorting------"
output
//-----------------------
// YOUR CODE GOES HERE
//-----------------------
output
output "------After sorting------"
loop C from 0 to 9
output NUMS[C]
end loop
output "------After sorting------"
Bubble Sort #
๐ป Using the example code, write the `bubbleSort`Using the example code, use the Bubble Sort
algorithm to sort the array. If you are unsure how to begin, use the provided checklist to help.
Improving your Bubble Sort #
๐ป Create a method called `swap()` that can swap two numbers in an arrayParameters: position1
, position2
, array
Return: updated array
- Swap the items at position1 and position2
- Return the updated array
Now use your swap
to improve your sorting
Parameter: an array of numbers
Return: a sorted array of numbers
- Sorts the array
- Everytime a swap is required, it should use
swap()
Selection Sort #
Now you will move on to the next sorting algorithm.
๐ป Create a method called `selectionSort`Parameter: an array of numbers
Return: a sorted array of numbers
- Sorts the array
- Everytime a swap is required, it should use
swap()
You should be able to use the same starter code to test your selectionSort
Deliverables #
โกโจPaste your code in your Exercises lab log
Extensions #
Sort by Alphabetical Order #
๐ป
Create a helper method that can tell which word should come first alphabetically, and returns true
/false
. Then adapt one of your sorting algorithms to use this helper method
Sort by Textbook ordering #
Textbooks are ordered in a strange way. The numbers in a textbook chapter might be numbered like this:
[1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14]
๐ป
Create a helper method that can tell which number is higher and return true
/false
. Then adapt one of your sorting algorithms to use this helper method.