冒泡排序原理iOS与Android演示

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

ios演示

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代码

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.work.eventtest.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/linear1">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text01"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text02"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text03"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text04"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text05"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text06"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text07"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text08"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text09"
            android:layout_weight="1"
            android:textSize="20sp"

            android:background="@drawable/s"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/text10"
            android:layout_weight="1"
            android:textSize="20sp"
            android:background="@drawable/s"/>
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@id/linear1"
        android:id="@+id/button1"
        android:layout_marginTop="0dp"
        android:text="从小到大排序"
        android:onClick="click"/>


</RelativeLayout>

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;j<array.length-1-i;j++){//内层循环控制每一趟排序多少次
                button.setText("从小到大排列"+"(第"+(i+1)+"次循环结束)");
                if(array[j]>array[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<array.length-1-i+10;j++){
                    button.setText("从大到小排列"+"(第"+(i-9)+"次循环结束)");
                    if(array[j]<array[j+1]){
                        int temp=array[j];
                        array[j]=array[j+1];
                        array[j+1]=temp;
                    }
                }
                showNewview();
                i++;
                if(i==20){
                    button.setText("从小到大排列");
                    i=0;
                }
            }
        }

    }
    private void showNewview(){
        text1.setText(String.valueOf(array[0]));
        LinearLayout.LayoutParams linearParams1 =(LinearLayout.LayoutParams) text1.getLayoutParams();
        linearParams1.height = array[0]*3;
        text1.setLayoutParams(linearParams1);

        text2.setText(String.valueOf(array[1]));
        LinearLayout.LayoutParams linearParams2 =(LinearLayout.LayoutParams) text2.getLayoutParams();
        linearParams2.height = array[1]*3;
        text2.setLayoutParams(linearParams2);

        text3.setText(String.valueOf(array[2]));
        LinearLayout.LayoutParams linearParams3 =(LinearLayout.LayoutParams) text3.getLayoutParams();
        linearParams3.height = array[2]*3;
        text3.setLayoutParams(linearParams3);

        text4.setText(String.valueOf(array[3]));
        LinearLayout.LayoutParams linearParams4 =(LinearLayout.LayoutParams) text4.getLayoutParams();
        linearParams4.height = array[3]*3;
        text4.setLayoutParams(linearParams4);

        text5.setText(String.valueOf(array[4]));
        LinearLayout.LayoutParams linearParams5 =(LinearLayout.LayoutParams) text5.getLayoutParams();
        linearParams5.height = array[4]*3;
        text5.setLayoutParams(linearParams5);

        text6.setText(String.valueOf(array[5]));
        LinearLayout.LayoutParams linearParams6 =(LinearLayout.LayoutParams) text6.getLayoutParams();
        linearParams6.height = array[5]*3;
        text6.setLayoutParams(linearParams6);

        text7.setText(String.valueOf(array[6]));
        LinearLayout.LayoutParams linearParams7 =(LinearLayout.LayoutParams) text7.getLayoutParams();
        linearParams7.height = array[6]*3;
        text7.setLayoutParams(linearParams7);

        text8.setText(String.valueOf(array[7]));
        LinearLayout.LayoutParams linearParams8 =(LinearLayout.LayoutParams) text8.getLayoutParams();
        linearParams8.height = array[7]*3;
        text8.setLayoutParams(linearParams8);

        text9.setText(String.valueOf(array[8]));
        LinearLayout.LayoutParams linearParams9 =(LinearLayout.LayoutParams) text9.getLayoutParams();
        linearParams9.height = array[8]*3;
        text9.setLayoutParams(linearParams9);

        text10.setText(String.valueOf(array[9]));
        LinearLayout.LayoutParams linearParams10 =(LinearLayout.LayoutParams) text10.getLayoutParams();
        linearParams10.height = array[9]*3;
        text10.setLayoutParams(linearParams10);
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容