Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting algorithms specially Bubble Sort

 
Iqbal Ali
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic