Hi, I need help getting started on this program:

Write a sort method that uses the bubble- sort algorithm. The bub-ble- sort algorithm makes several passes through the array. On each pass, suc-cessive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually bub-ble their way to the top and the larger values sink to the bottom. Use 6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5 to test the method.

I have taken java, this is my second time around, but we never covered searching or sorting the first time I took it. So, I'm not very familar or understanding this concept well enough. So, I was wondering if someone could provide me even a similar program that I might be able to follow and understand from that would be very helpful. That way I will get an idea of how it works, something fairly simple. This assignment is due July 8th. Thank you in advance! :)

@moutanna How does uncommented code explain how to design and code a program to solve a problem?

This is from an educational institution you cans send a mail to him. Please do not evoke a marginal issue as you did in the other thread. Thinks

Sorry, I thought it'd be better to teach students how to program. Show them the steps to solve a problem

But if you think it is better to give them a program with no explanation of how or why it works.

Just hope you're there the next time they want to solve a problem.

@student.09 -> What would you prefer, the answer or to know how to get the answer?

Sorry, for the delay in getting back to this thread, had final assignments to complete that it got hectic.

I have worked with moutanna in the past and he's always been able to help. I usually ask questions to understand what he's posted, so I learn that way...I guess learning backwards. But, I understand where you are coming from NormR1, don't worry I will learn this piece of program well before I go ahead and submit it this week.

I have taken java programming before, but this is the first time i'm having to do bubble sorting.
So, obviously this piece of program that moutanna has posted is just the method for sorting, I still need to write the main....correct? Because, when I complied and tried running it to test it, it didn't work.

As for the sorting it's just arrays, right? Could someone talk me through the steps of this method?

On each pass, successive neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise, the values remain unchanged.

Is this what you are talking about?

Yes, how does the program know how to compare those?
I'm not able to run it, so I'm not able to connect the logic.

Assuming the code is going thru an array.
To compare to adjacent elements have two indexes that differ by one: i and i-1 or i and i+1
It the two elements are in the wrong order, swap them; move one to a temp, move other to the first and move temp to the last.

So write a line of code that references two adjacent elements.

Write code to swap two elements in an array given their indexes.

Ok lets do it step by step:
Given an array, for example: int[] intArray = new int[]{4,5,3,2,1,6};
Write a loop that prints out two elements or the array on every loop:
1st & 2nd; 2nd & 3rd; 3rd & 4th
until all elements have been printed in pairs.

um okay let me see if I'm understnding what you are trying to explain:

we have a list of the inputted numbers and a variable TEMP, right?
so say we have an array of 5
the 1st number in that list would be compared to the following number--the 2nd number
if the first number is greater than the following then it's NOT sorted, therefore
that 1st number is assigned to the TEMP variable to hold it. then the 2nd number is equal to the first number -- I guess the 2nd number is being put into the place of the first. Then, the 1st number which was in TEMP is assigned back to the 2nd's place.

I know that may have been hard to follow so I guess let me try to represent my understanding by variables

a <--1
b <--2
c <--3
d <--4
e <--5

IF a > b
Sort = false
double TEMP = a
= b
[i+1] = TEMP <-- a

They are swapped like that, right? Would you say that I have got the idea that you were trying to explain to me?

Sorry NormR1 I hadn't seen your step by step post til after I posted the above post..

Write a small program and try it. It shouldn't take over 10-15 lines of code total.

do I need to add the main in there or just the method?
how do you try/test just a method in textpad?

A fully executable program requires a main() (applets excepted)

To understand the logic of the bubble sort please follow this link :

to test it's behavior you should add a main method in the same class or create a new one that contain a main method:
Here I'll add the main method in the same class I referenced in the previous post.

public class BubbleSort {
	  public static void bubbleSort(double[] list) {
	    boolean sorted = false;
	    for (int top = list.length - 1; top > 0 && !sorted; top--) {
	      sorted = true;
	      for (int i = 0; i < top; i++) {
	        if (list[i] > list[i+1] ) {
	          sorted = false;
	          double temp = list[i];
	          list[i] = list[i+1];
	          list[i+1] = temp;
public static void main(String[] args){

double[] in={8, 9, 6, 1, 2, 4, 5};

for(int i =0;i<in.length; ++i)


Now you need to compile the class using the "javac" command to build the class file and use the java command for execution:

Hope it helps.

oOoo, I like that link that explained the lines of code that was really helpful to understand of why what was put where. Thank you.
And, yes yesterday I had figured out that I needed to add a main, so I went ahead and did that for the numbers I was given to test for, it works perfectly! Thank you so much for your help once again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.