首页 > 其他分享 >数组冒泡排序

数组冒泡排序

时间:2023-09-29 14:57:32浏览次数:32  
标签:tmp 数组 int System 冒泡排序 ++ ints out

  • 第1次推导
public class test {
    public static void main(String[] args) {
        int[] ints = {6, 5, 9, 5};
        int tmp;

        if(ints[0]>ints[1]){
            tmp = ints[0];
            ints[0] = ints[1];
            ints[1] = tmp;
        }
        if(ints[1]>ints[2]){
            tmp = ints[1];
            ints[1] = ints[2];
            ints[2] = tmp;
        }
        if(ints[2]>ints[3]){
            tmp = ints[2];
            ints[2] = ints[3];
            ints[3] = tmp;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

        if(ints[0]>ints[1]){
            tmp = ints[0];
            ints[0] = ints[1];
            ints[1] = tmp;
        }
        if(ints[1]>ints[2]){
            tmp = ints[1];
            ints[1] = ints[2];
            ints[2] = tmp;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

        if(ints[0]>ints[1]){
            tmp = ints[0];
            ints[0] = ints[1];
            ints[1] = tmp;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

    }
}
  • 第2次推导
public class test {
    public static void main(String[] args) {
        int[] ints = {6, 5, 9, 5};
        int tmp;

        int i=0;
        int j=1;
        for(int a = 3; a>0; a--){
            if(ints[i]>ints[j]){
                tmp = ints[i];
                ints[i] = ints[j];
                ints[j] = tmp;
            }
            i++;
            j++;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

        i=0;
        j=1;
        for(int a = 2; a>0; a--){
            if(ints[i]>ints[j]){
                tmp = ints[i];
                ints[i] = ints[j];
                ints[j] = tmp;
            }
            i++;
            j++;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

        i=0;
        j=1;
        for(int a = 1; a>0; a--){
            if(ints[i]>ints[j]){
                tmp = ints[i];
                ints[i] = ints[j];
                ints[j] = tmp;
            }
            i++;
            j++;
        }
        System.out.println(ints[0] + "," + ints[1] + "," + ints[2] + "," + ints[3]);

    }
}
  • 最终完善
public class test {
    public static void main(String[] args) {
        int[] ints = {6, 5, 9, 2, 5, 6,7,3};
        for(int b = ints.length-1; b>0; b--){
            int tmp;
            int i=0;
            for(int a = b; a>0; a--){
                if(ints[i]>ints[i+1]){
                    tmp = ints[i];
                    ints[i] = ints[i+1];
                    ints[i+1] = tmp;
                }
                i++;
            }
        }
        for (int anInt : ints) {
            System.out.print(anInt + ",");
        }

    }
}

标签:tmp,数组,int,System,冒泡排序,++,ints,out
From: https://www.cnblogs.com/dogleftover/p/17736992.html

相关文章

  • 数组插入排序
    第1次推导publicclasstest{publicstaticvoidmain(String[]args){int[]ints={2,5,3,1,8,9};inttmp;//存储临时变量intj;//开始比较第几位的数//第1次//2,5,3,1,8,9j=1;//比较索引1的数......
  • [LeetCode] 2251. 花期内花的数目 - 二分查找/有序数组
    Problem:2251.花期内花的数目思路看题目应该是一道比较经典的差分,本来准备拿差分数组做的,后来搂了一眼题解,发现用二分的方法更简单解题方法此题有一种很简便的方法,第i个人到达时间为people[i],所以我们不难找到在这个时间之前花期已经开始的花的数量,即v1=start<=people[i]......
  • c语言,一维数组指针
    @TOC前言今天我们讲一下一维数组指针。一、一维数组指针的定义:概述:数组指针,就是数组类型的指针。数组里面的每一个元素都是一个地址。可以让数组指针指向一个数组的地址,通过地址遍历数组的各个元素。定义一维数组指针的步骤:inta[5]={4,5,6,7,8};//定义一个数组int(*......
  • 2023-09-13:用go语言,给定一个整数数组 nums 和一个正整数 k, 找出是否有可能把这个数组
    2023-09-13:用go语言,给定一个整数数组nums和一个正整数k,找出是否有可能把这个数组分成k个非空子集,其总和都相等。输入:nums=[4,3,2,3,5,2,1],k=4。输出:True。答案2023-09-13:第一种算法(canPartitionKSubsets1)使用动态规划的思想,具体过程如下:1.计算数组nums的总和sum......
  • 前端 数组删除元素
    以下是几种常用的数组删除数据的方式:使用pop()方法:pop()方法用于删除数组的最后一个元素,并返回被删除的元素。//示例数组letarr=[1,2,3,4];//删除最后一个元素letremovedElement=arr.pop();console.log(arr);//[1,2,3]console.log(removedElement);//4使......
  • 小程序数组数据变化渲染的不是最新值?
    微信小程序切换后数据不是最新值如果是相同的key此时vue会跳过更新复用上次的dom和值,把第二个的别名item换成不相同的,比如item1,还不行就第二个的index换成index1key也换成index1<viewclass="queryCert-result-list"v-if="list.length>0"><viewclass="queryCert-result-list......
  • 数组能改变大小吗
    提问数组能改变大小吗回答能usingSystem;publicclassSamplesArray{publicstaticvoidMain(){//Createandinitializeanewstringarray.String[]myArr={"The","quick","brown","fox","jump......
  • 如何将数组中元素为空的数据过滤掉?
    场景:后台返回所有文件列表信息,需要将fileId有值的文件过滤出来回显到页面上。错误处理:使用map+if判断letarr=[{fileId:'1',fileName:'缴费明细表'},{fileId:'2',fileName:'支付明细表'},{fileId......
  • 根据一个数组,创建一个Segment Tree(线段树)
    线段树的特点线段树的优势线段树的构造过程(0,5)37:数组元素下标0~5的元素之和是37(0,2)21:数组元素下标0~2的元素之和是21线段树的基本数据结构(结点结构由五个分量组成)运行结果(C语言代码)递归的创建一颗线段树,然后中序、先序、后序遍历这个结点#include<stdio.h>#include<st......
  • js 检索数组对象中某个属性的值是否不相等
    //判断选中的项是否有不相同的模型consthasNameProperty=_this.selectedOrderList.filter(order=>order.hasOwnProperty("item_model_id"));if(hasNameProperty.length>0){constisEveryNameEqual=hasNameProperty.reduce((prev,curr)=>{if......