Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
3
Indexable
Never
import java.util.Arrays;

class QuickSort {

    static int partition(String[] array, int low, int high) {
        String pivot = array[high];

        int i = (low - 1);

        for (int j = low; j < high; j++) {
            if (array[j].compareTo(pivot) < 0) {
                i++;
                String temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        String temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;

        return (i + 1);
    }

    static void quickSort(String[] array, int low, int high) {
        if (low < high) {

            int pi = partition(array, low, high);

            quickSort(array, low, pi - 1);

            quickSort(array, pi + 1, high);
        }
    }
}
class QuickSortString {
    public static void main(String[] args) {
        String[] data = {"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace"};
        int size = data.length;

        QuickSort.quickSort(data, 0, size - 1);

        System.out.println("Sorted Array in Ascending Order: ");
        System.out.println(Arrays.toString(data));
    }
}
Leave a Comment