Following is Objective-C method implementation I did for one of the most simplest sorting algorithms, **Bubble Sort** to sort an array of integers.

**Note:-** I have defined it as a static method in the **SimpleAlgorithms** class.

    + (NSArray *) bubbleSort:(NSArray *) arrayToBeSorted {
        NSMutableArray *muArrRaw = [[NSMutableArray alloc] initWithArray:arrayToBeSorted];
        for (int i = 0; i < [muArrRaw count]; i++) {
            for (int j = 0; j < [muArrRaw count] - i; j++) {
                if (j < ([muArrRaw count] - 1) && [[muArrRaw objectAtIndex:j] intValue] > [[muArrRaw objectAtIndex:(j + 1)] intValue]) {
                    int temp = [[muArrRaw objectAtIndex:j] intValue];
                    [muArrRaw replaceObjectAtIndex:j withObject:[muArrRaw objectAtIndex:(j + 1)]];
                    [muArrRaw replaceObjectAtIndex:(j + 1) withObject:[NSNumber numberWithInt:temp]];
                }
            }
        }
        
        return [muArrRaw mutableCopy];
    }

I call this method as follows:

    NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];
    NSArray *sortedArray = [SimpleAlgorithms bubbleSort:array];

I know this seems to be silly for asking for improvements for something like this. But I like to find out even smallest improvements that we can made to this kind of things also because sometimes I might have done something stupid in the above implementation also. **That's where we can embed power and value to the implementation**.

So please give your suggestions and improvements to this Algorithm implementation.

Thanks in Advanced!