Skip to main content
📚 College Credit Guide ✓ UPI Study 🕐 8 min read

What Is Bubble Sort Step By Step?

This article traces bubble sort in Java step by step, showing every pass, swap, loop bound, and array change.

US
UPI Study Team Member
📅 November 15, 2026
📖 8 min read
1 view
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 is a simple sorting method that compares two neighboring values, swaps them if they are in the wrong order, and repeats that process until the whole array is sorted. If you want to understand how a sorting algorithm works without heavy math, this one gives you a clean first look at loops, swaps, and array changes. Here is the basic pattern: scan across the list from left to right, compare each pair, and move the larger value toward the end one step at a time. That is why people call it a bubble simple swapping by step algorithm. It does not look smart on big inputs, and that weakness matters. Bubble sort usually runs in O(n^2) time, so a list of 10 items needs far fewer checks than a list of 1,000 items. Still, students like it because the logic stays visible. You can watch the array change after each pass, which makes the code easier to read than many faster sorts. A small example like [5, 1, 4, 2, 8] shows every swap clearly, and that same trace also helps when you write the Java loop structure yourself.

What Is Bubble Sort Step By Step: learner watching a phone lesson with earbuds and a marked practice card on a bus

What Is Bubble Sort Step By Step?

Bubble sort is a sorting algorithm that checks 2 neighboring array values at a time, swaps them if they are reversed, and keeps doing that for multiple passes until the list is in order. The classic example uses a 5-item array like [5, 1, 4, 2, 8], because you can track every change without losing the thread.

The catch: This method looks simple, but it can do a lot of work: a list of 5 items may need 4 passes, and a list of 100 items can need close to 4,950 comparisons in the worst case. That is the part students miss when they first read the code.

The word bubble fits because the largest unsorted value keeps moving rightward, one spot per swap, until it lands at the end of the array. I like that image because it matches the actual behavior, not just the name. You do not sort the whole list in one shot. You sort it one neighbor pair at a time.

In Java, that means the algorithm depends on two simple ideas: an outer loop for passes and an inner loop for pair checks. If you can trace 1 pass on paper, you can usually trace the whole thing in code. That is why this makes such a good introduction to java course topic, even though the algorithm itself is old and not fast on large data.

How Does Bubble Sort Compare Adjacent Elements?

Bubble sort only compares adjacent elements because each check can move one value exactly 1 place left or right, which keeps the logic easy to follow in Java with array[i] and array[i+1]. If you compare farther-apart values, you stop doing bubble sort and start doing something else.

Inside the inner loop, the code checks whether array[i] is bigger than array[i + 1]. If it is, the program uses a temporary variable to swap them. That swap takes 3 steps in code, and each step matters: save one value, copy the second value over, then write the saved value back.

Reality check: The algorithm does not move every item far at once. On a 5-element list like [3, 2, 1, 4, 5], the 3 rises slowly, then the 4, then the 5. That slow climb is the whole point of the algorithm, and it also explains why bubble sort feels simple but runs slowly on bigger inputs.

The last position fills with the largest unsorted number after each pass, so the next pass can skip that spot. If you trace [5, 1, 4, 2, 8], the 8 settles at the end after the first pass, and the 5 settles into its spot after later passes. That repeated nudging is the heart of Introduction to Java style practice, because students can see how a loop, a comparison, and a swap work together.

Introduction To Java UPI Study Course

Learn Introduction To Java Online for College Credit

This is one topic inside the full Introduction To Java course on UPI Study — a self-paced online class for college credit. Credit recommended by NCCRS; your college decides how credits transfer. Courses start at $250 with no deadlines and lifetime access.

See Introduction To Java Course →

Which colleges have matched Introduction to Java? 16 colleges confirm UPI Study's Introduction to Java (Computer Science 210, 3 credits), according to their equivalency pages on UPI Study. Here are 12 of them, with the course it counts as at each:

CollegeCourse it counts asCreditsHow it counts
Thomas Edison State UniversityCOS-2080 Java Programming3Transfers
University of the PeopleCS 1103 Programming 23Transfers
Post UniversityCIS 1XX - CIS Elective3Transfers
Strayer UniversityJava Programming I (CIS406)3Transfers
Columbia Southern UniversityOpen Electives3Transfers
New Jersey Institute of TechnologyCS 251 Object-Oriented Programming3Transfers
UA GranthamCS192 (Programming Essentials)3Transfers
University of Management and TechnologyCST 222 Programming in Java3Transfers
Woodmont CollegeCPT111X Java I3Transfers
Crestpoint UniversityGeneral Elective (Crestpoint)3Transfers (Elective)
Dunlap-Stone UniversityGeneral Education Technology3Transfers
Indiana TechAPP EL – Applied Elective3Transfers

Also confirmed by Maestro, Newlane, Nexford, Pathways.

See Introduction to Java on UPI Study →

ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Each college confirms the course when it evaluates your transcript.

How Do Bubble Sort Passes Change An Array?

A full bubble sort trace makes the algorithm feel less abstract. Use the array [5, 1, 4, 2, 8], because it gives you 4 clear passes and 1 easy final result. Watch how each pass moves the largest unsorted value to the right.

  1. Pass 1 starts with [5, 1, 4, 2, 8]. Compare 5 and 1, swap them, then compare 5 and 4, swap again, and keep moving right.
  2. After the first pass, the array becomes [1, 4, 2, 5, 8]. The 8 never moves, and that saves work right away.
  3. Pass 2 checks only the first 4 positions, so the code skips the final 8. Compare 1 and 4, then 4 and 2, and swap once.
  4. The second pass ends with [1, 2, 4, 5, 8]. That is already sorted, even though the algorithm still has more comparisons left in many Java versions.
  5. Pass 3 makes no swaps if the loop keeps going, which gives you a clean early-stop signal. A good optimization flag can cut useless work on nearly sorted arrays by 1 full pass.
  6. Pass 4 also finds no change, so the algorithm stops. On this 5-item list, the whole sort finishes in a tiny example; on 50 items, the same pattern can mean dozens more checks.

Why Does Bubble Sort Need Two Loops?

Bubble sort needs 2 loops because one pass cannot finish the job on most arrays, and Java has to repeat the same neighbor checks several times. The outer loop counts passes, while the inner loop walks across the shrinking unsorted section.

Each pass reduces the number of useful comparisons by 1. On a list of 6 items, the first pass may check 5 pairs, the next pass 4, then 3, then 2, then 1. That shrinking pattern is why the loop bound usually looks like n - 1 on the outside and n - 1 - pass on the inside.

What this means: The code gets shorter near the end of the sort because the last positions already hold the largest values. A smart version keeps a boolean flag, often called swapped, and stops after a pass with 0 swaps. That one small idea can save a lot of useless checks on a list that almost finished itself.

I think this is the best part of bubble sort for beginners: the loop math feels mechanical, not magical. You can point to each line and say what it does. A student who can read 2 nested loops here can handle harder sorting code later, especially after 1 or 2 careful practice runs.

What Java Bubble Sort Code Should You Read?

A short Java bubble sort example makes the trace real, not theoretical, and you can match every line to the 5-item array [5, 1, 4, 2, 8]. The code below uses 2 loops, 1 temporary variable, and an optional early-exit flag, which is the same structure you see in most Introduction to Java lessons and many Data Structures and Algorithms classes. That makes it a solid first sorting example, even if it will not win any speed contest against quicker methods on 1,000-item arrays.

If you line that code up with the earlier passes, the logic clicks faster than if you read it cold. The array does not sort itself by magic; each loop just repeats the same comparison pattern until no pair is out of order.

Frequently Asked Questions about Bubble Sort

Final Thoughts on Bubble Sort

Bubble sort teaches sorting by repetition, not by speed. That is why it helps new programmers see the shape of an algorithm before they meet faster methods like quicksort or mergesort. A 5-item array shows the idea cleanly, and a 50-item array shows the weakness fast. The biggest lesson sits in the loops. The outer loop handles passes, the inner loop handles neighbor checks, and the swap only happens when two values sit in the wrong order. Once you can explain those 3 parts in plain words, the code stops feeling mysterious. This sort also teaches a useful habit: track the array after each pass, not just the final answer. That habit helps with debugging, homework, and exam questions, because the steps matter as much as the result. If you can trace [5, 1, 4, 2, 8] by hand, you can usually trace any small bubble sort question your instructor throws at you. Try rewriting the algorithm on paper once, then type it in Java and run it with 2 or 3 different arrays. That extra 10 minutes usually beats staring at the code for an hour.

How UPI Study credits actually work

Ready to Earn College Credit?

Credit recommended by NCCRS · Self-paced · Your college decides how credits transfer · $250/course or $99/month (US)

More on Introduction To Java
© 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.

Comments

    No comments yet. Ask a question or share how it went for you.

    Leave a comment

    Comments go live straight away after an automatic check. Keep it kind and on topic, and please don’t post links, phone numbers, emails or other personal details. We remove comments that break these rules; use Report on any comment that does. Your email is only used to reply to you. Privacy policy