Boa tarde!! Pessoal, sou nova na área de Java, e preciso fazer uma ordenação utilizando os métodos QUickSort e ShellSort!
Alguém me ajuda?
[color=darkblue] [/color]
QUickSort e ShellSort
3 Respostas
public class QuickSort {
public interface Comparator {
public int compareTo(Object a, Object b);
}
private Object[] data;
private Comparator comparator;
public QuickSort(Object[] data, Comparator comparator) {
this.comparator = comparator;
this.data = data;
}
public void sort() {
quicksort(0, data.length - 1);
}
private void quicksort(int p, int r) {
while (p < r) {
int q = partition(p, r);
if (q == r)
q--;
quicksort(p, q);
p = q + 1; // made it tail recursive to keep stack depth from blowing up
}
}
private int partition(int lo, int hi) {
Object pivot = data[(lo+hi)/2];
while (true) {
while (comparator.compareTo(data[hi], pivot) >= 0 && lo < hi) {
hi--;
}
while (comparator.compareTo(data[lo], pivot) < 0 && lo < hi) {
lo++;
}
if (lo < hi) {
Object T = data[lo];
data[lo] = data[hi];
data[hi] = T;
} else
return hi;
}
}
}
import java.util.Comparator;
/**
* Implementation of the shell sort algorithm
* @author Patrick Meier
*/
public class ShellSort
extends AbstractSortWrapper
{
/**
* Constructor
*/
public ShellSort()
{
super();
}
/**
* Sorts the given object array.
* @param array the array to sort
* @param comparator the comparator to use
* @return the sorted array
*/
public Object[] sort( Object array[], Comparator comparator )
{
return shellSort( array, comparator );
}
/**
* Implements the selection sort to sort an integer-array
* @param array the array to sort
* @param comparator the comparator to use
* @return the sorted array
*/
public Object[] shellSort( Object array[], Comparator comparator )
{
int h = 1;
while( h <= array.length )
{
if( isStopped() )
return array;
h = 3 * h + 1;
}
while( h > 0 )
{
if( isStopped() )
return array;
h = h / 3;
for( int i = (h - 1); i < array.length; i++ )
{
int j = i;
if( isStopped() )
return array;
while( (j > h - 1) &&
(j - h - 1 < array.length) && (j - h - 1 >= 0) &&
comparator.compare( array[j], array[j - h - 1] ) < 0 )
{
doCompare();
doCompare();
if( isStopped() )
return array;
swap( array, j - h - 1, j ); // exchange of the (j-h-1)- and j-value
j = j - h - 1;
}
}
}
return array;
}
}
Pessoal, muito obrigado por tanta agilidade.
Vou realizar a implementação, qualquer dúvido os procuro novamente!
[]´s
Criado 18 de outubro de 2006
Ultima resposta 18 de out. de 2006
Respostas 3
Participantes 3
Alura O que é Python? — um guia completo para iniciar nessa linguagem de programação Acesse agora o guia sobre Python e inicie sua jornada nessa linguagem de programação: o que é e para que serve, sua sintaxe e como iniciar nela!
Casa do Codigo Engenharia de Prompt para Devs: Um guia para aprender a... Por Ricardo Pupo Larguesa — Casa do Codigo