qs

mail@pastecode.io avatar
unknown
java
2 years ago
1.5 kB
2
Indexable
Never
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
class GFG {

    static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    static int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = (low - 1);
        for (int j = low; j <= high - 1; j++) {

            if (arr[j] < pivot) {

                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, high);
        return (i + 1);
    }


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

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

            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
        }
    }

    static void printArray(int[] arr, int size) {
        for (int i = 0; i < size; i++)
            System.out.print(arr[i] + " ");

        System.out.println();
    }


    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("In0101.txt");
        PrintWriter zapis = new PrintWriter("Out0101.txt");
        Scanner scanner = new Scanner(new File("In0101.txt"));
        int [] tablica= new int [5];
        int i = 0;
        while(scanner.hasNextInt())
        {
            tablica[i++] = scanner.nextInt();
        }
        quickSort(tablica, 0, i - 1);
        zapis.println(Arrays.toString(tablica));
        zapis.close();



    }
}