QuickSort using Linked List

耗尽温柔 提交于 2019-12-13 07:26:13

问题


I need help with this code. I need to call the quicksort method without any parameters in the main method. But this program has a parameter. How can I make it work and not have any parameter when calling it in the main method?

Please help.

Employee Class

public class Employee
 {
     private String firstname;
     private int idNumber;
     private String lastname;


  Employee(String lname,String fname, int id)
  {  
      lastname = lname;
      firstname = fname;
      idNumber = id;
      }

public void setLastName(String lname)
  {lastname = lname;}
public String getLastName() 
  {return lastname;}

public void setFirstName(String fname)
    {firstname = fname;}
public String getFirstName()
    {return firstname;}

public void setidNumber(int id)
    {idNumber = id;}
public int getidNumber()
    {return idNumber;}



public String toString()
{
    String str =  "\nName: " + lastname + " " + firstname
                + "\nID: "  + idNumber;

    return str;
}
 public int compareTo(Employee Employee2)
 {
  int diff = lastname.compareToIgnoreCase(Employee2.getLastName());
  if(diff != 0)
        return diff;
    else
             return -1;

        }
  }

Linked List Class

public class DoublyLinkedList
{
public class DoublyLinkedListLink
{
      public Employee info;
          public DoublyLinkedListLink next;
       public DoublyLinkedListLink back;
//Default Constructor
//Postcondition: info = 0;
// next = null; back = null;
public DoublyLinkedListLink()
{
     info = null;
 next = null;
 back = null;
 }

public DoublyLinkedListLink(Employee item)
{
 info = item;
 next = null;
 back = null;
 }

 public void displayInfo()
{
 System.out.print(info + " ");
 }

 }


protected int count;
protected DoublyLinkedListLink first;
protected DoublyLinkedListLink last;

 public DoublyLinkedList()
{
        first = null;
    last = null;
    count = 0;
     }

 public void initializeList()
{
         first = null;
     last = null;
     count = 0;
     }

 public boolean isEmpty()
{
 return (first == null);
         }

 public int length()
{
    return count;
     }

 public void print()
{
 DoublyLinkedListLink current = first;
 while (current != null)
 {
     current.displayInfo();
     current = current.next;
     }//end while
 }//end print

 public void insert(Employee insertItem)
{
     DoublyLinkedListLink newNode = new DoublyLinkedListLink(insertItem);
        if (isEmpty())
     {
              first = newNode;
          last = newNode;
          count++;
              }
       else
      {
              last.next = newNode;
              newNode.back = last;
         }
          last = newNode;
     }

 public DoublyLinkedListLink partition(DoublyLinkedList list, DoublyLinkedListLink first, DoublyLinkedListLink last)
{
     DoublyLinkedListLink smallIndex = first;
     DoublyLinkedListLink index = smallIndex.next;
     DoublyLinkedListLink temp = new DoublyLinkedListLink();
         Employee pivot = first.info;

         while (index != last.next)
        {
         if((index.info).compareTo(pivot) <= 0)
         {
             smallIndex = smallIndex.next;
             temp.info = index.info;
             index.info = smallIndex.info;
             smallIndex.info = temp.info;
                }

         index = index.next;
        }

         temp.info = first.info;
         first.info = smallIndex.info;
         smallIndex.info = temp.info;
         System.out.print("The list in partition is: "); list.print();
         System.out.print("\n");
         return smallIndex;
      }


 private void recQuickSort(DoublyLinkedList list, DoublyLinkedListLink first, DoublyLinkedListLink last)
{
        while(first != last)
     {
         DoublyLinkedListLink pivotLocation = partition(list, first, last);
         recQuickSort(list, first, pivotLocation.back);
         recQuickSort(list, pivotLocation.next, last);
            }
 }

 public void quickSort(DoublyLinkedList list)
{
     recQuickSort(list, list.first, list.last);
    }

}

Main Method

    class MergeSortDriver
    {
    public static void main (String [] args)
    {
    Employee e1 = new Employee("Grey","Bob",5239);      
    Employee e2 = new Employee("Smith","Maggie", 9845);
    Employee e3 = new Employee("Ocasio","John", 8502);
    Employee e4 = new Employee("Yang", "Christina", 4656);
    Employee e5 = new Employee("Carpenter","Kimberely", 6798);
    Employee e6 = new Employee("Aguilar","Charlie", 5986);

    DoublyLinkedList a = new DoublyLinkedList();

    Employee A[] = {e1,e2,e3,e4,e5,e6};

    a.insert(e1);
    a.insert(e2);
    a.insert(e3);
    a.insert(e4);
    a.insert(e5);
    a.insert(e6);

    a.print();

    a.quickSort();

    a.print();   
      }
     }   

回答1:


Use this instead of the parameter. Instead of:

public void quickSort(DoublyLinkedList list)
{
    recQuickSort(list, list.first, list.last);
}

You can do

public void quickSort()
{
    recQuickSort(this, this.first, this.last);
}

This way you are using the instance you are invoking quickSort on, instead of one passed into the quickSort method.


EDIT - Regarding the error mentioned in comments:

The NullPointerException does not have anything to do with whether you use this instead of a parameter. Instead I suspect it has something to do with your while loop around the recursive calls.

while(first != last)
{
     DoublyLinkedListLink pivotLocation = partition(list, first, last);
     recQuickSort(list, first, pivotLocation.back);
     recQuickSort(list, pivotLocation.next, last);
}

Such a while-loop is usually not needed in a recursive solution. You can (kinda) think of your recursion as your loop. So instead of a loop, you should restrict the calls with ifs. It could look something like:

DoublyLinkedListLink pivot = partition(list, first, last);
if(first != pivot && first != pivot.back) {
    recQuickSort(list, first, pivot.back);
}
if(last != pivot && last != pivot.next) {
    recQuickSort(list, pivot.next, last);
}

Furthermore, you should handle the case when the list is empty. In this case partition will throw a NullPointerException because both first and last would be null.

I would think that fixing these two things would make it work. However, I have not tested it.


Also, try to keep code nicely formatted. Code without consistent formatting (such as indentation) is a pain to work with and look at.



来源:https://stackoverflow.com/questions/23117581/quicksort-using-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!