|
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
|
|
|
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");
}
}
|
|
|
|
|
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?
|
|
|
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?
|
|
|
|
|
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);
}
}
|
|
|
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");
}
}
|
|
|
|
|
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 .
|
|
|
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
|
|
|
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
|
|
|
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");
}
}
|
|
|
|
|
|
|
|
|
|