permaculture playing cards*
The moose likes Beginning Java and the fly likes Sorting algorithms specially Bubble Sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting algorithms specially Bubble Sort" Watch "Sorting algorithms specially Bubble Sort" New topic
Author

Sorting algorithms specially Bubble Sort

Iqbal Ali
Greenhorn

Joined: Aug 21, 2001
Posts: 9
I have to work on implementing sort algorithms in a Java. These are:

1- Bubble Sort.
2- Quick Sort.
3- and Shell sort.

I think I can handle the 2nd and 3rd one but I am clueless about the 1st one.

Is there someone to help me out?


-----<br />If you think education is expensive, try ignorance.
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Homework time again!

Well, I'm rather sure your course notes or text has a pseudo-code algorithm for Bubble-sort. Even if its missing, a Google search is definitely able to help you get it, then you merely need to implement the algorithm into Java code.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Don't say it's Data Structures and Algorithms related subject. I am also currently studying in that subject in MSCS. But in my master course, I do not need to implement them at all. Rather I have to analyze the running time and detailed worst-case, average-case and best-case running of each of them...

I do miss the time when I had to implement them in Java, when I was in bachelor course... But, Iqbal Ali, don't be discourage... As Cheng Wei Lee suggested, you'd better learn with Pseudocode from the textbook and implement them in Java... Just concentrate on understanding how they work, then you'll be able to beat it...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Dorothy Finkel-Laverty
Ranch Hand

Joined: Nov 24, 2001
Posts: 51
Bubble sort is named "bubble" because the largest value (or the smallest value, depending on whther you use < or > in your comparison) will float to the top of the list, like a bubble in a tank of water.

Assume you have a list of numbers, a, b, and c. The bubble sort is a variation on a swap - you compare two numbers, if a > b, then swap a & b. Next, compare (the new) b to c. At the end of the first iteration, the largest value will be at the top of the list. Simply start at the bottom of the list, and do it again. You are done when you pass through the list and no swaps take place.

There are several variation on it to enhance processing time - such as:
the first time through, loop n times, where n is the number of items in the list - 1. Next time, loop n-2 times (because you know that the largest value is already at the top, there is no need to compare it and waste the cycles). I will leave you to discover the other variations.

Good luck!
Mary Wallace
Ranch Hand

Joined: Aug 25, 2003
Posts: 138
Iqbal,

Please check this site


hth,

mw

Originally posted by Iqbal Ali:
I have to work on implementing sort algorithms in a Java. These are:

1- Bubble Sort.
2- Quick Sort.
3- and Shell sort.

I think I can handle the 2nd and 3rd one but I am clueless about the 1st one.

Is there someone to help me out?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting algorithms specially Bubble Sort