Home > Java > javaTutorial > Introduction to bubble sorting of java arrays

Introduction to bubble sorting of java arrays

高洛峰
Release: 2017-03-09 18:56:11
Original
1569 people have browsed it

This article introduces the introduction of bubble sorting of java arrays

package demo;

import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args) {
        int[] arr = new int[10];
        for(int i=0; i<arr.length; i++){
            arr[i] = (int) (Math.random()*100);
        }
        System.out.println("冒泡排序前:");
        System.out.println("第"+(0)+"次:"+Arrays.toString(arr));
        
        bubbleSort(arr);
        System.out.println("冒泡排序前:");
        System.out.println("第"+(0)+"次:"+Arrays.toString(arr));
    }
    /*
     * 冒泡排序
     */
    public static void bubbleSort(int a[]) {   
       for (int i = 0; i < a.length - 1; i++) {   
               for (int j = 0; j < a.length - 1; j++) {   
                   if (a[j] > a[j + 1]) {   
                       int temp = a[j];   
                       a[j] = a[j + 1];   
                       a[j + 1] = temp;   
                   }   
            }
               System.out.println("第"+(i+1)+"次:"+Arrays.toString(a));
       }
    }   
}
Copy after login


The above is the detailed content of Introduction to bubble sorting of java arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template