Lesson 9: Searching and Sorting
45 minutes
Overview
How can I combine searching and sorting algorithms to solve problems?
Students have explored searching and sorting algorithms, analyzed their efficiencies using execution counts, and implemented algorithms to work with multiple lists. Students plan and implement solutions using searching and sorting algorithms to solve problems involving one or more lists. Students then participate in a peer review to give and receive feedback on the progress they have made on their Creative Coding with the Console Project.
Agenda
Objectives
Students will be able to:
- Implement a linear or binary search algorithm to find a target element in a data structure
- Implement a selection, insertion, or merge sort algorithm to organize elements in a data structure
Preparation
- Create code review groups if you are not reusing the same groups
- Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the students
- Searching Algorithms - Video
- Sorting Algorithms - Video
- U8L9 Extra Practice - Handout
Teaching Guide
Warm Up (5 minutes)
Software Engineering Skills
Remarks
You have made a lot of progress in developing your software engineering skills. We have seen how these skills are useful to solve a variety of problems and create programs like what we have used in our daily lives.
Discuss: Click through the animated slide to display the prompts.
- How has your perception of software engineering changed in this unit?
- How have your software engineering skills improved in this unit?
Discussion Goal: Students share how their perception of software engineering has changed, including how software engineering involves translating everyday approaches to searching and sorting information to a programming language. Students identify the software engineering skills and characteristics they feel they improved in this unit.
Activity (35 minutes)
Searching and Sorting Algorithms (20 minutes)
Remarks
We have learned a lot about searching and sorting algorithms in this unit! These searching and sorting algorithms expand our skills and capabilities to help us solve problems that software engineers work on every day. Let's review the searching and sorting algorithms we have learned.
Do This: Review the lesson objectives.
Discuss: Click through the animated slide to display the prompts. Use the Retrieve-Pair-Share strategy to discuss the prompts.
- How does the linear search algorithm work?
- How does the binary search algorithm work?
- Which is more efficient? Why?
- When is the binary search algorithm not the most efficient?
Discussion Goal: Students recall that the linear search algorithm checks each element in the data structure from first to last until the target is found, while the binary search algorithm splits the list in half repeatedly until the target is found. Students note that the binary search is more efficient because it has fewer execution counts but only works when the list is sorted.
Have students recall the best case, average case, and worst case for the linear search and binary search algorithms. Ask guiding questions to help students identify which is most efficient, or you can write lists of numbers on the board and have students apply each algorithm to the list to determine the execution counts.
Display: Show the video – Searching Algorithms.
Discuss: Click through the animated slide to display the prompts. Use the Retrieve-Pair-Share strategy to discuss the prompts.
- How does the selection sort algorithm work?
- How does the insertion sort algorithm work?
- How does the merge sort algorithm work?
- Which is more efficient? Why?
Discussion Goal: Students recall that the selection sort algorithm finds the minimum value in the list and swaps with the previous element and repeats this process until the list is in order. Students recall that the insertion sort algorithm inserts the element into the correct position and shifts the rest of the elements over. Students note that the merge sort algorithm is a recursive algorithm that repeatedly divides the list into smaller lists until each list contains only one value and then merges the elements back together into one list. Students also note that each algorithm is fairly comparable in efficiency but one might be best depending on the size of the list.
Display: Show the video – Sorting Algorithms.
Do This: Direct students to Level 1 on Code Studio to complete Levels 1, 2, and 3. Students complete a Check for Understanding on Level 1, then continue to Level 2 to complete a choice level to implement a binary search. On Level 3, students complete a choice level to implement a sorting algorithm.
Encourage students to write pseudocode first, then translate their solution to Java.
Peer Review and Feedback (15 minutes)
Remarks
Before working towards the next benchmark, it is helpful to assess your achievement of this benchmark and get feedback on your development so far.
Do This: Click through the animated slide to have students participate in the Code Review Call and Response.
Do This: Direct students to complete a code review on Level 4.
Wrap Up (5 minutes)
Revisiting the Need to Knows
Remarks
We just learned a lot of new information today, which may have even answered some of the Need to Know questions you wrote down about the Creative Coding Project. As we progress through the unit, it is helpful to stop and note what we have learned that is related to or useful for the project.
Do This: Have students review the questions they wrote in the "Need to Know" column on page two of their Creative Coding with the Console Project Planning Guide. Students add new questions to this column, check off any answered questions, and write answers to any questions in the "Learned" column.
Do This: Have students share what they added to their chart with a partner.
If time permits, you can also have students share as a class.
Do This: Review the concepts covered in this lesson.
Assessment: Check for Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.