Bubble sort
public class BubbleSort {
private long[] a;
private int nElems;
public BubbleSort(int max) {
a = new long[max];
nElems = 0;
}
// put element into array
public void insert(long value) {
a[nElems] = value;
nElems++;
}
// displays array contents
public void display() {
for (int j = 0; j < nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public void bubbleSort() {
int out, in;
for (out = nElems - 1; out > 1; out--)
// outer loop (backward)
for (in = 0; in < out; in++)
// inner loop (forward)
if (a[in] > a[in + 1]) // out of order?
swap(in, in + 1); // swap them
}
private void swap(int one, int two) {
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public static void main(String[] args) {
int maxSize = 100; // array size
BubbleSort arr; // reference to array
arr = new BubbleSort(maxSize);
arr.insert(77); // insert 10 items
arr.insert(66);
arr.insert(44);
arr.insert(34);
arr.insert(22);
arr.insert(88);
arr.insert(12);
arr.insert(00);
arr.insert(55);
arr.insert(33);
arr.display();
arr.bubbleSort();
arr.display();
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Algorithm:
- Binary search
- Binary Search Insert
- Recursive Binary Search Implementation in Java
- Linear Searching double Arrays
- Bubble sort
- Heap sort
- Merge sort
- Fast Merge Sort
- Fast Quick Sort
- Simple version of quick sort
- Quicksort for sorting arrays
- Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
- Quick sort with median-of-three partitioning
- Insert sort
- Insert Sort for objects
- Selection sort
- Shell sort
- Fibonacci
- Hanoi puzzle
- Table of fahrenheit and celsius temperatures
- Growable integer array
- Linked List class
- Binary Tree
- Tree with generic user object