congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
SortUtils
Code IndexAdd Tabnine to your IDE (free)

How to use
SortUtils
in
smile.sort

Best Java code snippets using smile.sort.SortUtils (Showing top 20 results out of 315)

origin: com.github.haifengl/smile-math

/**
 * Sorts the specified array into ascending numerical order.
 */
public static void sort(float[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
  for (int i = n - 1; i > 0; i--) {
    SortUtils.swap(arr, 0, i);
    SortUtils.siftDown(arr, 0, i - 1);
  }
}
origin: com.github.haifengl/smile-math

/**
 * Reverses the order of the elements in the specified array.
 * @param a an array to reverse.
 */
public static <T> void reverse(T[] a) {
  int i = 0, j = a.length - 1;
  while (i < j) {
    SortUtils.swap(a, i++, j--);
  }
}
origin: com.github.haifengl/smile-math

/**
 * Place the array in max-heap order. Note that the array is not fully sorted.
 */
private static <T extends Comparable<? super T>> void heapify(T[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
}
origin: com.github.haifengl/smile-math

/**
 * To restore the max-heap condition when a node's priority is increased.
 * We move up the heap, exchaning the node at position k with its parent
 * (at postion k/2) if necessary, continuing as long as a[k/2] &lt; a[k] or
 * until we reach the top of the heap.
 */
public static void siftUp(float[] arr, int k) {
  while (k > 1 && arr[k/2] < arr[k]) {
    swap(arr, k, k/2);
    k = k/2;
  }
}
origin: com.github.haifengl/smile-math

/**
 * Place the array in max-heap order. Note that the array is not fully sorted.
 */
private static void heapify(double[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
}
origin: com.github.haifengl/smile-math

/**
 * Reverses the order of the elements in the specified array.
 * @param a an array to reverse.
 */
public static void reverse(double[] a) {
  int i = 0, j = a.length - 1;
  while (i < j) {
    SortUtils.swap(a, i++, j--);  // code for swap not shown, but easy enough
  }
}
origin: com.github.haifengl/smile-math

/**
 * Sorts the specified array into ascending numerical order.
 */
public static void sort(int[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
  for (int i = n - 1; i > 0; i--) {
    SortUtils.swap(arr, 0, i);
    SortUtils.siftDown(arr, 0, i - 1);
  }
}
origin: com.github.haifengl/smile-math

/**
 * Place the array in max-heap order. Note that the array is not fully sorted.
 */
private static void heapify(float[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
}
origin: com.github.haifengl/smile-math

/**
 * To restore the max-heap condition when a node's priority is increased.
 * We move up the heap, exchaning the node at position k with its parent
 * (at postion k/2) if necessary, continuing as long as a[k/2] &lt; a[k] or
 * until we reach the top of the heap.
 */
public static void siftUp(double[] arr, int k) {
  while (k > 1 && arr[k/2] < arr[k]) {
    swap(arr, k, k/2);
    k = k/2;
  }
}
origin: com.github.haifengl/smile-math

/**
 * Sorts the specified array into ascending numerical order.
 */
public static void sort(double[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
  for (int i = n - 1; i > 0; i--) {
    SortUtils.swap(arr, 0, i);
    SortUtils.siftDown(arr, 0, i - 1);
  }
}
origin: com.github.haifengl/smile-math

/**
 * Place the array in max-heap order. Note that the array is not fully sorted.
 */
private static void heapify(int[] arr) {
  int n = arr.length;
  for (int i = n / 2 - 1; i >= 0; i--)
    SortUtils.siftDown(arr, i, n - 1);
}
origin: com.github.haifengl/smile-math

/**
 * To restore the max-heap condition when a node's priority is increased.
 * We move up the heap, exchaning the node at position k with its parent
 * (at postion k/2) if necessary, continuing as long as a[k/2] &lt; a[k] or
 * until we reach the top of the heap.
 */
public static void siftUp(int[] arr, int k) {
  while (k > 1 && arr[k/2] < arr[k]) {
    swap(arr, k, k/2);
    k = k/2;
  }
}
origin: com.github.haifengl/smile-math

  /**
   * Sorts the specified array into ascending order.
   */
  public static <T extends Comparable<? super T>> void sort(T[] arr) {
    int n = arr.length;
    for (int i = n / 2 - 1; i >= 0; i--)
      SortUtils.siftDown(arr, i, n - 1);

    for (int i = n - 1; i > 0; i--) {
      SortUtils.swap(arr, 0, i);
      SortUtils.siftDown(arr, 0, i - 1);
    }
  }
}
origin: com.github.haifengl/smile-math

/**
 * In case of avoiding creating new objects frequently, one may check and
 * update the peek object directly and call this method to sort the internal
 * array in heap order.
 */
public void heapify() {
  if (n < k) {
    throw new IllegalStateException();
  }
  SortUtils.siftDown(heap, 0, k-1);
}
origin: com.github.haifengl/smile-math

/**
 * Reverses the order of the elements in the specified array.
 * @param a an array to reverse.
 */
public static void reverse(int[] a) {
  int i = 0, j = a.length - 1;
  while (i < j) {
    SortUtils.swap(a, i++, j--);  // code for swap not shown, but easy enough
  }
}
origin: com.github.haifengl/smile-math

/**
 * Assimilate a new value from the stream.
 */
public void add(double datum) {
  sorted = false;
  if (n < k) {
    heap[n++] = datum;
    if (n == k) {
      sort(heap, k);
    }
  } else {
    n++;
    if (datum < heap[0]) {
      heap[0] = datum;
      SortUtils.siftDown(heap, 0, k-1);
    }
  }
}
origin: com.github.haifengl/smile-math

/**
 * Reverses the order of the elements in the specified array.
 * @param a an array to reverse.
 */
public static void reverse(float[] a) {
  int i = 0, j = a.length - 1;
  while (i < j) {
    SortUtils.swap(a, i++, j--);  // code for swap not shown, but easy enough
  }
}
origin: com.github.haifengl/smile-math

/**
 * Assimilate a new value from the stream.
 */
public void add(T datum) {
  sorted = false;
  if (n < k) {
    heap[n++] = datum;
    if (n == k) {
      heapify(heap);
    }
  } else {
    n++;
    if (datum.compareTo(heap[0]) < 0) {
      heap[0] = datum;
      SortUtils.siftDown(heap, 0, k-1);
    }
  }
}
origin: com.github.haifengl/smile-math

/**
 * To restore the max-heap condition when a node's priority is decreased.
 * We move down the heap, exchanging the node at position k with the larger
 * of that node's two children if necessary and stopping when the node at
 * k is not smaller than either child or the bottom is reached. Note that
 * if n is even and k is n/2, then the node at k has only one child -- this
 * case must be treated properly.
 */
public static void siftDown(float[] arr, int k, int n) {
  while (2*k <= n) {
    int j = 2 * k;
    if (j < n && arr[j] < arr[j + 1]) {
      j++;
    }
    if (arr[k] >= arr[j]) {
      break;
    }
    swap(arr, k, j);
    k = j;
  }
}
origin: com.github.haifengl/smile-math

/**
 * Assimilate a new value from the stream.
 */
public void add(int datum) {
  sorted = false;
  if (n < k) {
    heap[n++] = datum;
    if (n == k) {
      heapify(heap);
    }
  } else {
    n++;
    if (datum < heap[0]) {
      heap[0] = datum;
      SortUtils.siftDown(heap, 0, k-1);
    }
  }
}
smile.sortSortUtils

Javadoc

Some useful functions such as swap and swif-down used in many sorting algorithms.

Most used methods

  • siftDown
    To restore the max-heap condition when a node's priority is decreased. We move down the heap, exchan
  • swap
    Swap two positions.

Popular in Java

  • Start an intent from android
  • getApplicationContext (Context)
  • getSupportFragmentManager (FragmentActivity)
  • getSystemService (Context)
  • FileReader (java.io)
    A specialized Reader that reads from a file in the file system. All read requests made by calling me
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • Deque (java.util)
    A linear collection that supports element insertion and removal at both ends. The name deque is shor
  • Servlet (javax.servlet)
    Defines methods that all servlets must implement. A servlet is a small Java program that runs within
  • LogFactory (org.apache.commons.logging)
    Factory for creating Log instances, with discovery and configuration features similar to that employ
  • Join (org.hibernate.mapping)
  • Top plugins for Android Studio
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now