如何将比较器传递给Java中的自定义快速排序算法

时间:2016-10-29 19:49:39

标签: java quicksort comparator

每当我运行我的测试程序时,它都会运行直到它到达personQueue.sort(),此时它什么都不做。我不确定它是否陷入了无限循环或者是什么。我认为问题来自PersonArrayQueue中的排序方法我相信它只是在第一个while循环中不断运行,并且由于某种原因从未进入第二个循环。任何帮助,将不胜感激。

我从这样的演示类的Comparator方法发送main

personQueue.sort(Person.PersonAgeComparator);

我的Person课程:

import java.util.*;

public class Person implements Comparable<Person> {
   private String firstName;
   private String lastName;
   private int age;

   public Person() {
      firstName = null;
      lastName = null;
      age = 0;
   }

   public Person(String first, String last, int personAge) {
      firstName = first;
      lastName = last;
      age = personAge;
   }

   public void setFirstName(String first) {
      firstName = first;
   }

   public void setLastName(String last) {
      lastName = last;
   }

   public void setAge(int personAge) {
      age = personAge;
   }

   public void setPersonalData(String first, String last, int personAge) {
      firstName = first;
      lastName = last;
      age = personAge;
   }

   public String getName() {
      String name = firstName + " " + lastName;
      return name;
   }

   public String getFirstName() {
      return firstName;
   }

   public String getLastName() {
      return lastName;
   }

   public int getAge() {
      return age;
   }

   @Override
   public String toString() {
      return firstName + " " + lastName + " " + age;
   }

   public void printInfo() {
      System.out.println(firstName + " " + lastName + " " + age);
   }

   public int compareTo(Person comparePerson) {
      int result = lastName.compareTo(comparePerson.lastName);
      return (result != 0 ? result :    firstName.compareTo(comparePerson.firstName));
   }

   public static Comparator<Person> PersonAgeComparator = new Comparator<Person>() {
      @Override
      public int compare(Person firstPerson, Person secondPerson) {  
         if (firstPerson.getAge() < secondPerson.getAge()) {
            return -1;
         } else if (firstPerson.getAge() == secondPerson.getAge()) {
            return 0;
         } else {
            return 1;
         }
      }
   };
}

最后是PersonArrayQueue

import java.util.*;

public final class PersonArrayQueue<T> implements QueueInterface<T> {
   private T[] queue;
   private int front;
   private int end;
   private static final int DEFAULT_CAPACITY = 20;
   private Comparator sortComparator;
   private Comparator nameComparator;   

   public PersonArrayQueue() {
      this(DEFAULT_CAPACITY);
   }

   public PersonArrayQueue(int capacity) {
      @SuppressWarnings("unchecked")
      T[] tempQueue = (T[]) new Object[capacity];
      queue = tempQueue;
      end = 0;
      front = 0;
   }

   public void enqueue(T anEntry) {
      queue[end] = anEntry;
      end++;
   }

   public T dequeue() {
      if (isEmpty()) {
         System.out.println("Queue is empty.");
         end = 0;
         front = 0;
         return null;
      } else {
         T data = queue[front];
         queue[front] = null;
         front++;
         return data;
      }
   }

   public T getFront() {
      if (isEmpty()) {
         System.out.println("Queue is empty.");
         return null;
      } else {
         T data = queue[front];
         return data;
      }
   }

   public boolean isEmpty() {
      return front == end;
   }

   public void clear() {
      while (!isEmpty()) {
         this.dequeue();
      }
   }

   public int length() {
      return end - front;
   }

   public void display() {
      for (int index = front; index < this.length(); index++) {
         System.out.println(queue[index] + " ");
      }
   }   

   public void sort(Comparator<? super T> theComparator) {  
      sortComparator = theComparator;

      if (queue == null || this.length() == 0) {
         return;
      }

      quickSort(sortComparator, front, end - 1);
   }

   public void quickSort(Comparator aComparator, int lowerIndex, int higherIndex) {
      int lower = lowerIndex;
      int upper = higherIndex;
      T pivot = queue[lowerIndex + (higherIndex - lowerIndex) / 2];

      while (lower <= upper) {
         while (aComparator.compare(queue[lower], pivot) > 0) {
            lower++;
         }

         while (aComparator.compare(queue[upper], pivot) < 0) {
            upper--;
         }

         if (aComparator.compare(queue[lower], queue[upper]) >= 0) {
            exchangePositions(lower, upper);
            lower++;
            upper--;
         }
      }

      if (lower < lowerIndex) {
         quickSort(aComparator, lower, higherIndex);
      }

      if (upper > lowerIndex) {
         quickSort(aComparator, lowerIndex, upper);
      }
   }

   public void exchangePositions(int lower, int upper) {
      T temp = queue[lower];
      queue[lower] = queue[upper];
      queue[upper] = temp;
   }
}

0 个答案:

没有答案