• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sorting Array Problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting Array Problems


  • Subject: Re: Sorting Array Problems
  • From: Fritz Anderson <email@hidden>
  • Date: Thu, 21 Jul 2005 14:59:44 -0500

The first thing that comes to me is that the lines

[studentNameA insertObject:[studentNameA objectAtIndex: x] atIndex: y];
[studentNameA removeObjectAtIndex: x+1];

assume y < x. This is not always true. Revise your swap so it's sensitive to whether the x object comes first.


Second, for cases where y < x, the swap will pull correctly-ordered items from the beginning of the list out of order, to the x index. You probably intend your inner loop to start y at x+1.

The overriding thing is that there is no reason for anybody, ever, to write his own sorting routine. Even bad sorts (and this one is bad, an O(N^2) sort) are easy to misimplement. The equivalent one-liner
[studentNameA sortUsingSelector: @selector(compare:)];
will be ten times as fast on surprisingly small lists, and is guaranteed to be correct.


    -- F

On 21 Jul 2005, at 2:16 PM, Kodex wrote:

Ok so I am sorting an array based on its characters,
It sorts ok but every now and then it will overwrite
data or miss a sort. On occassion with certain data it
wont sort correctly at all. Here is my code.

- (IBAction)sortAlpha:(id)sender
{
    int x;
    int y;

    for (x = 0; x < [studentNameA count]; x++)
    {
        for (y = 0; y < [studentNameA count]; y++)
        {

            NSString *string1 = [studentNameA objectAtIndex:
x];
            NSString *string2 = [studentNameA objectAtIndex:
y];

            BOOL ascending = [string1 compare:string2] ==
NSOrderedAscending;

            if(ascending)
            {

                    NSLog(@"%@(%i) comes before %@(%i)", string1, x,
string2, y);
                    [studentNameA insertObject:[studentNameA
objectAtIndex: x] atIndex: y];
                    [studentNameA removeObjectAtIndex: x+1];
            }


} }

    [table reloadData];
}


Anyone know what im doing wrong cause i cant seem to find the problem here. Thanks!



____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40manoverboard.org


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Sorting Array Problems (From: Kodex <email@hidden>)

  • Prev by Date: Re: Sorting Array Problems
  • Next by Date: Re: Sorting Array Problems
  • Previous by thread: Re: Sorting Array Problems
  • Next by thread: NSURLConnection dependency on RunLoop (Sanity check)
  • Index(es):
    • Date
    • Thread