冒泡排序原理iOS与Android演示

冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。所以,如果两个元素相等,不会再无聊地把他们俩交换一下的,所以冒泡排序是一种稳定排序算法。

ios演示

冒泡排序原理iOS与Android演示_第1张图片
ios-sort.gif

android演示

Android-sort.gif

ios代码

#import "ViewController.h"

static int i = 0;
@interface ViewController()

@property(nonatomic,strong)UITextView *text1;
@property(nonatomic,strong)UITextView *text2;
@property(nonatomic,strong)UITextView *text3;
@property(nonatomic,strong)UITextView *text4;
@property(nonatomic,strong)UITextView *text5;
@property(nonatomic,strong)UITextView *text6;
@property(nonatomic,strong)UITextView *text7;
@property(nonatomic,strong)UITextView *text8;
@property(nonatomic,strong)UITextView *text9;
@property(nonatomic,strong)UITextView *text10;
@property(nonatomic,strong)UIButton *button;//按钮
@property(nonatomic,strong)NSMutableArray *array;//要排列的数组

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _array = [NSMutableArray arrayWithObjects:@117,@198,@177,@100,@155,@127,@188,@137,@168,@144, nil];
    
    _text1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 50, 32, [_array[0] floatValue])];
    _text1.backgroundColor = [UIColor blackColor];
    _text1.layer.borderColor = UIColor.grayColor.CGColor;
    _text1.layer.borderWidth = 1;
    _text1.text = [NSString stringWithFormat:@"%d",[_array[0] intValue]];
    _text1.textColor = [UIColor whiteColor];
    [self.view layoutIfNeeded];
    [self.view addSubview:_text1];
    
    _text2 = [[UITextView alloc] initWithFrame:CGRectMake(32, 50, 32, [_array[1] floatValue])];
    _text2.backgroundColor = [UIColor blackColor];
    _text2.layer.borderColor = UIColor.grayColor.CGColor;
    _text2.layer.borderWidth = 1;
    _text2.text = [NSString stringWithFormat:@"%d",[_array[1] intValue]];
    _text2.textColor = [UIColor whiteColor];
    [self.view addSubview:_text2];
    
    _text3 = [[UITextView alloc] initWithFrame:CGRectMake(64, 50, 32, [_array[2] floatValue])];
    _text3.backgroundColor = [UIColor blackColor];
    _text3.layer.borderColor = UIColor.grayColor.CGColor;
    _text3.layer.borderWidth = 1;
    _text3.text = [NSString stringWithFormat:@"%d",[_array[2] intValue]];
    _text3.textColor = [UIColor whiteColor];
    [self.view addSubview:_text3];
    
    _text4 = [[UITextView alloc] initWithFrame:CGRectMake(96, 50, 32, [_array[3] floatValue])];
    _text4.backgroundColor = [UIColor blackColor];
    _text4.layer.borderColor = UIColor.grayColor.CGColor;
    _text4.layer.borderWidth = 1;
    _text4.text = [NSString stringWithFormat:@"%d",[_array[3] intValue]];
    _text4.textColor = [UIColor whiteColor];
    [self.view addSubview:_text4];
    
    _text5 = [[UITextView alloc] initWithFrame:CGRectMake(128, 50, 32, [_array[4] floatValue])];
    _text5.backgroundColor = [UIColor blackColor];
    _text5.layer.borderColor = UIColor.grayColor.CGColor;
    _text5.layer.borderWidth = 1;
    _text5.text = [NSString stringWithFormat:@"%d",[_array[4] intValue]];
    _text5.textColor = [UIColor whiteColor];
    [self.view addSubview:_text5];
    
    _text6 = [[UITextView alloc] initWithFrame:CGRectMake(160, 50, 32, [_array[5] floatValue])];
    _text6.backgroundColor = [UIColor blackColor];
    _text6.layer.borderColor = UIColor.grayColor.CGColor;
    _text6.layer.borderWidth = 1;
    _text6.text = [NSString stringWithFormat:@"%d",[_array[5] intValue]];
    _text6.textColor = [UIColor whiteColor];
    [self.view addSubview:_text6];
    
    _text7 = [[UITextView alloc] initWithFrame:CGRectMake(192, 50, 32, [_array[6] floatValue])];
    _text7.backgroundColor = [UIColor blackColor];
    _text7.layer.borderColor = UIColor.grayColor.CGColor;
    _text7.layer.borderWidth = 1;
    _text7.text = [NSString stringWithFormat:@"%d",[_array[6] intValue]];
    _text7.textColor = [UIColor whiteColor];
    [self.view addSubview:_text7];
    
    _text8 = [[UITextView alloc] initWithFrame:CGRectMake(224, 50, 32, [_array[7] floatValue])];
    _text8.backgroundColor = [UIColor blackColor];
    _text8.layer.borderColor = UIColor.grayColor.CGColor;
    _text8.layer.borderWidth = 1;
    _text8.text = [NSString stringWithFormat:@"%d",[_array[7] intValue]];
    _text8.textColor = [UIColor whiteColor];
    [self.view addSubview:_text8];
    
    _text9 = [[UITextView alloc] initWithFrame:CGRectMake(256, 50, 32, [_array[8] floatValue])];
    _text9.backgroundColor = [UIColor blackColor];
    _text9.layer.borderColor = UIColor.grayColor.CGColor;
    _text9.layer.borderWidth = 1;
    _text9.text = [NSString stringWithFormat:@"%d",[_array[8] intValue]];
    _text9.textColor = [UIColor whiteColor];
    [self.view addSubview:_text9];
    
    _text10 = [[UITextView alloc] initWithFrame:CGRectMake(288, 50, 32, [_array[9] floatValue])];
    _text10.backgroundColor = [UIColor blackColor];
    _text10.layer.borderColor = UIColor.grayColor.CGColor;
    _text10.layer.borderWidth = 1;
    _text10.text = [NSString stringWithFormat:@"%d",[_array[9] intValue]];
    _text10.textColor = [UIColor whiteColor];
    [self.view addSubview:_text10];
    

    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(0, 270, 320, 40);
    _button.backgroundColor = [UIColor blueColor];
    [_button setTitle:@"从小到大排列" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
}

-(void)click{
    if(i<10) {
        //这里为升序排序,比较相邻数字的大小,大的放后
        for (int j = 0; j < 10 - i-1; j++) {
            [_button setTitle:[NSString stringWithFormat:@"从小到大排列(第%d次循环结束)",i+1] forState:UIControlStateNormal];
            if ([_array[j] intValue] > [_array[j + 1] intValue]) {
                int temp = [_array[j] intValue];
                _array[j] = _array[j + 1];
                _array[j + 1] = [NSString stringWithFormat:@"%d",temp];
            }
        }
        [self deleyMethod];
        i++;
        if(i==10){
            [_button setTitle:@"从大到小排列" forState:UIControlStateNormal];
        }
    }else{
        if(i<20){
        
            //这里降序排序,比较相邻数字的大小,小的放后
            for (int j = 0; j < 20 - i-1; j++) {
                [_button setTitle:[NSString stringWithFormat:@"从大到小排列(第%d次循环结束)",i-9] forState:UIControlStateNormal];
                if ([_array[j] intValue] < [_array[j + 1] intValue]) {
                    int temp = [_array[j] intValue];
                    _array[j] = _array[j + 1];
                    _array[j + 1] = [NSString stringWithFormat:@"%d",temp];
                }
            }
            [self deleyMethod];
            i++;
            if(i==20){
                [_button setTitle:@"从小到大排列" forState:UIControlStateNormal];
                i=0;
            }
        }
    }
}

-(void)deleyMethod{
    [UIView beginAnimations:nil context:nil];
    _text1.text = [NSString stringWithFormat:@"%d",[_array[0] intValue]];
    _text1.frame = CGRectMake(0, 50, 32, [_array[0] floatValue]);

    _text2.text = [NSString stringWithFormat:@"%d",[_array[1] intValue]];
    _text2.frame = CGRectMake(32, 50, 32, [_array[1] floatValue]);
    
    _text3.text = [NSString stringWithFormat:@"%d",[_array[2] intValue]];
    _text3.frame = CGRectMake(64, 50, 32, [_array[2] floatValue]);
    
    _text4.text = [NSString stringWithFormat:@"%d",[_array[3] intValue]];
    _text4.frame = CGRectMake(96, 50, 32, [_array[3] floatValue]);
    
    _text5.text = [NSString stringWithFormat:@"%d",[_array[4] intValue]];
    _text5.frame = CGRectMake(128, 50, 32, [_array[4] floatValue]);
    
    _text6.text = [NSString stringWithFormat:@"%d",[_array[5] intValue]];
    _text6.frame = CGRectMake(160, 50, 32, [_array[5] floatValue]);
    
    _text7.text = [NSString stringWithFormat:@"%d",[_array[6] intValue]];
    _text7.frame = CGRectMake(192, 50, 32, [_array[6] floatValue]);
    
    _text8.text = [NSString stringWithFormat:@"%d",[_array[7] intValue]];
    _text8.frame = CGRectMake(224, 50, 32, [_array[7] floatValue]);
    
    _text9.text = [NSString stringWithFormat:@"%d",[_array[8] intValue]];
    _text9.frame = CGRectMake(256, 50, 32, [_array[8] floatValue]);
   
    _text10.text = [NSString stringWithFormat:@"%d",[_array[9] intValue]];
    _text10.frame = CGRectMake(288, 50, 32, [_array[9] floatValue]);
                
    [UIView setAnimationDuration:1.0f];
    [UIView commitAnimations];
}

@end

Android代码

布局文件



    
        
        
        
        
        
        
        
        
        
        
    

    

MainActivity文件

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{
    static int i = 0;
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private TextView text5;
    private TextView text6;
    private TextView text7;
    private TextView text8;
    private TextView text9;
    private TextView text10;
    private Button button;
    private int[] array;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        array = new int[]{117,198,177,100,155,127,188,137,168,144};
        
        text1 = (TextView)findViewById(R.id.text01);
        text1.setText(String.valueOf(array[0]));
        LinearLayout.LayoutParams linearParams1 =(LinearLayout.LayoutParams) text1.getLayoutParams();
        linearParams1.height = array[0]*3;
        text1.setLayoutParams(linearParams1);
        text2 = (TextView)findViewById(R.id.text02);
        text2.setText(String.valueOf(array[1]));
        LinearLayout.LayoutParams linearParams2 =(LinearLayout.LayoutParams) text2.getLayoutParams();
        linearParams2.height = array[1]*3;
        text2.setLayoutParams(linearParams2);
        text3 = (TextView)findViewById(R.id.text03);
        text3.setText(String.valueOf(array[2]));
        LinearLayout.LayoutParams linearParams3 =(LinearLayout.LayoutParams) text3.getLayoutParams();
        linearParams3.height = array[2]*3;
        text3.setLayoutParams(linearParams3);
        text4 = (TextView)findViewById(R.id.text04);
        text4.setText(String.valueOf(array[3]));
        LinearLayout.LayoutParams linearParams4 =(LinearLayout.LayoutParams) text4.getLayoutParams();
        linearParams4.height = array[3]*3;
        text4.setLayoutParams(linearParams4);
        text5 = (TextView)findViewById(R.id.text05);
        text5.setText(String.valueOf(array[4]));
        LinearLayout.LayoutParams linearParams5 =(LinearLayout.LayoutParams) text5.getLayoutParams();
        linearParams5.height = array[4]*3;
        text5.setLayoutParams(linearParams5);
        text6 = (TextView)findViewById(R.id.text06);
        text6.setText(String.valueOf(array[5]));
        LinearLayout.LayoutParams linearParams6 =(LinearLayout.LayoutParams) text6.getLayoutParams();
        linearParams6.height = array[5]*3;
        text6.setLayoutParams(linearParams6);
        text7 = (TextView)findViewById(R.id.text07);
        text7.setText(String.valueOf(array[6]));
        LinearLayout.LayoutParams linearParams7 =(LinearLayout.LayoutParams) text7.getLayoutParams();
        linearParams7.height = array[6]*3;
        text7.setLayoutParams(linearParams7);
        text8 = (TextView)findViewById(R.id.text08);
        text8.setText(String.valueOf(array[7]));
        LinearLayout.LayoutParams linearParams8 =(LinearLayout.LayoutParams) text8.getLayoutParams();
        linearParams8.height = array[7]*3;
        text8.setLayoutParams(linearParams8);
        text9 = (TextView)findViewById(R.id.text09);
        text9.setText(String.valueOf(array[8]));
        LinearLayout.LayoutParams linearParams9 =(LinearLayout.LayoutParams) text9.getLayoutParams();
        linearParams9.height = array[8]*3;
        text9.setLayoutParams(linearParams9);
        text10 = (TextView)findViewById(R.id.text10);
        text10.setText(String.valueOf(array[9]));
        LinearLayout.LayoutParams linearParams10 =(LinearLayout.LayoutParams) text10.getLayoutParams();
        linearParams10.height = array[9]*3;
        text10.setLayoutParams(linearParams10);

        button = (Button)findViewById(R.id.button1);

    }
    public void click(View view){
        if(i<10){
            for(int j=0;jarray[j+1]){
                    int temp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=temp;
                }
            }
            showNewview();
            i++;
            if(i==10){
                button.setText("从大到小排列");
            }
        }else{
            if(i<20){
                for(int j=0;j

你可能感兴趣的:(冒泡排序原理iOS与Android演示)