Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
public class Main
{
	public static void main(String[] args) {
		int[] array = {37, 84, 26, 59, 92, 41, 71};
		quickSort(array, 0, array.length-1);
		
		for (int i = 0; i < array.length; i++) {
		    System.out.println(array[i]);
		}
	}
	
private static void quickSort(int[] array, int lowIndex, int highIndex) {
    if (lowIndex < highIndex) {
        int pivot = array[highIndex];
        int leftPointer = lowIndex;
        int rightPointer = highIndex - 1;
        
        while (leftPointer <= rightPointer) {
            while (leftPointer <= rightPointer && array[leftPointer] < pivot) {
                leftPointer++;
            }
            
            while (leftPointer <= rightPointer && array[rightPointer] > pivot) {
                rightPointer--;
            }
            
            if (leftPointer <= rightPointer) {
                int temp = array[leftPointer];
                array[leftPointer] = array[rightPointer];
                array[rightPointer] = temp;
                
                leftPointer++;
                rightPointer--;
            }
        }
        
        int temp = array[leftPointer];
        array[leftPointer] = array[highIndex];
        array[highIndex] = temp;
        
        quickSort(array, lowIndex, leftPointer - 1);
        quickSort(array, leftPointer + 1, highIndex);
    }
}


}