Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lesson3Collections.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
43 changes: 43 additions & 0 deletions sortCollections.java
Original file line number Diff line number Diff line change
@@ -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;
}
}