All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sorts.BubbleSort Maven / Gradle / Ivy

package sorts;

/**
 * BubbleSort.java
 * Created by Stijn Strickx on May 21, 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */

/**
 * Bubble sort algorithm Time Complexity: O(n*n) Memory Complexity: O(1) Stable:
 * yes
 */

public class BubbleSort extends Sorter {

  @Override
  public > void sort(T[] a) {
    boolean swapped = true;
    int i = a.length - 1;
    while (swapped && i >= 0) {
      swapped = false;
      for (int j = 0; j < i; j++) {
        if (a[j].compareTo(a[j + 1]) > 0) {
          swap(a, j, j + 1);
          swapped = true;
        }
      }
      i--;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy