第一版冒泡: main.m #import <stdio.h> int main( int argc, const char *argv[] ) { int a[4] = {11,26,3,55}; int i,j,temp; for(i = 0;i<3;i++) for(j =i+1;j<4;j++) if(a[i]>a[j]){ temp = a[i]; a[i] = a[j]; a[j] = temp; } for(i=0;i<4;i++) printf("%d ",a[i] ); return 0; } 第二版冒泡(加上了實例方法和頭文件,以及make文件): main.m #import "bubble.h" #import <stdio.h> int main( int argc, const char *argv[] ) { Bubble *bb = [[Bubble alloc] init]; // print bb[] printf( "Bubble: " ); [bb initial]; [bb sort]; printf( "/n" ); //free memory [bb release]; return 0; } bubble.h #import <Foundation/NSObject.h> @interface Bubble: NSObject { int a[4]; } -(void) initial; -(void) sort; @end bubble.m #import "bubble.h" #import <stdio.h> @implementation Bubble -(void) initial { a[0] = 11; a[1] = 26; a[2] = 3; a[3] = 55; } -(void) sort { int i,j,temp; for(i = 0;i<3;i++) for(j =i+1;j<4;j++) if(a[i]>a[j]){ temp = a[i]; a[i] = a[j]; a[j] = temp; } for(i=0;i<4;i++) printf("%d ",a[i] ); } @end GNUmakefile: include $(GNUSTEP_MAKEFILES)/common.make TOOL_NAME=Bubble Bubble_HEADERS = bubble.h Bubble_OBJC_FILES=main.m bubble.m include $(GNUSTEP_MAKEFILES)/tool.make