Arrays.sort??? How good it is

S

Sanny

Arrays.sort uses the fastest way to sort the array.

But say I have a Array of lebgth 200. And I only want to sort first
20 / 30 elements can I tell that it has to sort only forst 30
elements?

And Say I have an Array

int[] cars=new int[200];

And it has a few random values.

I also have Other Array carname[]

String[] carname = new String[200];

What I want is when Array cars is Sorted the other Array carname is
also sorted.

Say cars[5]; is highest value and is at Top position then I want
carname[5] String also to be sorted to first place.

If I use Arrays.sort(cars) How will I update the corresponding Array
carsname[] So that both are Sorted Simultaniously

And will using Arrays.sort faster than using seperate function using
MergeSort/ Quick Sort.

And The Old Question Say cars[1] value has changed now I want to
insert it into the Array at a position where the Array is fully
Sorted. Currently I am using ArrayCopy to shift all elements and doing
binary search on the sorted Array cars to know the position where the
cars[1] Value should be shifted after it changes.

Is there any inbuilt routines for these work. Last Time I asked this
question. People talked about Linked List and I get errors using JDK
1.3 using linkedlist

List<Integer> cars = new LinkedList<Integer>();

I am getting Compilation error "value needed" May be I need JDK 1.5
for using above code and is not supported in JDK 1.3?

Bye
Sanny
 
L

Lew

Sanny said:
And Say I have an Array

int[] cars=new int[200];

And it has a few random values.

I also have Other Array carname[]

String[] carname = new String[200];

What I want is when Array cars is Sorted the other Array carname is
also sorted.

Say cars[5]; is highest value and is at Top position then I want
carname[5] String also to be sorted to first place.

If I use Arrays.sort(cars) How will I update the corresponding Array
carsname[] So that both are Sorted Simultaniously

Make a class that associates the two concepts. Do not keep them in separate
arrays with no logical connection.
List<Integer> cars = new LinkedList<Integer>();

I am getting Compilation error "value needed" May be I need JDK 1.5
for using above code and is not supported in JDK 1.3?

Generics were introduced in Java 5. Java 1.3 was retired some time ago. Java
1.4 will be retired this year.

Even Java 5 will enter End-of-Life this year.

Have you looked at the Sun Java web sites? All this information is there.

It's amazingly useful to read the documentation.
 
R

Roedy Green

But say I have a Array of lebgth 200. And I only want to sort first
20 / 30 elements can I tell that it has to sort only forst 30
elements?

Use System.arrayCopy to extract the first 30 elements and sort that.
 
R

Roedy Green

And will using Arrays.sort faster than using seperate function using
MergeSort/ Quick Sort.

The only sort I found quicker than Sun's was RadixSort, and even then,
not in the general case.

It is surprisingly well written.
 
R

Roedy Green

And The Old Question Say cars[1] value has changed now I want to
insert it into the Array at a position where the Array is fully
Sorted. Currently I am using ArrayCopy to shift all elements and doing
binary search on the sorted Array cars to know the position where the
cars[1] Value should be shifted after it changes.

Is there any inbuilt routines for these work. Last Time I asked this
question. People talked about Linked List and I get errors using JDK
1.3 using linkedlist

See http://mindprod.com/jgloss/binarysearch.html

The cheapy way to do it is just tack the new element on the end and
resort. This is not quite as awful as it first appears since Sun's
sort takes advantage of pre-existing order.
List<Integer> cars = new LinkedList<Integer>();

I am getting Compilation error "value needed" May be I need JDK 1.5
for using above code and is not supported in JDK 1.3?
Yes, generics only work in JDK 1.5+
 
J

Jeff Higgins

public class Car
implements Comparable<Car> {

private int car;
private String name;
private static boolean
carOrder = true;

public Car
(int car, String name,
boolean order) {
this.car = car;
this.name = name;
carOrder = order;
}

public int getCar() {
return car;
}

public String getName() {
return name;
}

public static boolean
getNaturalOrder() {
return carOrder;
}

public static void
setNaturalOrderToCar
(boolean order) {
carOrder = order;
}

public int compareTo(Car c) {
if (c instanceof Car) {
if (carOrder) {
if (this.car > ((Car) c).car)
return 1;
else if (this.car == ((Car) c).car)
return 0;
else
return -1;
} else {
return
this.name.compareTo(((Car) c).name);
}
} else
throw new IllegalArgumentException
("comparing apples to cars");
}

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


import java.util.Arrays;
import java.util.Random;

public class Cars {

private Car[] cars;
private boolean compareByCar;
private Random rand = new Random();

public Cars
(int initialCount,
boolean init,
int maxCar,
int nameLength,
boolean compareByCar) {

if (init)
init(initialCount,
maxCar,
nameLength,
compareByCar); }

public Car[]
getCars() {
return cars; }

public void // TODO
setCars(Car[] newCars) {}

public Car[]
sort(boolean comp) {

Car[] result =
Arrays.copyOf(cars, cars.length);

if (comp == Car.getNaturalOrder()) {
Arrays.sort((Car[])result);}
else {
Car.setNaturalOrderToCar(comp);
Arrays.sort((Car[])result); }

return result; }

public void // TODO
resize(int newSise){}

public void
replaceRange // TODO
(int position, Car[] replacement){}

public boolean
getCompareByCar() {
return compareByCar; }

public void
setCompareByCar(boolean comp) {
this.compareByCar = comp;
Car.setNaturalOrderToCar(comp); }

public void
init
(int initialCount,
int maxCar,
int nameLength,
boolean comp) {

this.compareByCar = comp;
Car.setNaturalOrderToCar(comp);
cars = new Car[initialCount];

for (int i = 0; i < cars.length; i++) {
cars =
new Car(
(rand.nextInt(maxCar - 0 + 1) + 0),
(randomName(nameLength)),
compareByCar); } }

private String
randomName(int size) {

char[] letters =
{ 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q',
'r', 's', 't', 'u', 'v', 'w',
'x', 'y','z' };

char[] buffer = new char[size];

for (int i = 0; i < size; i++) {
buffer =
letters[rand.nextInt(25 - 0 + 1) + 0]; }

return new String(buffer); }

}


public class TestCars {

public static void main(String[] args) {

Cars chevrolet =
new Cars(6, true, 75000, 5, true);

for (Car c : chevrolet.getCars()) {
System.out.println(c.toString()); }

System.out.println();

for (Car c : chevrolet.sort(false)) {
System.out.println(c.toString()); }
}
}
 
S

Sanny

public Car[]
sort(boolean comp) {

Car[] result =
Arrays.copyOf(cars, cars.length);

if (comp == Car.getNaturalOrder()) {
Arrays.sort((Car[])result);}
else {
Car.setNaturalOrderToCar(comp);
Arrays.sort((Car[])result); }

return result; }

How will sort(boolean comp) function Sort? Please Explain how this
function sorts each car based on
"int initialCount"

Bye
Sanny
 
J

Jeff Higgins

Sanny said:
public Car[]
sort(boolean comp) {

Car[] result =
Arrays.copyOf(cars, cars.length);

if (comp == Car.getNaturalOrder()) {
Arrays.sort((Car[])result);}
else {
Car.setNaturalOrderToCar(comp);
Arrays.sort((Car[])result); }

return result; }

How will sort(boolean comp) function Sort? Please Explain how this
function sorts each car based on
"int initialCount"

initialCount just tells the Cars class how many cars to start out with.

the Cars.sort(boolean comp) method takes a boolean argument,
that is whether to sort by Car.car (true) or Car.name(false).

Car implements the java.lang.Comparable interface,
see the car.compareTo(Car c) method for details.

Arrays.sort() sorts on the 'natural order' of the object in the array.
The natural order of Car is given by the java.lang.Comparable interface
implementation method Car.CompareTo(Car c).

All of this has been given by others upthread, see their good advice.
 
J

Jeff Higgins

Jeff said:
Sanny said:
public Car[]
sort(boolean comp) {

Car[] result =
Arrays.copyOf(cars, cars.length);

if (comp == Car.getNaturalOrder()) {
Arrays.sort((Car[])result);}
else {
Car.setNaturalOrderToCar(comp);
Arrays.sort((Car[])result); }

return result; }

How will sort(boolean comp) function Sort? Please Explain how this
function sorts each car based on
"int initialCount"

initialCount just tells the Cars class how many cars to start out with.

the Cars.sort(boolean comp) method takes a boolean argument,
that is whether to sort by Car.car (true) or Car.name(false).

Car implements the java.lang.Comparable interface,
see the car.compareTo(Car c) method for details.

Arrays.sort() sorts on the 'natural order' of the object in the array.
The natural order of Car is given by the java.lang.Comparable interface
implementation method Car.CompareTo(Car c).

This might be a more understandable implementation of
Car.compareTo(Car otherCar) method.

public int compareTo(Car otherCar) {
if (otherCar instanceof Car) {
return (carOrder) ?
Integer.valueOf(car).compareTo
(Integer.valueOf(otherCar.car)) :
name.compareTo(((Car)otherCar).name); }
else {
throw new ClassCastException
("comparing " + otherCar.getClass() + " to Car");
}
}
 
D

Daniel Pitts

Sanny said:
Arrays.sort uses the fastest way to sort the array.

But say I have a Array of lebgth 200. And I only want to sort first
20 / 30 elements can I tell that it has to sort only forst 30
elements?
Something like:
Collections.sort(Arrays.asList(myArray).subList(0, 30));
Note, this will only work with non-primitive arrays.
And Say I have an Array

int[] cars=new int[200];

And it has a few random values.

I also have Other Array carname[]

String[] carname = new String[200];

What I want is when Array cars is Sorted the other Array carname is
also sorted.

Say cars[5]; is highest value and is at Top position then I want
carname[5] String also to be sorted to first place.

If I use Arrays.sort(cars) How will I update the corresponding Array
carsname[] So that both are Sorted Simultaniously
I would suggest having a Car class that has the String and int in itself.
And will using Arrays.sort faster than using seperate function using
MergeSort/ Quick Sort.
It may not be, but in most situations it is Fast Enough. They happen to
use an optimized version of Quick Sort.
And The Old Question Say cars[1] value has changed now I want to
insert it into the Array at a position where the Array is fully
Sorted. Currently I am using ArrayCopy to shift all elements and doing
binary search on the sorted Array cars to know the position where the
cars[1] Value should be shifted after it changes.
I would suggest using some implementation of SortedSet, or using an
ArrayList instead.
Is there any inbuilt routines for these work. Last Time I asked this
question. People talked about Linked List and I get errors using JDK
1.3 using linkedlist

List<Integer> cars = new LinkedList<Integer>();

I am getting Compilation error "value needed" May be I need JDK 1.5
for using above code and is not supported in JDK 1.3?

Bye
Sanny

You need JDK1.5 to use Generics (the <Integer> part). You should be
using at least Java 5 anyway.


Hope this all helps,
Daniel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top