Sorting Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Sorting.
This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.
Concept Recap
Rearranging items in a collection into a defined order, such as smallest to largest or alphabetical. Sorting is one of the most studied problems in computer science, with algorithms ranging from simple (bubble sort, O(n^2)) to efficient (merge sort, O(n \log n)).
Putting things in order—alphabetical, numerical, by date—so they are easier to find and use.
Read the full concept explanation →How to Use These Examples
- Read the first worked example with the solution open so the structure is clear.
- Try the practice problems before revealing each solution.
- Use the related concepts and background knowledge badges if you feel stuck.
What to Focus On
Core idea: Different sorting algorithms have different efficiency trade-offs and work better in different situations.
Common stuck point: Stable sort preserves original order of equal elements; unstable doesn't.
Sense of Study hint: When choosing a sorting algorithm, consider the data size and whether simplicity or speed matters more. For small datasets or learning, bubble sort is easy to understand. For large datasets, use merge sort or your language's built-in sort (usually optimized). Always clarify what 'order' means—ascending, descending, or custom.
Worked Examples
Example 1
mediumSolution
- 1 Step 1: Pass 1 — compare adjacent pairs and swap if needed: (5,2)→swap→[2,5,8,1,9], (5,8)→ok, (8,1)→swap→[2,5,1,8,9], (8,9)→ok. After pass 1: [2,5,1,8,9].
- 2 Step 2: Pass 2: (2,5)→ok, (5,1)→swap→[2,1,5,8,9], (5,8)→ok, (8,9)→ok. After pass 2: [2,1,5,8,9].
- 3 Step 3: Continue until no swaps occur. Bubble sort has O(n^2) time complexity.
Answer
Example 2
hardPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
mediumRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.