Back to Week 3
Lab Session

Week 3: Lab — Sorting & Searching

Hands-on Programming Exercises — Ch. 7-8

Progress0 / 5 completed

1Sort a Class Leaderboard

Think about it: A teacher has 5 student exam grades and wants to display them sorted from lowest to highest to see who needs extra help at a glance. She doesn't have a built-in sort — she needs to write her own. How would you do it? Pick any approach: find the smallest and put it first, or take each grade and slide it into the right position — your choice!
Read 5 integers (exam grades) into an array. Write your own logic to sort them in ascending order (without using Arrays.sort). Use nested loops and swapping. Print the array before and after sorting.
Expected OutputEnter 5 grades: 78 92 65 88 71 Before sorting: 78 92 65 88 71 After sorting: 65 71 78 88 92
Your Code
One approach: use two nested for loops. Outer: for (int i = 0; i < 4; i++). Inner: for (int j = i + 1; j < 5; j++). If grades[i] > grades[j], swap them using a temp variable: int temp = grades[i]; grades[i] = grades[j]; grades[j] = temp;.

2Sort Product Prices — Most Expensive First

Think about it: An online store wants to show products sorted from most expensive to cheapest so customers can see premium items first. The trick here is: sorting in descending order — everything you know about sorting still works, you just flip the comparison! Instead of checking "is this smaller?", you check "is this bigger?".
Read N doubles (product prices) into an array. Sort them in descending order (highest to lowest) using nested loops and swapping. Print the sorted prices.
Expected OutputEnter number of products: 5 Enter 5 prices: 29.99 9.50 45.00 12.75 31.20 Prices (high to low): 45.0 31.2 29.99 12.75 9.5
Your Code
Same as ascending sort, but flip the comparison. Outer: for (int i = 0; i < n - 1; i++). Inner: for (int j = i + 1; j < n; j++). If prices[i] < prices[j] (notice the < instead of >), swap: double temp = prices[i]; prices[i] = prices[j]; prices[j] = temp;.

3Find a Student ID in a List

Think about it: The university lost-and-found office has a box of student ID cards — they're in no particular order. A student comes in and gives their ID number. The only way to find the card is to check each one, starting from the first, until you find it or run out of cards. This is linear search — simple and works on any list, sorted or not.
Read N integers (student IDs) into an array, then read a target ID to search for. Loop through the array one element at a time. If found, print the index. If not found, print "ID not found".
Expected OutputEnter number of IDs: 6 Enter 6 IDs: 1045 2031 1578 3022 1190 2455 Enter ID to search: 3022 Found at index: 3
Your Code
Use a flag: boolean found = false;. Loop: for (int i = 0; i < n; i++). Inside: if (ids[i] == target) → print "Found at index: " + i, set found = true;, and break;. After the loop: if (!found) System.out.println("ID not found");.

4Sort Scores, Then Find a Grade

Think about it: A teacher enters exam scores in random order. She wants to: (1) sort them so she can see the results clearly, then (2) quickly check if a specific score exists. This is a very common pattern in programming — sort first, then search. Once data is sorted, you can search much faster by always looking in the middle and eliminating half the remaining data each time.
Read N integers into an array. First, sort the array using Arrays.sort(). Print the sorted array using Arrays.toString(). Then read a target value and use Arrays.binarySearch() to find it. Print the result — if index >= 0, it's found; otherwise it's not.
Expected OutputEnter number of scores: 6 Enter 6 scores: 85 72 90 68 95 78 Sorted: [68, 72, 78, 85, 90, 95] Enter score to find: 90 Score 90 found at index: 4
Your Code
Sort: Arrays.sort(scores);. Print: System.out.println("Sorted: " + Arrays.toString(scores));. Search: int index = Arrays.binarySearch(scores, target);. Then: if (index >= 0) print found with index, else print "Score not found".

5Top 3 Players from a Tournament

Think about it: A gaming tournament just ended and you have all the player scores. The organizer needs the top 3 highest scores for the podium. How do you find them? One smart approach: sort all scores from highest to lowest, then just take the first 3. Sorting makes many problems easy — finding the top N, the bottom N, the median, etc.
Read N integers (player scores) into an array. Sort the array in descending order using your own nested loops. Then print only the top 3 scores (the first 3 elements after sorting). If there are fewer than 3 scores, print all of them.
Expected OutputEnter number of players: 7 Enter 7 scores: 340 520 190 680 450 720 310 Top 3 scores: 1st place: 720 2nd place: 680 3rd place: 520
Your Code
Sort descending: outer for (int i = 0; i < n - 1; i++), inner for (int j = i + 1; j < n; j++). If scores[i] < scores[j], swap with temp. For top 3: int top = Math.min(3, n); then for (int i = 0; i < top; i++) print places[i] + " place: " + scores[i].