Join Raghavendra Dixit for an in-depth discussion in this video Bubble sort algorithm, part of Introduction to Data Structures & Algorithms in Java.
So as suggested, let's follow this process and see how it helps us solve the cue balls. So we compare the first two cue balls in the bin. Now, two is less than ten, so we swap them using the extra bin provided. Then we move to the right by one bin. So we compare ten and eight. Well eight is smaller than ten, so we do the swap. Then we move to the right by one bin again, and we keep doing that until we reach the last bin.
And now if you notice, the ball having the largest number is in the rightmost bin, isn't it? And this should have been because every time we compared the ball with the larger number moved to the right. So this way of sorting is known as the Bubble Sort Algorithm because in each pass, the largest number kind of bubbles to one side. So we can keep a pointer here or a stick to keep track of the sorted ball.
Because the largest number has reached the rightmost bin, we can consider that part as sorted, right? Now we again start from the leftmost bin and compare the balls in the first and second bins. Now eight is greater than two so we don't do anything, alright? We don't need to swap the balls now. And we just move to the right by one bin. Now we compare eight and six.
And eight is greater than six so we do the swapping. And we keep repeating this process until we reach the last bin of the unsorted part. Now we don't need to consider the last bin containing the ball with number ten because that is already the largest. So now the second largest number, eight, is at the second last position. So we can move our pointer here to keep track of the sorted part.
And we keep repeating this process over and over until all the cue balls are sorted. Which means that the pointer has come to the first position. So this was the Bubble Sort Algorithm in action that you just saw.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Why study data structures and algorithms?
- How to calculate the time complexity
- Using Big O notation
- Using basic sorting and search algorithms
- Searching elements in unordered arrays and ordered arrays
- Implementing a linked list in Java
- Implementing stacks using arrays
- Queues using arrays
- Binary search trees
- Representing heaps using arrays
Skill Level Intermediate
1. Introduction to Algorithms
2. Analysis of Algorithms
3. Basic Sorting and Search Algorithms
4. Linked Lists
5. Stacks and Queues
7. Binary Search Trees
8. More Sorting Algorithms
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.