codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Help !Sorting Problem !!!  happygirl at 08:47 on Sunday, February 26, 2006
 

I facing problem in this question :

Create a class Sorter and implement the quick sort algorithm. Test with an arrays of object of 3 different classes selected from the standard Java lang package that implement the Comaparable interface i.e Integer etc. Include an output method in Sorter to display the contents of the arrays before and after the sort.

Actually i confused with the Java lang package , i don't know how to use to class there , and the comparable interface. Can show me any example and explanation about this ? thanks !!

<Added>

about array sorting

  Re: Help !Sorting Problem !!!  crwood at 17:10 on Monday, February 27, 2006
 


import java.util.Random;

public class SorterTest
{
public static void main(String[] args)
{
Random seed = new Random();
Integer[] i = new Integer[5];
for(int j = 0; j < i.length; j++)
i[j] = new Integer(seed.nextInt(26));
Sorter.sort(i);

String domain = "abcdefghijklmnopqrstuvwxyz";
String[] s = new String[6];
for(int j = 0; j < s.length; j++)
{
s[j] = "";
for(int k = 0; k < 4; k++)
s[j] += String.valueOf(domain.charAt(seed.nextInt(domain.length())));
}
Sorter.sort(s);

Byte[] b = new Byte[8];
byte[] buffer = new byte[1];
for(int j = 0; j < b.length; j++)
{
seed.nextBytes(buffer);
b[j] = new Byte(buffer[0]);
}
Sorter.sort(b);
}
}

class Sorter
{
public static void sort(Object[] o)
{
showArray(o, "before sort");
sortArray(o);
showArray(o, "after sort");
}

private static void sortArray(Object[] o)
{
if(!(o[0] instanceof Comparable))
throw new IllegalArgumentException("must implement Comparable inteface");
for(int j = 0; j < o.length; j++)
{
for(int k = j+1; k < o.length; k++)
{
if(((Comparable)o[k]).compareTo((Comparable)o[j]) < 0)
{
Object temp = o[j];
o[j] = o[k];
o[k] = temp;
}
}
}
}

private static void showArray(Object[] o, String s)
{
System.out.println(s);
String id = o[0].getClass().getName();
String name = id.substring(id.lastIndexOf(".")+1);
System.out.print(name + "[");
for(int j = 0; j < o.length; j++)
{
System.out.print(o[j]);
if(j < o.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}
}


  Re: Help !Sorting Problem !!!  happygirl at 02:22 on Tuesday, February 28, 2006
 

Thank you for ur help here !! ! Let say i using the public string to String method to test on an array of such object (also using quick sort) i.e StudentMark or Top football scorer that must be implement the Comparable interface, is it still follow the same way like previous question?

  Re: Help !Sorting Problem !!!  happygirl at 02:25 on Tuesday, February 28, 2006
 

Thank you for ur help here !! ! Let say i using the public string to String method to test on an array of such object (also using quick sort) i.e StudentMark or Top football scorer that must be implement the Comparable interface, is it still follow the same way like previous question?

  Re: Help !Sorting Problem !!!  crwood at 18:40 on Tuesday, February 28, 2006
 

It should. Try it.

  Re: Help !Sorting Problem !!!  happygirl at 02:29 on Wednesday, March 01, 2006
 

the code u provide me was not accepted , we must use the following code , but it still got problem with the declaration of object []a , can u tell me how to declare it ? thanks!

import java.util.Random;

public class Sorter
{

private static void displayData(Object [] a, String s)
{

System.out.println(s);
String id = a[0].getClass().getName();
String name = id.substring(id.lastIndexOf(".")+1);
System.out.print(name + "[");
for(int j = 0; j < a.length; j++)
{
System.out.print(a[j]);
if(j < a.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}


public static void quicksort (Object [] a, int left, int right)
{

if (left < right) {
int p = partition(a, left, right);
quicksort(a, left, p-1);
quicksort(a, p+1, right);
}

}

public static int partition(Object [] a, int left, int right)
{

int pivot = a[left];
int p = left;
for (int r = left+1; r <= right; r++)
{
if(a[r]<pivot)
{
a[p] = a[r];
a[r] = a[p+1];
a[p+1] = pivot;
p++;
}
}
return p;
}

public static void main(String[] args)
{

Random seed = new Random();
Integer[] i = new Integer[5];
for(int j = 0; j < i.length; j++)
i[j] = new Integer(seed.nextInt(26));


System.out.print("Content array before sorting :\n");
displayData(i);

System.out.println("Content array after being sorted :\n");
quicksort(i,0,i.length-1);
displayData(i);

String domain = "abcdefghijklmnopqrstuvwxyz";
String[] s = new String[6];
for(int j = 0; j < s.length; j++)
{
s[j] = "";
for(int k = 0; k < 4; k++)
s[j] += String.valueOf(domain.charAt(seed.nextInt(domain.length())));
}

System.out.print("Content array before sorting :\n");
displayData(s);
System.out.println("Content array after being sorted :\n");
quicksort(s,0,s.length-1);
displayData(s);
//arrange.sort(s);

Byte[] b = new Byte[8];
byte[] buffer = new byte[1];
for(int j = 0; j < b.length; j++)
{
seed.nextBytes(buffer);
b[j] = new Byte(buffer[0]);
}

System.out.print("Content array before sorting :\n");
displayData(b);
System.out.println("Content array after being sorted :\n");
quicksort(b,0,b.length-1);
displayData(b);

}
}



  Re: Help !Sorting Problem !!!  crwood at 08:19 on Wednesday, March 01, 2006
 

Works okay after changes made:

import java.util.Random;

public class Sorter1
{
private static void displayData(Object[] a, String s)
{
System.out.println(s);
String id = a[0].getClass().getName();
String name = id.substring(id.lastIndexOf(".")+1);
System.out.print(name + "[");
for(int j = 0; j < a.length; j++)
{
System.out.print(a[j]);
if(j < a.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}

public static void quicksort (Object [] a, int left, int right)
{
if (left < right) {
int p = partition(a, left, right);
quicksort(a, left, p-1);
quicksort(a, p+1, right);
}
}

public static int partition(Object [] a, int left, int right)
{
Object pivot = a[left];
int p = left;
for (int r = left+1; r <= right; r++)
{
if(((Comparable)a[r]).compareTo((Comparable)pivot) < 0)
{
a[p] = a[r];
a[r] = a[p+1];
a[p+1] = pivot;
p++;
}
}
return p;
}

public static void main(String[] args)
{
Random seed = new Random();
Integer[] i = new Integer[5];
for(int j = 0; j < i.length; j++)
i[j] = new Integer(seed.nextInt(26));

displayData(i, "Content array before sorting :\n");

quicksort(i,0,i.length-1);
displayData(i, "Content array after being sorted :\n");

String domain = "abcdefghijklmnopqrstuvwxyz";
String[] s = new String[6];
for(int j = 0; j < s.length; j++)
{
s[j] = "";
for(int k = 0; k < 4; k++)
s[j] += String.valueOf(domain.charAt(seed.nextInt(domain.length())));
}

displayData(s, "Content array before sorting :\n");
quicksort(s,0,s.length-1);
displayData(s, "Content array after being sorted :\n");
// arrange.sort(s);

Byte[] b = new Byte[8];
byte[] buffer = new byte[1];
for(int j = 0; j < b.length; j++)
{
seed.nextBytes(buffer);
b[j] = new Byte(buffer[0]);
}

displayData(b, "Content array before sorting :\n");
quicksort(b,0,b.length-1);
displayData(b, "Content array after being sorted :\n");
}
}


  Re: Help !  happygirl at 08:35 on Thursday, March 02, 2006
 

how i going to delete all those thing i publish in this forum, coz the coding here if found by university may consider plagarismn ? Thanks pls show me how to delete it .

  Re: Help !Sorting Problem !!!  happygirl at 10:46 on Sunday, March 05, 2006
 

I want to sort an java Object such as : StudentAge, this program will using below algorithm to sort it , how i going to implement it ?

public static void quicksort(Object [] a, int left, int right)
{

if (left < right) {
int p = partition(a, left, right);
quicksort(a, left, p-1);
quicksort(a, p+1, right);
}


public static int partition(Object [] a, int left, int right)
{

Object pivot = a[left];
int p = left;
for (int r = left+1; r <= right; r++)
{
if(((Comparable)a[r]).compareTo((Comparable)pivot) < 0)
{
a[p] = a[r];
a[r] = a[p+1];
a[p+1] = pivot;
p++;
}
}
return p;
}


the output must be like that :

before sorting :
Lisa : 30
Andy : 25
Winson :19

After sorting :

winson : 19
Andy : 25
Lisa : 30



  Re: Help !Sorting Problem !!!  happygirl at 10:46 on Sunday, March 05, 2006
 

I want to sort an java Object such as : StudentAge, this program will using below algorithm to sort it , how i going to implement it ?

public static void quicksort(Object [] a, int left, int right)
{

if (left < right) {
int p = partition(a, left, right);
quicksort(a, left, p-1);
quicksort(a, p+1, right);
}


public static int partition(Object [] a, int left, int right)
{

Object pivot = a[left];
int p = left;
for (int r = left+1; r <= right; r++)
{
if(((Comparable)a[r]).compareTo((Comparable)pivot) < 0)
{
a[p] = a[r];
a[r] = a[p+1];
a[p+1] = pivot;
p++;
}
}
return p;
}


the output must be like that :

before sorting :
Lisa : 30
Andy : 25
Winson :19

After sorting :

winson : 19
Andy : 25
Lisa : 30



  Re: Help !Sorting Problem !!!  crwood at 06:47 on Tuesday, March 07, 2006
 


public class SortByAge
{
public static void main(String[] args)
{
String[] names = { "Lisa", "Andy", "Winson" };
int[] ages = { 30, 25, 19 };
Person[] people = new Person[ages.length];

for(int j = 0; j < people.length; j++)
people[j] = new Person(names[j], ages[j]);
Sorter.sort(people);
}
}

class Person implements Comparable
{
String name;
int age;

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

public int compareTo(Object o)
{
return new Integer(age).compareTo(new Integer(((Person)o).age));
}

public String toString()
{
return name + " : " + age;
}
}

class Sorter
{
public static void sort(Object[] o)
{
showArray(o, "before sort");
sortArray(o);
showArray(o, "after sort");
}

private static void sortArray(Object[] o)
{
if(!(o[0] instanceof Comparable))
throw new IllegalArgumentException("must implement Comparable inteface");
for(int j = 0; j < o.length; j++)
{
for(int k = j+1; k < o.length; k++)
{
if(((Comparable)o[k]).compareTo((Comparable)o[j]) < 0)
{
Object temp = o[j];
o[j] = o[k];
o[k] = temp;
}
}
}
}

private static void showArray(Object[] o, String s)
{
System.out.println(s);
String id = o[0].getClass().getName();
String name = id.substring(id.lastIndexOf(".")+1);
System.out.print(name + "[");
for(int j = 0; j < o.length; j++)
{
System.out.print(o[j]);
if(j < o.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: need help in applet communication
•  Re: MS Access connection in JSP
•  Highlight a DataGrid Row and Stay there -
•  Numeric field
•  Re: visible/invisible divs
•  radio buttons questionnaire
•  Re: Splitter in DHTML, no frames
•  Re: how to set value to texbox
•  Re: IE page Redirect


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006