DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2006
    Posts
    1

    Bubble Sort and Insertion Sort help.

    Hello,
    I need help sorting this program with bubble sort in alphabetical order.

    class Student {
    int age;
    String name,
    id;

    Student(int a, String n, String i)
    {
    age = a;
    name = n;
    id = i;
    }
    public String toString()
    {
    return (age + ", " + name + ", " + id);
    }
    }

    public class EjeBubbleSort2{
    public static void main(String args[]){
    Student lista[] = new Student[6], tempValue;

    int i,
    j;

    lista[0] = new Student(23,"Jose" , "11111111");
    lista[1] = new Student(43,"Maria" , "22222222");
    lista[2] = new Student(28,"Luis" , "33333333");
    lista[3] = new Student(32,"Juan" , "44444444");
    lista[4] = new Student(15,"Carmen", "55555555");
    lista[5] = new Student(23,"Luisa" , "66666666");

    for (i = 0; i <lista.length - 1; i++ )
    {
    System.out.println("Pass # " + (i+1));
    for (j = 0; j<lista.length-i-1; j++)
    {
    if (lista[j].age > lista[j+1].age)
    {
    System.out.println("Exchange " + lista[j] +
    " <-> " + lista[j+1]);

    tempValue = lista[j];
    lista[j] = lista[j+1];
    lista[j+1] = tempValue;
    }
    }
    }
    for (i=0; i<lista.length; i++)
    {
    System.out.println(lista[i]);
    }
    }
    }

    After that I need to numerically sort it with Insertion Sort.

    Any experts could please help me?
    I attached the bubble sort in numerical order.
    Thank you.

  2. #2
    Join Date
    Oct 2005
    Posts
    4

    Hope this resolves your problem

    Hello,
    Here is the program that bubbles sort student array in alphabatical order.
    ---------------------------------
    class Student {
    int age;
    String name,
    id;

    Student(int a, String n, String i)
    {
    age = a;
    name = n;
    id = i;
    }
    public String toString()
    {
    return (age + ", " + name + ", " + id);
    }
    }

    public class EjeBubbleSort2{
    public static void main(String args[]){
    Student lista[] = new Student[6], tempValue;

    int i,
    j;

    lista[0] = new Student(23,"Jose" , "11111111");
    lista[1] = new Student(43,"Maria" , "22222222");
    lista[2] = new Student(28,"Luis" , "33333333");
    lista[3] = new Student(32,"Juan" , "44444444");
    lista[4] = new Student(15,"Carmen", "55555555");
    lista[5] = new Student(23,"Luisa" , "66666666");

    for (i = 0; i <lista.length - 1; i++ )
    {
    System.out.println("Pass # " + (i+1));
    for (j = 0; j<lista.length-i-1; j++)
    {
    if (lista[j].name.compareTo(lista[j+1].name)>0)
    {
    System.out.println("Exchange " + lista[j] +
    " <-> " + lista[j+1]);

    tempValue = lista[j];
    lista[j] = lista[j+1];
    lista[j+1] = tempValue;
    }
    }
    }
    for (i=0; i<lista.length; i++)
    {
    System.out.println(lista[i]);
    }
    }
    }
    ---------------------------------

    Here is the program that performs a insertion sort on student array in numrical order.

    class Student {
    int age;
    String name,
    id;

    Student(int a, String n, String i)
    {
    age = a;
    name = n;
    id = i;
    }
    public String toString()
    {
    return (age + ", " + name + ", " + id);
    }
    }

    public class EjeBubbleSort1{
    public static void main(String args[]){
    Student lista[] = new Student[6], tempValue;

    int i;
    int j;

    lista[0] = new Student(23,"Jose" , "11111111");
    lista[1] = new Student(43,"Maria" , "22222222");
    lista[2] = new Student(28,"Luis" , "33333333");
    lista[3] = new Student(32,"Juan" , "44444444");
    lista[4] = new Student(15,"Carmen", "55555555");
    lista[5] = new Student(23,"Luisa" , "66666666");

    for (i = 1; i <lista.length; i++ )
    {
    System.out.println("Pass # " + (i));

    for (j = i; j>0; j--)
    {
    if (lista[j].age < lista[j-1].age)
    {
    System.out.println("Exchange " + lista[j] +
    " <-> " + lista[j-1]);

    tempValue = lista[j];
    lista[j] = lista[j-1];
    lista[j-1] = tempValue;
    }
    }

    }
    for (i=0; i<lista.length; i++)
    {
    System.out.println(lista[i]);
    }
    }
    }

    Hope this resolves your problem.
    mcaashish

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links