java help once again..

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

Write a program that implements a method that receives an array parameter and sorts that array using the bubble-sort algorithm. The bubble-sort algorithm makes several passes through the array. On each pass, successive 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 because the smaller values gradually "bubble" their way to the top. The algorithm may be described as follows: just expecting basic tips from gurus and funny programs are also welcome. boolean changed; do{   changed = false;   for(int i = 0; i < list.length - 1; i++){     if(list[i] > list[i + 1]){        swap list[i] with list[i + 1];       changed = true;      }   } }while(changed); Continue to properly document your source code. Write this program as if you were explaining it to someone new to arrays. Fully document your code in such a way newcomers to Java will understand and be able to implement a Java array. Your grade on this assignment will be based on your thoroughness of documentation as well as you correctness of code.

redlotus · Feb 11, 2009 6:49 PM · 2,858 views

2 Replies

boolean sorted = false; // sort the array while (!sorted) { sorted = true; for (int i=0; i < 9; i++) { if (n[i] > n[i+1]) { int temp = n[i]; n[i] = n[i+1]; n[i+1] = temp; sorted = false; } } }

serial · Feb 11, 2009 9:03 PM

public class BubbleSort {   private long[] a;   private int nElems;   public BubbleSort(int max) {     a = new long[max];     nElems = 0;   }   //   put element into array   public void insert(long value) {     a[nElems] = value;     nElems++;   }   //   displays array contents   public void display() {     for (int j = 0; j < nElems; j++)       System.out.print(a[j] + " ");     System.out.println("");   }   public void bubbleSort() {     int out, in;     for (out = nElems - 1; out > 1; out--)       // outer loop (backward)       for (in = 0; in < out; in++)         // inner loop (forward)         if (a[in] > a[in + 1]) // out of order?           swap(in, in + 1); // swap them   }   private void swap(int one, int two) {     long temp = a[one];     a[one] = a[two];     a[two] = temp;   }   public static void main(String[] args) {     int maxSize = 100; // array size     BubbleSort arr; // reference to array     arr = new BubbleSort(maxSize);      arr.insert(77); // insert 10 items     arr.insert(66);     arr.insert(44);     arr.insert(34);     arr.insert(22);     arr.insert(88);     arr.insert(12);     arr.insert(00);     arr.insert(55);     arr.insert(33);     arr.display();      arr.bubbleSort();     arr.display();    } }

Nas · Feb 11, 2009 9:06 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all