Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Removing Duplicates--Insertion Sort

Thread view: 
Max Santelle - 12 Feb 2005 01:21 GMT
This may be pretty elementary to some of you, but I've tried to do this
and can only get the remove to happen for one pair of duplicates.  Can
someone help me with what i'm doing wrong?

public void insertionSort()
     {
     int in, out;
     int j=0;
     long key =-1;

     for(out=1; out<nElems; out++)     // out is dividing line
        {
        long temp = a[out];
                  // remove marked item
        in = out;                      // start shifts at out
        if(a[in-1]==temp)
              temp=key;
        while(in>0 && a[in-1] >= temp) // until one is smaller,
           {
           a[in] = a[in-1];            // shift item to right
           --in;                       // go left one position
           }
        a[in] = temp;                  // insert marked item
         }  // end for
          delete(key);
  }  // end insertionSort()

     public boolean delete(long value)
     {
     int j;
     for(j=0; j<nElems; j++)        // look for it
        if( value == a[j] )
           break;
     if(j==nElems)                  // can't find it
        return false;
     else                           // found it
        {
        for(int k=j; k<nElems; k++) // move higher ones down
           a[k] = a[k+1];
        nElems--;                   // decrement size
        return true;
        }
     }  // end delete()

Thanks in advance...
Heiner K?cker - 12 Feb 2005 12:40 GMT
Max Santelle
> This may be pretty elementary to some of you, but I've tried to do this
> and can only get the remove to happen for one pair of duplicates.  Can
> someone help me with what i'm doing wrong?

Is TreeSet not useful for you?

Simple use Long objects as keys instead long values.

Heiner Kuecker
Internet: http://www.heinerkuecker.de  http://www.heiner-kuecker.de
JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
Domain Specific Languages http://www.heinerkuecker.de/DomainParser.html
Max Santelle - 13 Feb 2005 05:52 GMT
I'm just learning how to program using java, so I am not up to Trees or
TreeSets yet.
But thanks for replying!
PerfectDayToChaseTornados - 13 Feb 2005 20:04 GMT
| I'm just learning how to program using java, so I am not up to Trees or
| TreeSets yet.
| But thanks for replying!

Heiner is right, a Set will not allow duplicates. It should simplify your
problem :-)
Signature

-P
"Programs that are hard to read are hard to modify.
 Programs that have duplicated logic are hard to modify.
 Programs with complex conditional logic are hard to modify"

( Kent Beck)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.