forked from theprogrammedwords/Algorithm-Solutions-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortArray.java
More file actions
34 lines (26 loc) · 900 Bytes
/
SortArray.java
File metadata and controls
34 lines (26 loc) · 900 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Given an array of n strings. Sort the array in lexicographical order.
import java.util.*;
class SortArray {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String arr[] = new String[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next();
String sortedArr[] = sortArray(n, arr);
for (String word : sortedArr)
System.out.print(word + " ");
}
static String[] sortArray(int n, String[] arr) {
for(int i = 0; i< n; i++){
for(int j = i+1; j<n ; j++){
if(arr[i].compareToIgnoreCase(arr[j]) > 0){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
}