programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# bubble sort explanation

phi tran
Greenhorn
Posts: 14
hi everyone!

So I'm going to be showing a friend of mine how the bubble sort works. I wrote this piece of code to show them how the iteration looks like and how many times it iterates. I think the output provides enough information, but I just wanted to get some perspective in case I missed anything? I can provide the output if anyone is interested. Thanks everyone

L Foster
Ranch Hand
Posts: 242
14
• 1
The first thing I notice, is that you've called this program "helloworld.HelloWorld". I would have called it something like "algorithms.BubbleSort", or "sort.BubbleSort", or perhaps "self.phitran.BubbleSort", etc.

I just snapped it into NetBeans IDE and it compiled and ran perfectly. It looks like a great way to display differences in algorithmic processing. Consider making other sort routines and doing the same. You could maybe even make a little mini-library of dump routines in some logging class that you could snap into each one. You might find it fun, and a great way to critically examine all the assertions that are made about sorting algorithms.

Nice code.

run:
unsorted: [5, 2, 10, 6, 12, 3, 20, 8, 7, 9]
1th: [2, 5, 10, 6, 12, 3, 20, 8, 7, 9]
1th: [2, 5, 10, 6, 12, 3, 20, 8, 7, 9]
1th: [2, 5, 6, 10, 12, 3, 20, 8, 7, 9]
1th: [2, 5, 6, 10, 12, 3, 20, 8, 7, 9]
1th: [2, 5, 6, 10, 3, 12, 20, 8, 7, 9]
1th: [2, 5, 6, 10, 3, 12, 20, 8, 7, 9]
1th: [2, 5, 6, 10, 3, 12, 8, 20, 7, 9]
1th: [2, 5, 6, 10, 3, 12, 8, 7, 20, 9]
1th: [2, 5, 6, 10, 3, 12, 8, 7, 9, 20]
interation on 1th: 9
2th: [2, 5, 6, 10, 3, 12, 8, 7, 9, 20]
2th: [2, 5, 6, 10, 3, 12, 8, 7, 9, 20]
2th: [2, 5, 6, 10, 3, 12, 8, 7, 9, 20]
2th: [2, 5, 6, 3, 10, 12, 8, 7, 9, 20]
2th: [2, 5, 6, 3, 10, 12, 8, 7, 9, 20]
2th: [2, 5, 6, 3, 10, 8, 12, 7, 9, 20]
2th: [2, 5, 6, 3, 10, 8, 7, 12, 9, 20]
2th: [2, 5, 6, 3, 10, 8, 7, 9, 12, 20]
interation on 2th: 8
3th: [2, 5, 6, 3, 10, 8, 7, 9, 12, 20]
3th: [2, 5, 6, 3, 10, 8, 7, 9, 12, 20]
3th: [2, 5, 3, 6, 10, 8, 7, 9, 12, 20]
3th: [2, 5, 3, 6, 10, 8, 7, 9, 12, 20]
3th: [2, 5, 3, 6, 8, 10, 7, 9, 12, 20]
3th: [2, 5, 3, 6, 8, 7, 10, 9, 12, 20]
3th: [2, 5, 3, 6, 8, 7, 9, 10, 12, 20]
interation on 3th: 7
4th: [2, 5, 3, 6, 8, 7, 9, 10, 12, 20]
4th: [2, 3, 5, 6, 8, 7, 9, 10, 12, 20]
4th: [2, 3, 5, 6, 8, 7, 9, 10, 12, 20]
4th: [2, 3, 5, 6, 8, 7, 9, 10, 12, 20]
4th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
4th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 4th: 6
5th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
5th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
5th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
5th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
5th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 5th: 5
6th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
6th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
6th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
6th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 6th: 4
7th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
7th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
7th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 7th: 3
8th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
8th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 8th: 2
9th: [2, 3, 5, 6, 7, 8, 9, 10, 12, 20]
interation on 9th: 1
interation on 10th: 0
total iterations: 45
array sorted - ascending:
[2, 3, 5, 6, 7, 8, 9, 10, 12, 20]

Junilu Lacar
Sheriff
Posts: 11477
180
• 1
Line 25 - remove this local variable, there's no point in having it. You're not saving anything, not clock cycles, not memory (you're using more), not keystrokes (lame excuse to use a temporary variable and you're typing more chars than you're saving)

Line 26 - temp is declared in a scope that is wider than its actual use. Delete this line and declare the type of temp on line 34 instead, where you assign a value to it inside the if statement to swap two array elements.

Line 43 - you mis-spelled "iteration" - you have "interation"

As already mentioned, use your IDE to autoformat and properly indent your code. In Eclipse, it's Ctrl+Shift+F (Windows) or Cmd+Shift+F (Mac)

Junilu Lacar
Sheriff
Posts: 11477
180
Another variable you can get rid of is the iteration variable declared on line 30. If you delete that, you would also delete lines 40 & 44 and your inner for-loop would be:

phi tran
Greenhorn
Posts: 14
It looks like a great way to display differences in algorithmic processing.

Thank you

Consider making other sort routines and doing the same. You could maybe even make a little mini-library of dump routines in some logging class that you could snap into each one. You might find it fun, and a great way to critically examine all the assertions that are made about sorting algorithms.

I plan to do this with the other sorting algorithms. And thank you for the great idea about making a mini-library of dump routines. You're right, I think I will find it fun to do.

Thanks L Foster.

Line 25 - remove this local variable...
Line 26 - temp is declared in a scope that is wider than its actual use.

I made edits to line 25, 26 thanks! Mis-spelled is misspelled XD.

Thanks Junilu

Junilu Lacar
Sheriff
Posts: 11477
180
• 1
phi tran wrote:Mis-spelled is misspelled XD.

Touché

phi tran
Greenhorn
Posts: 14
thanks for being able to take a little joke

Nigel Browne
Ranch Hand
Posts: 704
Junilu Lacar wrote:
phi tran wrote:Mis-spelled is misspelled XD.

Touché

Misspelled is misspelt in the British isles

phi tran
Greenhorn
Posts: 14
Nigel Browne wrote:
Junilu Lacar wrote:
phi tran wrote:Mis-spelled is misspelled XD.

Touché

Misspelled is misspelt in the British isles

^^^hahaha