Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 20th, 2005, 10:43 AM   #1
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
Bubble Sort

Hi there,

I'm prett new to Java and I can do basic things, such as create menus and the basi "Hello world" thing and have done various other exercises.

I need to do a bubble sort where I have to enter and read characters from an array. I need to be able to edit names into the arrays with a maximum character length and sort these names into albhabetical order using bubble sort.

This is an exercise i am currently doing in class and I am struggling with. The lecturers do nothing with helping or advising me they sit and twiddle their thumbs.

I have a Java API index thingy which doesn't help much and am really struggling to connect with Java.

Any help of advice greatly appreciated.

Steve
JavaDummy is offline   Reply With Quote
Old Jun 20th, 2005, 11:03 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
do you have any code that you have used to try this out? as its much easier to help then do.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 20th, 2005, 11:14 AM   #3
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
No, no code to work with. i do have some bubble sort code which pertains to comparing integers and numbers, but not text. We were shown how to do it using numbers and i can understand that but I don't exactly know how to compare texts within arrays.

I do know you have to use a String method, and I can intiliase the number of arrays quite easily.

Steve
JavaDummy is offline   Reply With Quote
Old Jun 20th, 2005, 11:38 AM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
if you have variables of type String, you can use the String.compareTo() method to compare them. ex:
String a = "bla"
String b = "alb"
int c = a.compareTo(b);
System.out.print(c);
c < 0 iff a is lexicographically less than b
c > 0 iff a is lexicographically larger than b
c == 0 iff a is lexicographically equal to b

hope this helps a little bit
-Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jun 20th, 2005, 11:42 AM   #5
EdSalamander
Programmer
 
EdSalamander's Avatar
 
Join Date: Dec 2004
Location: Tucson, AZ, USA
Posts: 80
Rep Power: 4 EdSalamander is on a distinguished road
Send a message via AIM to EdSalamander
The Comparable interface is your friend. All hail the Comparable interface.. *insert droning chanting*

__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose?
EdSalamander is offline   Reply With Quote
Old Jun 20th, 2005, 12:16 PM   #6
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
Thanks Dizz, that should help.

How can i define the maximum length of the arrays? ie change it so it become 30 characters maximum?

Steve
JavaDummy is offline   Reply With Quote
Old Jun 21st, 2005, 8:54 AM   #7
JavaDummy
Newbie
 
Join Date: Jun 2005
Posts: 10
Rep Power: 0 JavaDummy is on a distinguished road
Hi,

I have some code now for this bubble sort:

import java.io.*;
public class BubbleSorting {

public static void main(String args[]) throws IOException
{
String[] list = { "Julie", "Stevie", "Richard", "Daniel", "Adam", "Colin", "Lauren", "Kelly", "Jim", "George" };
String temp;
int i,j,x;
System.out.println("*****Unsorted List*****");
System.out.println("-----------------");
System.out.println("-----------------");
for(i=0;i<=9;i++)
	  System.out.println(list[i]);
	  System.out.println("-----------------");
	  System.out.println("-----------------");
for(i=1;i<=8;i++) {
	for(j=0;j<=(9-i);j++) {
		if((x=list[j].compareTo(list[j+1]))>0) {
			temp = list [j];
			list[j] = list[j+1];
			list[j+1] = temp;
}
}
}
System.out.println("*****Sorted List*****");
for(i=0;i<=9;i++)
	  System.out.println(list[i]);
	  System.out.println("-----------------");
      System.out.println("-----------------");
}
}

I now need to allow entry of more names and repeat the sort until the word "quit" is entered and ensure that the maximum length of the array is 30.

Can anyone advise?

Thanks in advance.
Steve
JavaDummy is offline   Reply With Quote
Old Jul 8th, 2005, 1:11 AM   #8
HelloWorldProgram
Newbie
 
Join Date: Jul 2005
Posts: 6
Rep Power: 0 HelloWorldProgram is on a distinguished road
Ok

This is an example I made to sort people by their personal info, You have to modify it so that it sorts by whatever you want, by lastname, firstname or age.

class Person {

	private String lastname, firstname;

	private int age;

	public Person(String lastname, String firstname, int age) {
		this.lastname = lastname;
		this.firstname = firstname;
		this.age = age;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getFirstname() {
		return firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void DisplayPerson() {

		System.out.println(lastname + ", " + firstname + ", " + age);

	}
}

class Sorting {

	Person[] p;

	int size;

	int nElems;

	public Sorting(int size) {

		this.size = size;
		p = new Person[size];
		int nElems = 0;
	}

	public void Insert(String lastname, String firstname, int age) {

		p[nElems] = new Person(lastname, firstname, age);
		nElems++;
	}

	public void Display() {

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

			p[i].DisplayPerson();
			//System.out.println(" ");
		}
	}

	public void sort() {

		int i, j;

		for (i = 0; i < p.length - 1; i++) {
			for (j = 0; j < p.length - (i + 1); j++) {

				if ((p[j].getLastname()).compareTo(p[j + 1].getLastname()) > 0) {

					Person temp;
					temp = p[j];
					p[j] = p[j + 1];
					p[j + 1] = temp;
				}
			}
		}
	}
}

class Driver {

	public static void main(String args[]) {

		Sorting s = new Sorting(5);

		s.Insert("martinez", "gabriel", 24);
		s.Insert("severo", "roberto", 54);
		s.Insert("molina", "hilda", 74);
		s.Insert("caridad", "olga", 54);
		s.Insert("chente", "ainadys", 23);

		System.out.println("Before sorting: ");
		s.Display();

		s.sort();

		System.out.println("After sorting: ");
		s.Display();

	}
}
HelloWorldProgram is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC