how to quicksort a string array in java

Solutions on MaxInterview for how to quicksort a string array in java by the best coders in the world

showing results for - "how to quicksort a string array in java"
Alex
15 Jul 2020
1package com.javacodegeeks.sorting.quicksort;
2 
3public class QuicksortStringExample {
4 
5    private static String []a;
6    public static void main(String[] args) {
7        // Get an String array
8        a = new String[]{"X","E","C","A"};
9         
10        // prints the given array
11        printArray();
12         
13        // sort the array
14        sort();
15         
16        System.out.println("");
17         
18        //prints the sorted array
19        printArray();
20         
21    }
22     
23    // This method sort an array internally and internally calls quickSort 
24    public static void sort(){
25        int left = 0;
26        int right = a.length-1;
27             
28        quickSort(left, right);
29    }
30     
31    // This method is used to sort the array using quicksort algorithm.
32    // It takes left and the right end of the array as two cursors
33    private static void quickSort(int left,int right){
34         
35        // If both cursor scanned the complete array quicksort exits
36        if(left >= right)
37            return;
38         
39        // Pivot using median of 3 approach
40        String pivot = getMedian(left, right);
41        int partition = partition(left, right, pivot);
42         
43        // Recursively, calls the quicksort with the different left and right parameters of the sub-array
44        quickSort(0, partition-1);
45        quickSort(partition+1, right);
46    }
47     
48    // This method is used to partition the given array and returns the integer which points to the sorted pivot index
49    private static int partition(int left,int right,String pivot){
50        int leftCursor = left-1;
51        int rightCursor = right;
52        while(leftCursor < rightCursor){
53        while(((Comparable<String>)a[++leftCursor]).compareTo(pivot) < 0);
54        while(rightCursor > 0 && ((Comparable<String>)a[--rightCursor]).compareTo(pivot) > 0);
55            if(leftCursor >= rightCursor){
56                break;
57            }else{
58                swap(leftCursor, rightCursor);
59            }
60        }
61        swap(leftCursor, right);
62        return leftCursor;
63    }
64     
65    public static String getMedian(int left,int right){
66        int center = (left+right)/2;
67         
68        if(((Comparable<String>)a[left]).compareTo(a[center]) > 0)
69            swap(left,center);
70         
71        if(((Comparable<String>)a[left]).compareTo(a[right]) > 0)
72            swap(left, right);
73         
74        if(((Comparable<String>)a[center]).compareTo(a[right]) > 0)
75            swap(center, right);
76         
77        swap(center, right);
78        return a[right];
79    }
80     
81    // This method is used to swap the values between the two given index
82    public static void swap(int left,int right){
83        String temp = a[left];
84        a[left] = a[right];
85        a[right] = temp;
86    }
87     
88    public static void printArray(){
89        for(String i : a){
90            System.out.print(i+" ");
91        }
92    }
93     
94}
95