博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
冒泡排序
阅读量:4478 次
发布时间:2019-06-08

本文共 2003 字,大约阅读时间需要 6 分钟。

/**  bubbleSort.java*  demonstrates bubble sort*  use long[] a for example*/package pers.sort_bubble;class ArrayBub {    private long[] a;        //ref to array a    private int nElems;        //number of data array//……………………..    public ArrayBub(int max) {        //constructor        a = new long[max];            //create the array        nElems = 0;                    //no items yet    }//……………………..    public void insert(long value) {        //put element into array        a[nElems] = value;                    //insert it        nElems++;                            //increment size    }//……………………..    public void display() {                    //display array contents        for (int j = 0; j < nElems; j++) {        //for each element            System.out.print(a[j] + "\t");        //display it        }        System.out.println("");    }//……………………..        public void bubbleSort() {        int out, in;        for (out = nElems-1; out > 0; out--) {        //outer loop nElems-1 times            for (in = 0; in < out; in++) {            //inner loop                if (a[in] > a[in+1]) {        //out of order?                    swap(in, in+1);            //swap                }            }        }    }//……………………..    private void swap(int one, int two) {        long temp;        temp = a[one];        a[one] = a[two];        a[two] = temp;    }}//end class ArrayBubpublic class BubbleSortApp {    public static void main(String[] args) {        int maxSize = 100;        //array size        ArrayBub arr;            //reference to array        arr = new ArrayBub(maxSize);        //creat the array        arr.insert(100);        //insert items        arr.insert(9);        arr.insert(65);        arr.insert(77);        arr.insert(55);        arr.insert(2);        arr.insert(41);        arr.insert(1);        arr.display();        //display them        arr.bubbleSort();        //bubble sort them         arr.display();    }    }

转载于:https://www.cnblogs.com/mmsumz/p/7851100.html

你可能感兴趣的文章
图的遍历(深度优先与广度优先搜索两种方案)
查看>>
快速读入模板
查看>>
\n ^ \t的使用
查看>>
css盒模型
查看>>
探索式测试:测试自动化
查看>>
make install fping
查看>>
面试笔试题
查看>>
#loj3051 [十二省联考2019] 皮配
查看>>
MySql可视化工具MySQL Workbench使用教程
查看>>
个人站立会议第二阶段07
查看>>
云时代架构阅读笔记五——Web应用安全
查看>>
IOS 单击手势和cell点击冲突
查看>>
学习_HTML5_day3
查看>>
计算机网络与应用第二次笔记
查看>>
Django之ORM查询
查看>>
学习python第七天
查看>>
Flask基础(07)-->正则自定义转换器
查看>>
C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)
查看>>
Spring Boot构建RESTful API与单元测试
查看>>
【JavaScript你需要知道的基础知识~】
查看>>