The Answer to Life, the Universe, and Everything

Tuesday, September 26, 2006

Quick Sort in Java

I developed small application and used sort algorythm. Here is the good performance quick sort Java sample.


public static DTO[] sort(DTO _dto[],int left,int right){
//Quick Sort
int key;
if(left<right){
key = _dto[(left+right)/2].getDateValue();
int i=left -1;
int j=right +1;
while(true){
while(_dto[++i].getDateValue() while(_dto[--j].getDateValue()>key);
if(i>=j)break;
DTO t = _dto[i];
_dto[i]=_dto[j];
_dto[j] =t;

}
sort(_dto, left, i-1);
sort(_dto, j+1, right);
}


/*Bubble Sort
for(int i=0;i<_dto.length-1;i++){
for(int j=_dto.length-1;j>i;j--){
if(_dto[j].getDateValue()>_dto[j-1].getDateValue()){
DietDTO t=_dto[j];
_dto[j]=_dto[j-1];
_dto[j-1]=t;
}

}

}
*/

return _dto;
}

No comments: