Quick Sort in Java
Page 1 of 1
Quick Sort in Java
public class QuickSort{
private int[] numbers;
private int number;
public void sort(int[] values){
if(values==null||values.length==0)
return;
this.numbers=values;
number=values.length;
quicksort(0,number-1);
}
private void quicksort(int low, int high){
int i=low; int j=high;
int pivot=numbers[low+(low+high)/2];
while(i<=j){
while(numbers[i]<pivot){
i++;
}
while(numbers[j]>pivot)
j--;
if(i<=j){
exchange(i,j);
i++;j--;
}
}
if(low<i)
quicksort(low,j);
if(i<high)
quicksort(i,high);
}
}
private int[] numbers;
private int number;
public void sort(int[] values){
if(values==null||values.length==0)
return;
this.numbers=values;
number=values.length;
quicksort(0,number-1);
}
private void quicksort(int low, int high){
int i=low; int j=high;
int pivot=numbers[low+(low+high)/2];
while(i<=j){
while(numbers[i]<pivot){
i++;
}
while(numbers[j]>pivot)
j--;
if(i<=j){
exchange(i,j);
i++;j--;
}
}
if(low<i)
quicksort(low,j);
if(i<high)
quicksort(i,high);
}
}
skyboard- Posts : 31
Join date : 2011-09-03
Similar topics
» Complexity of Merge Sort, Buble Sort, Quick Sort
» Classic Quick Sort
» big array, can not put memory, how to sort external sort, and then merge
» Java Collection
» What are the differences between C++ and Java?
» Classic Quick Sort
» big array, can not put memory, how to sort external sort, and then merge
» Java Collection
» What are the differences between C++ and Java?
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|