用OC实现的冒泡排序、选择排序、矩阵相乘,纯粹是用来练习语法。
冒泡排序,程序如下:
void bubbleSort() { //初始化数组 NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:8]; [array1 addObject:@"5"]; [array1 addObject:@"10"]; [array1 addObject:@"8"]; [array1 addObject:@"12"]; [array1 addObject:@"9"]; [array1 addObject:@"4"]; [array1 addObject:@"20"]; [array1 addObject:@"14"]; NSInteger count = array1.count; for(int i = 0; i < count; ++i){ for(int j = 0; j < count - i - 1; ++j){ NSInteger before = [[array1 objectAtIndex:j] integerValue]; NSInteger after = [[array1 objectAtIndex:j + 1] integerValue]; if(before > after){ NSInteger temp = before; [array1 setObject:[NSString stringWithFormat:@"%ld", after] atIndexedSubscript:j]; [array1 setObject:[NSString stringWithFormat:@"%ld",temp ] atIndexedSubscript:j + 1]; } } } for(int i = 0; i < count; ++i){ NSLog(@"%@",[array1 objectAtIndex:i]); } }
选择排序,程序如下:
void selectSort() { //初始化数组 NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:8]; [array1 addObject:@"5"]; [array1 addObject:@"10"]; [array1 addObject:@"8"]; [array1 addObject:@"12"]; [array1 addObject:@"9"]; [array1 addObject:@"4"]; [array1 addObject:@"20"]; [array1 addObject:@"14"]; NSInteger count = array1.count; for(int i = 0; i < count - 1; ++i){ NSInteger min = i; for(int j = i + 1; j < count; ++j){ NSInteger minNum = [[array1 objectAtIndex:min] integerValue]; NSInteger num = [[array1 objectAtIndex:j] integerValue]; if(minNum > num){ min = j; } } NSInteger changeNum = [[array1 objectAtIndex:min] integerValue]; NSInteger temp = [[array1 objectAtIndex:i] integerValue]; [array1 setObject:[NSString stringWithFormat:@"%ld",changeNum] atIndexedSubscript:i]; [array1 setObject:[NSString stringWithFormat:@"%ld",temp] atIndexedSubscript:min]; } for(int i = 0; i < count; ++i){ NSLog(@"%@",[array1 objectAtIndex:i]); } }
矩阵相乘,程序如下:
int main(int argc, const char * argv[]) { @autoreleasepool { //bubbleSort(); //selectSort(); //矩阵相乘 //初始化矩阵1,矩阵1为: // 1 0 2 // -1 3 1 NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableArray *row1 = [[NSMutableArray alloc] initWithCapacity:3]; NSMutableArray *row2 = [[NSMutableArray alloc] initWithCapacity:3]; [row1 addObject:@"1"]; [row1 addObject:@"0"]; [row1 addObject:@"2"]; [row2 addObject:@"-1"]; [row2 addObject:@"3"]; [row2 addObject:@"1"]; [array1 addObject:row1]; [array1 addObject:row2]; //初始化矩阵2,矩阵2为 // 3 1 // 2 1 // 1 0 NSMutableArray *array2 = [[NSMutableArray alloc] initWithCapacity:3]; NSMutableArray *newrow1 = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableArray *newrow2 = [[NSMutableArray alloc] initWithCapacity:2]; NSMutableArray *newrow3 = [[NSMutableArray alloc] initWithCapacity:2]; [newrow1 addObject:@"3"]; [newrow1 addObject:@"1"]; [newrow2 addObject:@"2"]; [newrow2 addObject:@"1"]; [newrow3 addObject:@"1"]; [newrow3 addObject:@"0"]; [array2 addObject:newrow1]; [array2 addObject:newrow2]; [array2 addObject:newrow3]; //第一个矩阵的行数 NSInteger numRow1 = array1.count; //第一个矩阵的列数 NSArray *Col = [array1 objectAtIndex:0]; NSInteger numCol1 = Col.count; NSLog(@"numRow1 = %ld",numRow1); NSLog(@"numCol1 = %ld",numCol1); //第二个矩阵的列 NSArray *Col2 = [array2 objectAtIndex:0]; NSInteger numCol2 = Col2.count; NSLog(@"numCol2 = %ld",numCol2); NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:numRow1]; //开始矩阵相乘 for(int i = 0; i < numRow1; ++i){ NSMutableArray *temprow = [[NSMutableArray alloc] initWithCapacity:numCol2]; for(int j = 0; j < numCol2; ++j){ NSInteger sum = 0; for(int k = 0; k < numCol1; ++k){ NSInteger num1 = [[[array1 objectAtIndex:i] objectAtIndex:k] intValue]; NSInteger num2 = [[[array2 objectAtIndex:k] objectAtIndex:j] intValue]; sum = sum + (num1 * num2); } NSString *tempString = [NSString stringWithFormat:@"%ld",sum ]; [temprow addObject:tempString]; } [array3 addObject:temprow]; } for(int i = 0; i < numRow1;++i){ for(int j = 0; j < numCol2; ++j){ NSLog(@"%@",[[array3 objectAtIndex:i] objectAtIndex:j]); } } } return 0; }
时间: 2024-12-26 02:55:53