Счетчик для алгоритма быстрой сортировки - PullRequest
0 голосов
/ 20 января 2019

Мой профессор попросил нас выбрать лучший алгоритм сортировки, мы выбрали Quicksort.Перед нами стояла задача создать программу, которая сортирует массив целых чисел с использованием выбранного алгоритма, но только с использованием 1 метода, и создать счетчик, который подсчитывает, сколько раз строка кода выполняется в программе для определения самой быстрой и лучшей памяти.мудрый алгоритм.

Я знаю, что Quicksort отлично подходит для больших наборов данных, что дает причину, по которой мой счетчик счетчиков высок, но я просто хотел проверить, правильны ли строки, в которые я положил счетчик.Есть предложения?

public class JavaProgram {

    @FunctionalInterface
    public interface Func {
        void call(int[] arr, int i, int j);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int counter=0; counter++;
        System.out.println("Enter size of array: "); counter++;
        int x = sc.nextInt(); counter++;
        System.out.println("Enter numbers in array: "); counter++;
        int[] numbers = new int[x]; counter++;
        for(int i=0;i<numbers.length;i++)
        {
            numbers[i] = sc.nextInt(); counter++;
        }

        Func swap = (int[] arr, int i, int j) -> {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }; counter++;

        Stack<Integer> stack = new Stack<>(); counter++;
        stack.push(0); counter++;
        stack.push(numbers.length); counter++;

        while (!stack.isEmpty()) { counter++;
            int end = stack.pop(); counter++;
            int start = stack.pop(); counter++;
            if (end - start < 2) { counter++;
                continue;
            } counter++;

            // partitioning part
            int position = start + ((end - start) / 2); counter++;
            int low = start; counter++;
            int high = end - 2; counter++;
            int piv = numbers[position]; counter++;
            swap.call(numbers, position, end - 1); counter++;
            while (low < high) { counter++;
                if (numbers[low] < piv) { counter++;
                    low++; counter++;
                } else if (numbers[high] >= piv) { counter++;
                    high--; counter++;
                } else { counter++;
                    swap.call(numbers, low, high); counter++;
                }
            }
            position = high; counter++;
            if (numbers[high] < piv) { counter++;
                position++; counter++;
            }
            swap.call(numbers, end - 1, position); counter++;


            stack.push(position + 1); counter++;
            stack.push(end); counter++;
            stack.push(start); counter++;
            stack.push(position); counter++;
        }

        System.out.println("Sorted array: " + Arrays.toString(numbers)); counter++;
        System.out.println("Counter: "+counter);
    }
}  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...