1、插入法 c写法:
void insertSort(int a[ ], int n)
{
int i,j,p;
for (i = 1; i<n; i++)
{
t=a[ i ];
for( j = i-1; j >= 0; j--)
{
if( t < a[ j ] )
{
a[ i ] = a[ j ];
a[ j ] = t;
}//if
}//for2
}//for1
//打印需要遍历
printf("element of array is:");
for( int t = 0; t<n; t++)
{
printf("%d," a[t]);
}
}
2、选择法OC写法:
- (void)swapWithData:(NSMutableArray *)aData index1:(NSInteger)index1 index2:(NSInteger)index2
{
NSNumber *tmp = [aData objectAtIndex:index1];
[aData replaceObjectAtIndex:index1 withObject:[aData objectAtIndex:index2]];
[aData replaceObjectAtIndex:index2 withObject:tmp];
}
- (void)sortWithArray:(NSArray *)aData
{
NSMutableArray *data =[[[NSMutable Array] alloc] initWithArray:aData];
for (int i = 0; i<[data count] -1; i++)
{
int m = i;
if([data objectAtIndex:j] < [data objectAtIndex:m])
m = j;
if (m != i)
[self swapWithData:data index1:m index2:i];
}
//打印是无需使用for循环
NSLog(@"选择排序后结果为%@", [data description];
[data release];
}
比较我们可以看出,c更简洁,oc更容易看懂; 另外oc可以使用c, c确不能使用oc,oc更灵活;