Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:53:12+00:00 2026-05-29T07:53:12+00:00

I’m trying to make bubble sort using Cocoa classes, but I’ve found some problems.

  • 0

I’m trying to make bubble sort using Cocoa classes, but I’ve found some problems. here is the code:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *array=[[NSMutableArray alloc] init];

int a,b;
for (int i=0;i<10;i++) {
    int newElement=rand()%100;
    [array addObject:[NSString stringWithFormat:@"%d",newElement]]; 
    NSLog(@"%i %@",i,[array objectAtIndex:i]);
}
[array addObject:[NSNull null]];

/*for (int a=1;a<10;a++){ */
//===Problem exact in this place
    for (int i=9;i>=1;i--) {
        NSNumber *tmp1=[array objectAtIndex:i]; //9
        NSNumber *tmp2=[array objectAtIndex:i-1]; //8
        if ([[array objectAtIndex:i] compare:[array objectAtIndex:i-1]]==NSOrderedAscending) { //if ([obj1 integerValue] > [obj2 integerValue]) this is -NSOrderedDescending;   
            [tmp1 retain];
            [array replaceObjectAtIndex:i-1 withObject:tmp1];
            [array replaceObjectAtIndex:i withObject:tmp2];
            [tmp1 release];
        }

    }

`NSLog(@"\n==SORTED==\n"); 
for (int i=0;i<10;i++) NSLog(@"%i %@",i,[array objectAtIndex:i]);`

So in output in terminal i see this:

==UNSORTED==


7
49
73
58
30
72
44
78
23
9

==SORTED==

23
7
49
73
58
30
72
44
78
9

I cant’ understand why does it not start from index 9? As I can see it start sorting from index 8 (taking integer 23) and goes to index 1. But when he meets integer 7 at index 1 why it replaces by integer 23, it’s not corresponds to the condition of a circle for()???.. Also exact the same algorithm on C++ without classes and OOP stuff works great, so I guess an algorithm is correct there. Please guys help understand this, I spent a half of a day on it :)) Thanks a lot in advance for the answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:53:13+00:00Added an answer on May 29, 2026 at 7:53 am
    1. You’re only doing a single pass of bubble-sort, which is insufficient to fully sort the array.
    2. Your array is full of strings, not numbers, so “9” comes after “78” just like “z” comes after “xy”.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.