DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2006

    Reference-Based Selection Sort

    I'm having problems doing a selection sort with a linked list (not a doubly linked list). I have a class called OrderedList that extends a class called List. In my sort method, I try to make a new OrderedList to put the smallest nodes, one by one, into it. I try to remove the smallest node from the old list so that it can find the next smallest with the next pass. Anyways, I keep getting null errors and such...here is my sort method of my OrderedList class.

    public Node sort(Node someHead){

    Node n1 = someHead;
    Node current = null;
    Node smallest = someHead;
    Node nodeA;
    Node beforeSmallest=null;
    Node current2;
    OrderedList collection2 = new OrderedList();

    for(current2=head; current2.getNext()!=null; current2=current2.getNext()){

    // Find the node with the smallest element
    for (current = n1.getNext(); current.getNext()!=null; current= current.getNext()){

    // If "smallest" has a larger element than "current,"
    // "smallest" is not actually the smallest element.
    // This assignment fixes that.
    if (smallest.getElement().compareTo(current.getElement())>0)
    smallest = current;

    // Add that "smallest" element to a new list

    // Find the node just before the smallest node

    // As long as "smallest" isn't at the head, find the node located
    // before it.
    if (smallest!=head){
    for (nodeA=head; nodeA.getNext()==smallest; nodeA=nodeA.getNext())

    // Overwrite the smallest node by setting beforeSmallest's next
    // node to be the node after smallest
    // if the smallest node is at the head of the list, then get rid of it
    // by setting the next node to be the head.

    // return the sorted list
    return collection2.head;

    Can anyone give me some help with this? If you need more information about my implementation or my classes or methods let me know. Thanks for any advice.

  2. #2
    Join Date
    Dec 2004
    San Bernardino County, California
    Could you be more specific about the "null errors and such" that you are receiving, and let us know whether these are runtime errors (do your file(s) compile)?

Similar Threads

  1. library linking problem
    By sankar_de in forum C++
    Replies: 5
    Last Post: 08-10-2005, 07:35 AM
  2. Replies: 2
    Last Post: 10-30-2002, 08:28 PM
  3. Replies: 0
    Last Post: 06-12-2002, 10:59 AM
  4. Replies: 2
    Last Post: 11-22-2000, 12:50 AM
  5. Re: Sort problem in MS SQL Server 7.0
    By D. Patrick Hoerter in forum Database
    Replies: 1
    Last Post: 06-26-2000, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.