Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fonte;
- public class QuickSort {
- private int array[];
- private int length;
- public void sort(int[] inputArr) {
- if (inputArr == null || inputArr.length == 0) {
- return;
- }
- this.array = inputArr;
- length = inputArr.length;
- quickSort(0, length - 1);
- }
- private void quickSort(int menorindice, int maiorindice) {
- int i = menorindice;
- int j = maiorindice;
- int pivot = array[menorindice+(maiorindice-menorindice)/2];
- while (i <= j) {
- while (array[i] < pivot) {
- i++;
- }
- while (array[j] > pivot) {
- j--;
- }
- if (i <= j) {
- exchangeNumbers(i, j);
- i++;
- j--;
- }
- }
- if (menorindice < j)
- quickSort(menorindice, j);
- if (i < maiorindice)
- quickSort(i, maiorindice);
- }
- private void exchangeNumbers(int i, int j) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- public static void main(String a[]){
- QuickSort sorter = new QuickSort();
- int[] input = {4, 7, 1, 8, 0, 10, 55, 34, 88};
- sorter.sort(input);
- for(int i:input){
- System.out.print(i);
- System.out.print(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement