3 Replies - 25111 Views - Last Post: 29 September 2008 - 04:04 PM Rate Topic: -----

#1 CainSonozaki  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 29-September 08

Bubble Sort String

Posted 29 September 2008 - 12:31 PM

This is what i need

A BubbleSort class with a method called sort that accepts an array of Strings and sorts the array in
alphabetical order (regardless of case) and a method called print that displays the strings in the array.
The algorithm should implement one efficiency improvement in that once a list is sorted, the algorithm
should end rather than continuing to attempt to sort. So if an array of 100 sorted items is given to the
sort routine, the algorithm should only make 1 pass before ending.
Example
Sorting a list such as:
arr = {“Banana”, “Apple”, “artichoke”};
sorter.sort();
sorter.print();
would display:
Apple
artichoke
Banana


This is what i have so far though im not sure if im doing it the right way and i still need it to ignore the case when sorting
import java.util.*;

public class BubbleSort {
	
	public static void main(String[] args) {
		
		//Declare and Initialize Array
		String list[]={"artichoke" , "Apple" , "CHERRY" , "banana"};
		BubbleSort(list);
		//Begin For loop
		for(int i=0; i<list.length; i++)
		{
			System.out.println(list[i]);
		}
	}// End of Main
	
	private static void BubbleSort(String[] array) {
		String temp;
		// Begin For loop
		for(int i=0; i<array.length; i++) {
			//Open For loop
			for(int j=0; j<array.length-1-i; j++) {
				//Open If Statement Compare and sort strings
			if(array[j].compareTo(array[j+1])>0) {
				temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}//End of If Statement
		}//End of For Loop
		}//End of For Loop
	}//End of Private Class BubbleSort
}// End of Public class BubbleSort




Is This A Good Question/Topic? 1

Replies To: Bubble Sort String

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5766
  • View blog
  • Posts: 12,580
  • Joined: 16-October 07

Re: Bubble Sort String

Posted 29 September 2008 - 01:25 PM

Doesn't look like a bubble sort. More like an upside down selection sort with a bug.

You probably want code that looks like this, from your example:
import java.util.*;

public class BubbleSort {
	public static void main(String[] args) {
		String list[]={"artichoke" , "Apple" , "CHERRY" , "banana"};
		BubbleSort sorter = BubbleSort();
		sorter.sort(list);
		sorter.print(list);
	}
	
	public void sort(String[] list) {
		// ...
	}
	
	public void print(String[] list) {
		for(int i=0; i<list.length; i++) {
			System.out.println(list[i]);
		}
	}
}



Or, you can initialize the object with the array, if you like.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 CainSonozaki  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 29-September 08

Re: Bubble Sort String

Posted 29 September 2008 - 03:17 PM

View Postbaavgai, on 29 Sep, 2008 - 01:25 PM, said:

Doesn't look like a bubble sort. More like an upside down selection sort with a bug.

You probably want code that looks like this, from your example:
import java.util.*;

public class BubbleSort {
	public static void main(String[] args) {
		String list[]={"artichoke" , "Apple" , "CHERRY" , "banana"};
		BubbleSort sorter = BubbleSort();
		sorter.sort(list);
		sorter.print(list);
	}
	
	public void sort(String[] list) {
		// ...
	}
	
	public void print(String[] list) {
		for(int i=0; i<list.length; i++) {
			System.out.println(list[i]);
		}
	}
}



Or, you can initialize the object with the array, if you like.

Hope this helps.


I tried using that but it gave me this error

> "C:\Program Files\Java\jdk1.6.0_04\bin\javac.exe" BubbleSort.java
BubbleSort.java:7: cannot find symbol
symbol : method BubbleSort()
location: class BubbleSort
BubbleSort sorter = BubbleSort();
^
1 error

> Terminated with exit code 1.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5766
  • View blog
  • Posts: 12,580
  • Joined: 16-October 07

Re: Bubble Sort String

Posted 29 September 2008 - 04:04 PM

View PostCainSonozaki, on 29 Sep, 2008 - 06:17 PM, said:

symbol : method BubbleSort()
location: class BubbleSort
BubbleSort sorter = BubbleSort();
^


Sorry, forgot a new: BubbleSort sorter = new BubbleSort();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1