Bubble Sort int array - Java Data Structure

Java examples for Data Structure:Sort

Description

Bubble Sort int array

Demo Code


//package com.java2s;

public class Main {

    public static void BubbleSort(int[] array) {

        int size = array.length;
        for (int i = 0; i < size - 1; i++) {
            boolean hasSwap = false;
            for (int j = i + 1; j < size; j++) {
                if (array[j] < array[i]) {
                    // swap
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;/*from www  .j  ava 2 s  .c om*/
                    hasSwap = true;
                }
            }
            if (!hasSwap) {
                break;
            }
        }

    }
}

Related Tutorials