Java sorting algorithm--bubble sort

Source: Internet
Author: User

Import java.util.arrays;//=================================================//File name:bubble_sort//------------ ------------------------------------------------------------------//author:common//class Name: arrays_bubble//property://Method: C Lass arrays_bubble{private int[] arrays;private int curnum;public arrays_bubble (int max) {//Create an empty array of Max lengths super (); arrays = new Int[max];curnum = 0;} public void Insert (int value) {//empty array add element arrays[curnum] = value;curnum++;} public void display () {//display array System.out.println (arrays.tostring (Arrays));} private void Swap (int one,int) {//Interchange int temp = Arrays[one];arrays[one] = Arrays[two];arrays[two] = temp;} public void Bubblesort () {int out,in;for (out=curnum-1;out>0;out--) {//curnum is prepared for the next insert and should be subtracted from 1for (in=0;in< out;in++) {//start with the last number, compare the maximum to the last if (Arrays[in]>arrays[out]) {swap (in,in+1), and the previous number,}}} Main class//function:bubble_sortpublic class Bubble_sort {public static void main (string[] args) {//TODO auto-generated method stub int maxSize = 100; Arrays_bubble Arrays_demo = new Arrays_bubble (maxSize); Arrays_demo.insert (; arrays_demo.insert); Arrays_demo.insert Demo.insert (Arrays_demo.insert); Arrays_demo.display (); Arrays_demo. Bubblesort (); Arrays_demo.display ();}}

Java sorting algorithm--bubble sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.