diff --git a/lesson3Collections.java b/lesson3Collections.java new file mode 100644 index 0000000..6bea959 --- /dev/null +++ b/lesson3Collections.java @@ -0,0 +1,11 @@ +package ru.geekbrains; + +import java.util.Arrays; + +public class lesson3Collections { + public static void main(String[] args) { + int[] array = new int[] {350, 2, 45, 45, 6, 90, 5, 10, 1, 11}; + sortCollections.quickSorting(array, 0, array.length - 1); + System.out.println(Arrays.toString(array)); + } +} diff --git a/sortCollections.java b/sortCollections.java new file mode 100644 index 0000000..eab05ed --- /dev/null +++ b/sortCollections.java @@ -0,0 +1,43 @@ +package ru.geekbrains; + +public class sortCollections { + public static void quickSorting(int[] arr, int from, int to) { + if (from < to) { + int divideIndex = partition(arr, from, to); + quickSorting(arr, from, divideIndex - 1); + quickSorting(arr, divideIndex, to); + } + } + + public static int partition(int[] arr, int from, int to) { // от 0 до 9 + int rightIndex = to; + int leftIndex = from; + + int pivot = arr[from]; + while (leftIndex <= rightIndex) { + + while (arr[leftIndex] < pivot) { + leftIndex ++; + } + + while (arr[rightIndex] > pivot) { + rightIndex --; + } + + if (leftIndex <= rightIndex) { + swap(arr, rightIndex, leftIndex); + leftIndex ++; + rightIndex --; + } + } + return leftIndex; + +} + + + public static void swap(int[] array, int index1, int index2) { + int tmp = array[index1]; + array[index1] = array[index2]; + array[index2] = tmp; + } +} \ No newline at end of file