bubble sort


See On Github

Data

Source Code

//
//  BubbleSort.h
//  Algorithms
//
//  Created by intoxicated on 1/4/14.
//  Copyright (c) 2014 intoxicated. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BubbleSort : NSObject

+ (void)sort:(NSMutableArray *)array;

@end
//
//  BubbleSort.m
//  Algorithms
//
//  Created by intoxicated on 1/4/14.
//  Copyright (c) 2014 intoxicated. All rights reserved.
//
//  Bubblesort

#import "BubbleSort.h"

@interface BubbleSort()

+ (void)swap:(NSMutableArray *)array firstIndex:(NSInteger)fi secondIndex:(NSInteger)si;

@end

@implementation BubbleSort

+ (void)swap:(NSMutableArray *)array firstIndex:(NSInteger)fi secondIndex:(NSInteger)si
{
    NSNumber * temp = [array objectAtIndex:fi];

    [array replaceObjectAtIndex:fi withObject:[array objectAtIndex:si]];
    [array replaceObjectAtIndex:si withObject:temp];
}

+ (void)sort:(NSMutableArray *)array
{
    NSInteger count = array.count;

    do
    {
        for(int i = 1; i < count ; ++i)
        {
            if([[array objectAtIndex:i-1] intValue] > [[array objectAtIndex:i] intValue])
                [self swap:array firstIndex:i secondIndex:i-1];
        }
        count = count - 1;
    }while(count > 0);
}

@end
//
//  BubbleSort_test
//
//  Created by intoxicated on 1/4/14.
//  Copyright (c) 2014 intoxicated. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BubbleSort.h"

#define integer(x) [NSNumber numberWithInt:x]

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSMutableArray * array = [[NSMutableArray alloc]
                                  initWithArray:@[integer(5), integer(9), integer(2), integer(7),
                                                  integer(1), integer(8)]];
        NSLog(@"Before sorted: %@", array);
        
        [BubbleSort sort:array];
        
        NSLog(@"After sorted: %@", array);
    }
    return 0;
}