冒泡排序原理 Android 和 iOS 演示

冒泡排序就是把小的元素往前調或者把大的元素日後調。比較是相鄰的兩個元素比較,交換也發生在這兩個元素之間。因此,若是兩個元素相等,不會再無聊地把他們倆交換一下的,因此冒泡排序是一種穩定排序算法。javascript

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

佈局文件java

<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文件android

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);
    }

}複製代碼
相關文章
相關標籤/搜索