Java Bubble Sort bubbleSort(int array[], int index[])

Here you can find the source of bubbleSort(int array[], int index[])

Description

Bubble Sort for Integer array

License

Apache License

Parameter

Parameter Description
array Value of the original array
index Index of the original array

Return

Array is sorted in ascending order (lowest to highest). Together with its original index stored in index[]

Declaration

static public int[] bubbleSort(int array[], int index[]) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**// w ww.  j a va2s . c  om
     * Bubble Sort for Integer array
     * 
     * @param array
     *            Value of the original array
     * @param index
     *            Index of the original array
     * @return Array is sorted in ascending order (lowest to highest). Together
     *         with its original index stored in index[]
     */
    static public int[] bubbleSort(int array[], int index[]) {
        boolean swappedOnPrevRun = true;
        while (swappedOnPrevRun) {
            swappedOnPrevRun = false;
            // this variable keeps track of whether to continue sorting or exit
            for (int i = 0; i < array.length - 1; i++)
            // loop through every element in the array,
            // except for the last one
            {
                if (array[i] > array[i + 1])
                // if current element is greater than the next
                {
                    // swap the two elements
                    swappedOnPrevRun = true; // we don't want the loop to end
                    // just yet, we're not done
                    int temp = array[i]; // store element i in a temporary
                                         // variable
                    array[i] = array[i + 1]; // set element i+1 to where i used
                    // to be
                    array[i + 1] = temp; // release the old i from temp into i+1
                                         // slot

                    temp = index[i];
                    index[i] = index[i + 1];
                    index[i + 1] = temp;
                }
            }
        }
        return array;
    }

    /**
     * Bubble Sort for Double array
     * 
     * @param array
     *            Value of the original array
     * @param index
     *            Index of the original array
     * @return Array is sorted in ascending order (lowest to highest). Together
     *         with its original index stored in index[]
     */
    static public double[] bubbleSort(double array[], int index[]) {
        boolean swappedOnPrevRun = true;
        while (swappedOnPrevRun) {
            swappedOnPrevRun = false;
            // this variable keeps track of whether to continue sorting or exit
            for (int i = 0; i < array.length - 1; i++)
            // loop through every element in the array,
            // except for the last one
            {
                if (array[i] > array[i + 1])
                // if current element is greater than the next
                {
                    // swap the two elements
                    swappedOnPrevRun = true; // we don't want the loop to end
                    // just yet, we're not done
                    double temp = array[i]; // store element i in a temporary
                    // variable
                    array[i] = array[i + 1]; // set element i+1 to where i used
                    // to be
                    array[i + 1] = temp; // release the old i from temp into i+1
                                         // slot

                    int temp1 = index[i];
                    index[i] = index[i + 1];
                    index[i + 1] = temp1;
                }
            }
        }
        return array;
    }
}

Related

  1. bubbleSort(int[] a)
  2. bubbleSort(int[] array)
  3. BubbleSort(int[] data)
  4. bubbleSort(int[] source)