-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigSorting.java
More file actions
55 lines (52 loc) · 1.42 KB
/
BigSorting.java
File metadata and controls
55 lines (52 loc) · 1.42 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int partition(String arr[], int low, int high)
{
String pivot = arr[high];
int i = (low-1);
for (int j=low; j<high; j++)
{
if (arr[j].length() < pivot.length())
{
i++;
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
if (arr[j].length() == pivot.length() && pivot.compareTo(arr[j]) >= 0)
{
i++;
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
String temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
static void sort(String arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] arr = new String[n];
for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
arr[unsorted_i] = in.next();
}
sort(arr,0,n-1);
for(int i = 0;i<n;i++)System.out.println(arr[i]);
}
}