Skip to content

Commit 5faa8d1

Browse files
committed
feat: Sort array of strings alphabetically using Arrays.sort()
🧠 Logic: - Initialize a string array with various programming language names. - Use `Arrays.sort()` to perform lexicographical (dictionary order) sorting. - Print each string from the sorted array using enhanced for loop. Signed-off-by: Somesh diwan <[email protected]>
1 parent 4dddb42 commit 5faa8d1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
public class SortTheString {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
String arr[]={"java","python","pascal","smalltalk","ada","basic"};
54

65
java.util.Arrays.sort(arr);
76

87
for(String x:arr)
98
System.out.println(x);
10-
119
}
1210
}
1311

1412
/*
1513
Array Initialization:
1614
1715
An array arr is created with strings: {"java", "python", "pascal", "smalltalk", "ada", "basic"}.
18-
Sorting:
1916
20-
The java.util.Arrays.sort(arr) method is used to sort the array. It sorts the elements in natural (lexicographical) order by default.
17+
Sorting:
18+
The java.util.Arrays.sort(arr) method is used to sort the array.
2119
20+
It sorts the elements in natural (lexicographical) order by default.
2221
2322
4. What would happen if one of the strings in the array had an uppercase letter, e.g., "Java" instead of "java"?
24-
Strings with uppercase letters will be sorted before lowercase strings because uppercase letters have lower Unicode values than lowercase letters.
23+
24+
Strings with uppercase letters will be sorted before lowercase strings because uppercase letters have
25+
lower Unicode values than lowercase letters.
26+
2527
For example,
2628
if the array were {"Java", "python", "pascal", "smalltalk", "ada", "basic"}, the output would be:
2729
28-
2930
5. Can the Arrays.sort() method sort the strings in reverse order? How would you modify the code to achieve this?
3031
Yes, to sort strings in reverse order, use Arrays.sort() with a custom comparator, such as:
3132
3233
java.util.Arrays.sort(arr, java.util.Collections.reverseOrder());
3334
3435
6. What is the time complexity of the Arrays.sort() method used in the program?
35-
The time complexity of Arrays.sort() for an array of strings is O(n log n), where n is the number of elements in the array. This is because it uses the dual-pivot Quicksort algorithm for non-primitive types.
36+
The time complexity of Arrays.sort() for an array of strings is O(n log n), where n is the number of elements in the
37+
array.
38+
39+
This is because it uses the dual-pivot Quicksort algorithm for non-primitive types.
3640
3741
10. How does the Arrays.sort() method compare strings internally? Discuss how it uses lexicographical order.
3842
Arrays.sort() internally uses the compareTo() method of the String class.
39-
The compareTo() method compares strings lexicographically by comparing their Unicode values character by character. If one string is a prefix of the other, the shorter string is considered smaller.
40-
*/
43+
The compareTo() method compares strings lexicographically by comparing their Unicode values character by character.
44+
If one string is a prefix of the other, the shorter string is considered smaller.
45+
*/

0 commit comments

Comments
 (0)