📚 College Credit Guide ✓ UPI Study 🕐 7 min read

What Are Bubble Sort and Selection Sort?

This article explains bubble sort and selection sort step by step, compares their time costs, and shows when each one makes sense in intro CS study.

US
UPI Study Team Member
📅 August 07, 2026
📖 7 min read
US
About the Author
The UPI Study team works directly with students on credit transfer, degree planning, and course selection. We've helped thousands of students figure out what counts toward their degree and how to finish faster without paying more than they have to. This post is written the way we'd explain it to you directly.
🦉

Bubble sort and selection sort are two basic sorting methods that show up early in data structure and algorithms courses because they are easy to trace by hand. Bubble sort compares neighbors and swaps out-of-order items. Selection sort scans for the smallest item and places it at the front. Both teach the same core idea: sorting costs time, and the way you move data changes that cost. Students usually meet these two first because they expose the shape of O(n^2) work without hiding the steps inside fancy code. That matters in class, in exam prep, and in interviews where a teacher wants you to explain what happens on each pass. A five-item list can feel tiny, but the same rules still apply when you scale to 500 or 50,000 items. That jump is where the weakness shows. The big difference sits in how they move through the array. Bubble sort makes repeated adjacent swaps, so the largest unsorted value drifts to the end after each pass. Selection sort makes one swap per pass after it finds the minimum. One looks noisy. The other looks tidy. Both are simple, and both can be slow on large inputs.

Data Structures and Algorithms
College credit · ACE & NCCRS reviewed · self-paced
View course
Close-up view of colorful programming code on a screen, ideal for tech and development themes — UPI Study

How Does Bubble Sort Actually Work?

Bubble sort works by comparing two adjacent items, swapping them if they are out of order, and repeating that pass until the biggest unsorted item lands at the end. It feels clunky, and that is the point: you can watch the array change one tiny move at a time.

  1. Start with an array like [5, 1, 4, 2, 8]. Compare 5 and 1, then swap them because 5 > 1.
  2. Move one spot right and compare 5 and 4, then swap again. Compare 5 and 2 next, and swap that pair too.
  3. Compare 5 and 8 last. No swap happens because 5 < 8, so the largest item has already drifted to the end after 1 pass.
  4. Run a second pass on the first 4 items. Now 1 stays put, 4 and 2 swap, and the end position stays fixed.
  5. Keep going until a full pass makes 0 swaps. That early-stop check can save time on nearly sorted input, sometimes after just 1 extra pass.

How Does Selection Sort Actually Work?

Selection sort works by scanning the unsorted part of the list, finding the smallest item, and swapping it into the next open spot at the front. The sorted section grows left to right, and the unsorted section shrinks one item at a time. Clean. Predictable. A little arrogant, honestly.

  1. Take [29, 10, 14, 37, 13]. Start at position 0 and scan all 5 items to find the minimum, which is 10.
  2. Swap 10 with 29. Now the array becomes [10, 29, 14, 37, 13], and the first slot belongs to the sorted side.
  3. Move to position 1 and scan the last 4 items. The smallest value there is 13, so swap it with 29.
  4. After 2 passes, the front part reads [10, 13]. The last 3 items still need sorting, so the search keeps shrinking.
  5. Repeat until only 1 item remains unsorted. Selection sort always uses one swap per pass, even if the list already looks almost finished.

What Is the Time Complexity Difference?

These two algorithms look similar on paper, but the numbers tell a sharper story. Both basic versions do about n(n-1)/2 comparisons, which means 45 comparisons for 10 items and 4,950 for 100 items. The real gap shows up in swaps and in the early-exit trick bubble sort can use. That matters in a data structure and algorithms course, because instructors love asking why two O(n^2) methods still behave differently.

ThingBubble SortSelection Sort
Comparisons~n(n-1)/2~n(n-1)/2
SwapsUp to ~n(n-1)/2At most n-1
Best caseO(n) with early stopO(n^2)
Average / worstO(n^2) / O(n^2)O(n^2) / O(n^2)
Extra spaceO(1)O(1)

Selection sort wins on swap count, and bubble sort can win on nearly sorted data if the early-stop flag trips after 1 pass. That is a small edge, not a miracle.

Data Structures Algorithms UPI Study Course

Learn Data Structures Algorithms Online for College Credit

This is one topic inside the full Data Structures Algorithms course on UPI Study — a self-paced, online class that earns real college credit. Credits are ACE and NCCRS evaluated and transfer to partner colleges across the US and Canada. Courses start at $250 with no deadlines and lifetime access.

Browse Data Structures Course →

Which Sort Is Easier To Implement?

Selection sort is usually easier to code because you only track the current minimum and make 1 swap per pass. That simple pattern helps beginners avoid messy swap bugs, especially on the first 2 homework sets in a data structures and algorithms course. Bubble sort feels more visual, though, because you can see values slide one step at a time.

A lot of students trip on off-by-one mistakes. They loop too far, compare past the end, or forget to reset the minimum index on each pass. I think selection sort teaches cleaner thinking, but bubble sort teaches better intuition for how local swaps change an array. Both show up in intro exams for that reason.

If you study Data Structures and Algorithms or Programming in Python, you will see these algorithms because they are small enough to trace in 10 minutes and still reveal real logic. That is why teachers keep them around.

A downside hits fast: neither algorithm scales well. On 1,000 items, the plain version can force about 499,500 comparisons, and that gets old in a hurry.

When Should Students Use Bubble Sort?

Bubble sort makes sense for tiny arrays, like 5 to 20 items, where the code stays easy to trace and the list changes in a very visible way. It also helps when the input already sits close to sorted, because an early-exit check can stop after 1 pass if no swaps happen. That makes it handy for classroom demos and whiteboard work.

The adjacent-swap pattern also helps when the lesson centers on local movement. A teacher can point to 2 neighbors and say, “These are out of order,” which feels concrete in a way some students like. If you use Programming in C, bubble sort also gives you a clean chance to practice loops, indexes, and swap variables without extra syntax noise.

Do not choose bubble sort for large datasets. On 10,000 items, O(n^2) work turns into 100,000,000 comparisons in the rough worst case, and that is far too slow for real sorting jobs. It is a teaching tool first, not a speed tool.

Why Choose Selection Sort Over Bubble Sort?

Selection sort is the better pick when you want fewer swaps, a fixed pass pattern, and a simple way to reason about progress. It makes at most n-1 swaps, so for 20 items you still only move values 19 times, while bubble sort may swap far more often. That difference matters when each swap costs something.

Reality check: If you care about code that is easy to explain in a 2-minute oral answer, selection sort usually wins because each pass has one job: find the minimum, then place it. Bubble sort looks more active, but that motion can hide the structure for some beginners.

Students in a data structure and algorithms course often choose selection sort first when they want a clean mental model for loops and indices. Students choose bubble sort when they want to see data move gradually and talk about early exit on nearly sorted input. That choice shows up again in interview basics, where clarity beats fancy tricks.

If you want one rule, use selection sort for fewer writes and bubble sort for a simple visual story. Neither one belongs in serious production code, and that honesty saves time.

Frequently Asked Questions about Sorting Algorithms

Final Thoughts on Sorting Algorithms

Bubble sort and selection sort teach the same lesson from different angles: sorting takes repeated work, and the way you move items changes how that work feels. Bubble sort shows local swaps in plain sight. Selection sort shows a steady search for the smallest item. That is why both still show up in intro classes, even though faster methods like quicksort and mergesort beat them on real workloads. If you are studying for an exam, trace both algorithms on paper with 5 items, then try again with 8. That small drill forces the pass structure into your head. Watch the comparison count too. Once you see how both land near n(n-1)/2 comparisons, the O(n^2) label stops feeling abstract and starts looking like a warning sign. Pick bubble sort when you want a visual story and an early-stop trick. Pick selection sort when you want fewer swaps and a cleaner pass pattern. That choice is simple, and in beginner CS, simple usually wins. A good next step is to write both versions from scratch, run them on a 10-item list, and time the difference yourself.

How UPI Study credits actually work

Ready to Earn College Credit?

ACE & NCCRS approved · Self-paced · Transfer to colleges · $250/course or $99/month

More on Data Structures Algorithms
© UPI Study. This article and its educational content are solely owned by UPI Study and licensed under CC BY-NC-ND 4.0. It is not free to reuse or modify. Any citation must credit UPI Study with a direct link to this page.