Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Programm {
- static int[] numbers = new int[7];
- public static void main(String[] args) {
- Scanner keyScanner = new Scanner(System.in);
- for (int i = 0; i < 7; i++) {
- numbers[i] = keyScanner.nextInt();
- }
- for (int i = 0; i < 7; i++) {
- System.out.print(numbers[i] + "; ");
- }
- QuickSort(0, 6);
- for (int i = 0; i < 7; i++) {
- System.out.print(numbers[i] + "; ");
- }
- // numbers = [1, 7, 3, 9, 2, -4, 0];
- }
- static void QuickSort(int left, int right) {
- System.out.println(left);
- System.out.println(right);
- if (left < right) {
- // int key = (left + right) / 2; #missing numbers before key's index
- int key = numbers[(left + right) / 2];
- int l = left;
- int r = right;
- while (l <= r) {
- while (numbers[l] < key) {
- l++;
- }
- // while (numbers[r] >= key) { #just ">" is enough, "=" causes infinity loop
- while (numbers[r] > key) {
- r--;
- }
- if (l <= r) {
- int t = numbers[r];
- numbers[r] = numbers[l];
- numbers[l] = t;
- l++;
- r--;
- }
- }
- if (r > left)
- QuickSort(left, r);
- if (right > l)
- QuickSort(l, right);
- }
- }
- }
Add Comment
Please, Sign In to add comment