is there any function in java to sort string array in (nlogn) time.
i got a function java.util.Arrays.sort(array);but it does not compare based on their length.it
works only on same length string.
so can anyone help me to know about whether/not any function exist. ???
java string array sorting
Moderator: Board moderators
-
- Experienced poster
- Posts: 136
- Joined: Sat Nov 29, 2008 8:01 am
- Location: narayangong,bangladesh.
- Contact:
java string array sorting
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
Re: java string array sorting
Please, don't post text in funny colors, and capitalize the first letter of each sentence.
java.utils.Arrays.sort(Object[]) uses mergesort, and so runs in Theta(n log n) time (assuming that each comparison takes O(1) time).
java.utils.Arrays.sort(Object[]) uses mergesort, and so runs in Theta(n log n) time (assuming that each comparison takes O(1) time).
What do you mean? It compares objects using their compareTo() function of java.lang.Comparable interface that they must implement. String's default comparison function is lexicographic, it works for strings of any size. If you don't like it, you can override it.sazzadcsedu wrote:i got a function java.util.Arrays.sort(array);but it does not compare based on their length.it works only on same length string.
-
- Experienced poster
- Posts: 136
- Joined: Sat Nov 29, 2008 8:01 am
- Location: narayangong,bangladesh.
- Contact:
Re: java string array sorting
Ok.
But compareTo function works on only same length string.
If String s[]={"aac","ab","abc","asd","a"};
Then it return :
a
aac
ab
abc
asd
Which is not based on length .so if i want to get like:
a
ab
aac
abc
asd
Then, is any function available??????
Or can you give me details/defination of (compareTo && java.utils.Arrays.sort(Object[])) function so that i can modify them.
But compareTo function works on only same length string.
If String s[]={"aac","ab","abc","asd","a"};
Then it return :
a
aac
ab
abc
asd
Which is not based on length .so if i want to get like:
a
ab
aac
abc
asd
Then, is any function available??????
Or can you give me details/defination of (compareTo && java.utils.Arrays.sort(Object[])) function so that i can modify them.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
Re: java string array sorting
Implement your comparison function using java.util.Comparator interface and pass an instance of that to java.util.Arrays.sort():
or even:
Code: Select all
import java.util.*;
class MyComparator implements Comparator<String> {
public int compare(String s, String t) {
if (s.length() != t.length())
return s.length() - t.length();
else
return s.compareTo(t);
}
}
public class Main {
public static void main(String[] args) {
Arrays.sort(args, new MyComparator());
for (String s : args)
System.out.println(s);
}
}
Code: Select all
import java.util.*;
public class Main {
public static void main(String[] args) {
Arrays.sort(args, new Comparator<String>() {
public int compare(String s, String t) {
if (s.length() != t.length())
return s.length() - t.length();
else
return s.compareTo(t);
}
});
for (String s : args)
System.out.println(s);
}
}
http://hg.openjdk.java.net/jdk6/jdk6-ga ... rrays.javaOr can you give me details/defination of (compareTo && java.utils.Arrays.sort(Object[])) function so that i can modify them.
-
- Experienced poster
- Posts: 136
- Joined: Sat Nov 29, 2008 8:01 am
- Location: narayangong,bangladesh.
- Contact:
Re: java string array sorting
Thanx mf for your help.Sorry for late reply.
Can you give me some critical i/o for 10364(squre)???
Can you give me some critical i/o for 10364(squre)???
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
Re: java string array sorting
No, why on earth would I have it?!sazzadcsedu wrote:Can you give me some critical i/o for 10364(squre)???
But that problem seems to be from Waterloo programing contests. They always publish their data and solutions, google them.