开发者

How do I sort an array of ints in cocoa?

I'm brand new to programming on the Mac (i.e. xcode and cocoa) and I'm trying to simply perform a bubble sort and am having a lot of difficulty with this.

The goal of this is to filter an image using a median filter by using a 9 pixel kernel. I take in the grey scale values of all nine pixels and then I'm trying to put them in a nine point array and sort the array to extract the median value of the nine (so it doesn't matter if I use ascending or descending).

I've been trying to store the pixel values (which are ints) into a NSMutableArray but I really have no idea how to go about doing this or how to then sort them when the array is populated.

    // Perform median filter on all images in the stack 
    for (x = 0; x < [curPix pwidth]; x++){
        for (y = 0; y < [curPix pheight]; y++){

            float value;
            int tLeft, tMid, tRight, cLeft, index, cRight, bLeft, bMid, bRight; // takes in pixel placement
            value = tLeft = tMid = tRight =开发者_开发问答 cLeft = index = cRight = bLeft = bMid = bRight = 0;
            curPos = y * [curPix pwidth] + x;

            if (x != 0 && y != 0 && x != ([curPix pwidth]-1) && y != ([curPix pheight]-1)){

                //Make kernel for median filter
                index   = fImage[curPos];                       // index pixel
                tLeft   = fImage[index - [curPix pwidth] - 1];  // top left
                tMid    = fImage[index - [curPix pwidth]];      // top middle
                tRight  = fImage[index - [curPix pwidth] + 1];  // top right
                cLeft   = fImage[index - 1];                    // center left
                cRight  = fImage[index + 1];                    // center right
                bLeft   = fImage[index + [curPix pwidth] - 1];  // bottom left
                bMid    = fImage[index + [curPix pwidth]];      // bottom middle
                bRight  = fImage[index + [curPix pwidth] + 1];  // bottom right

                // Need to make array, populate with pixels (above), and sort.
                // Once sorted, take median value, save it as 'value', and store it as new pixel value

                fImage[curPos] = (int) value;   // return value to index
            }
            else {
                fImage[curPos] = fImage[curPos];                
            }
        }
    }


How do I sort an array of ints in cocoa?

int is a C type, so, the same way as in C.

Mac OS X comes with a number of sort functions in the standard library. qsort, which is Quicksort, is defined by C; the others come, I think, from BSD. They're all under qsort's manpage.

Each of the functions takes an array of pointer-sized elements, so you'll want to use long (or, for more portability, intptr_t), not int, for the elements.

Make a C array of such elements, fill it out manually, then sort with one of these functions and find the median.


To fill them into an NS(Mutable)Array you need to wrap your ints in instances of NSNumber, e.g.:

NSMutableArray *myArray = [NSMutableArray array];
[myArray addObject:[NSNumber numberWithInt:42]];

There are various methods to sort the resulting array, to get you started:

[myArray sortUsingSelector:@selector(compare:)];

Fortunately, this doesn't use BubbleSort.


To begin with I would steer away from bubble sorts unless you are sorting a small sample. Bubble sort is very slow. Not being a cocoa expert I can't really help you very much other than suggesting you look into the built in array sorting routines.

This is for the iphone but might help: http://www.iphonedevsdk.com/forum/iphone-sdk-development/61615-how-do-i-sort-30-000-objects.html

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜