Tina Comston Comp-111 Week 12 Franklin University |
||||
Week 12 | ||||
Preparation
|
||||
Guided
Learning Activity |
||||
Assignment
- no homework assignment this week Concepts - Sorting and Searching In
the reading this week you are presented with several sorting
algorithms. Each algorithm takes a different approach to sorting
items, and each has a different efficiency associated with it - BUT
each of the algorithms works. You can copy the code directly from
the book or reading guide, modify it to work with whatever you are
sorting and it will work. Selection Sort YouTube example: https://www.youtube.com/watch?v=f8hXR_Hvybo Insertion Sort YouTube example: https://www.youtube.com/watch?v=DFG-XuyPYUQ Bubble Sort YouTube example: https://www.youtube.com/watch?v=8Kp-8OGwphY Using the Bubble Sort Algorithm Here is the third version of the Bubble Sort Algorithm copied directly from the reading guide: public static int[] bblSort3(int arr[], int left, int right) { boolean didSwap = true; }int last = right-1; // continue as long as a swap happened in the last pass, // didSwap is a sentinel while (didSwap) { // assume the array is sorted }didSwap = false; // Look at all the unsorted elements per pass for (int i=left; i<last; ++i) { // detect if the array was actually unsorted }if (arr[i] > arr[i+1]) { didSwap = true; }int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; --last; return arr; Here's an explanation of the algorithm: public static int[] bblSort3(int arr[], int left, int right)
boolean didSwap = true; int last = right-1; // continue as long as a swap happened in the last pass, // didSwap is a sentinel while (didSwap)
didSwap = false; // Look at all the unsorted elements per pass for (int i=left; i<last; ++i) {
// detect if the array was actually unsorted if (arr[i] > arr[i+1]) { didSwap = true; }int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;
--last;
Using the algorithm The above algorithm works great with integer data stored in an array, but what if you have string data? You would need to: What if you have an ArrayList and not an array?
You would need to: What if you have an object and you want to sort on an instance variable in the object?
You would need to:
|
||||
Reflection
Paper
|
||||
Lab 5
|