import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(
System.in );
int[] arr = new int[30];
System.out.println("Введите 30 целочисленных элементов массива:");
for (int i = 0; i < 30; i++) {
arr[i] = scanner.nextInt();
}
int k = 3;
quickSelect(arr, 0, arr.length - 1, k);
Arrays.sort(arr, arr.length - k, arr.length);
System.out.println("3 наибольших элемента в массиве: " + Arrays.toString(Arrays.copyOfRange(arr, arr.length - k, arr.length));
}
private static void quickSelect(int[] arr, int low, int high, int k) {
if (low < high) {
int pivotIdx = partition(arr, low, high);
if (pivotIdx == arr.length - k) {
return;
} else if (pivotIdx < arr.length - k) {
quickSelect(arr, pivotIdx + 1, high, k);
} else {
quickSelect(arr, low, pivotIdx - 1, k);
}
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[high];
arr[high] = temp;
return i;
}
}