-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSortStringArray.java
More file actions
52 lines (40 loc) · 1.42 KB
/
QuickSortStringArray.java
File metadata and controls
52 lines (40 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
/**
Author: Rajin Santos Gajadhar
Student ID: 239479650
Assignment 2, Question 4
Any and all work in this file is my own.
*/
import java.util.Arrays;
public class QuickSortStringArray {
public static void quickSort(String[] arr, int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
}
}
private static int partition(String[] arr, int begin, int end) {
String pivot = arr[end];
int i = (begin - 1);
for (int j = begin; j < end; j++) {
if (arr[j].length() <= pivot.length()) {
i++;
String swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}
String swapTemp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = swapTemp;
return i + 1;
}
public static void main(String[] args) {
String[] input1 = {"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
quickSort(input1, 0, input1.length - 1);
System.out.println(Arrays.toString(input1));
String[] input2 = {"Dog", "Elephant", "Cat", "Hippopotamus", "Bear", "Fox", "Giraffe"};
quickSort(input2, 0, input2.length - 1);
System.out.println(Arrays.toString(input2));
}
}