// ViewController.m
// 基础数据算法
//
// Created by 陈凯 on 15/7/10.
// Copyright (c) 2015年 leTian. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self intadd];
NSLog(@"插入排序:\n");
[self insert];
NSLog(@"冒泡排序:\n");
[self bubbling];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)intadd{
NSInteger a,b,c;
a = 1;
b = a++;
c = ++a;
NSLog(@"自增:\na=%li,\nb=%li,\nc=%li",a,b,c);
}
//插入排序
- (void)insert{
NSMutableArray *arr;
arr = [NSMutableArray arrayWithObjects:@"5",@"6",@"4",@"3",@"2",@"1",@"9",@"8",@"3",@"7", nil];
int j,i,m;
for(j=1;j<10;j++)
{
m = [arr[j]intValue];
for(i=j-1;i>=0;i--)
{
if([arr[i]intValue]<m)
break;
else
arr[i+1] = arr[i];
}
arr[i+1] = [NSString stringWithFormat:@"%i",m] ;
}
for (int i=0; i<10; i++) {
NSLog(@"%@",arr[i]);
}
}
//冒泡排序
- (void)bubbling{
int x[20];
for (int i = 0; i<20; i++) {
x[i] = arc4random()%20 + 1;//获取随机数 [1,21);
}
for (int i = 0; i<20; i++) {
for (int j = 0; j<20-1-i; j++) {
if (x[j] > x[j+1]) {
int num = x[j];
x[j] = x[j+1];
x[j+1] = num;
}
}
}
for (int i = 0; i<20; i++) {
NSLog(@"%i",x[i]);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// 基础数据算法
//
// Created by 陈凯 on 15/7/10.
// Copyright (c) 2015年 leTian. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self intadd];
NSLog(@"插入排序:\n");
[self insert];
NSLog(@"冒泡排序:\n");
[self bubbling];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)intadd{
NSInteger a,b,c;
a = 1;
b = a++;
c = ++a;
NSLog(@"自增:\na=%li,\nb=%li,\nc=%li",a,b,c);
}
//插入排序
- (void)insert{
NSMutableArray *arr;
arr = [NSMutableArray arrayWithObjects:@"5",@"6",@"4",@"3",@"2",@"1",@"9",@"8",@"3",@"7", nil];
int j,i,m;
for(j=1;j<10;j++)
{
m = [arr[j]intValue];
for(i=j-1;i>=0;i--)
{
if([arr[i]intValue]<m)
break;
else
arr[i+1] = arr[i];
}
arr[i+1] = [NSString stringWithFormat:@"%i",m] ;
}
for (int i=0; i<10; i++) {
NSLog(@"%@",arr[i]);
}
}
//冒泡排序
- (void)bubbling{
int x[20];
for (int i = 0; i<20; i++) {
x[i] = arc4random()%20 + 1;//获取随机数 [1,21);
}
for (int i = 0; i<20; i++) {
for (int j = 0; j<20-1-i; j++) {
if (x[j] > x[j+1]) {
int num = x[j];
x[j] = x[j+1];
x[j+1] = num;
}
}
}
for (int i = 0; i<20; i++) {
NSLog(@"%i",x[i]);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end