![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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 larger than b c == 0 iff a is lexicographically equal to b hope this helps a little bit -Dizz |
|
|
|
|
|
#5 |
|
Programmer
|
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? |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2005
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jul 2005
Posts: 6
Rep Power: 0
![]() |
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();
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|