package yi.ds.sort;

import java.util.*;

/**
 * Sorting utility functions
 */

public class SortUtil {
  /**
   * Sort the data and return their index in the sorted list
   * @param data
   * @return
   */
  public static int[] sort(final double[] data, boolean small_first) {
    // array to store the index of the data
    Integer[] index = new Integer[data.length];
    for (int i=0; i<data.length; i++) {
      index[i] = new Integer(i);
    }

    if (small_first) {
      // sort the index
      Arrays.sort(index, new Comparator() {
        public int compare(Object o1, Object o2) {
          int index1 = ( (Integer) o1).intValue();
          int index2 = ( (Integer) o2).intValue();
          if (data[index1] < data[index2])
            return -1;
          else if (data[index1] > data[index2])
            return 1;
          return 0;
        }
      });
    } else {
      // sort the index
      Arrays.sort(index, new Comparator() {
        public int compare(Object o1, Object o2) {
          int index1 = ( (Integer) o1).intValue();
          int index2 = ( (Integer) o2).intValue();
          if (data[index1] > data[index2])
            return -1;
          else if (data[index1] < data[index2])
            return 1;
          return 0;
        }
      });
    }

    return yi.util.data.Conversion.toIntArray(index);
  }

  public static int[] sort(final double[] data) {
    return sort(data, true);
  }


  /**
   * Return the index of the minimum
   * @param data
   * @return
   */
  public static int minimum(double[] data) {
    double min_score = Double.POSITIVE_INFINITY;
    int min = 0;
    for (int i=0; i<data.length; i++) {
      if (data[i] < min_score) {
        min_score = data[i];
        min = i;
      }
    }
    return min;
  }

  /**
   * Return the index of the maximum
   * @param data
   * @return
   */
  public static int maximum(double[] data) {
    double max_score = Double.NEGATIVE_INFINITY;
    int max = 0;
    for (int i=0; i<data.length; i++) {
      if (data[i] > max_score) {
        max_score = data[i];
        max = i;
      }
    }
    return max;
  }

  public static int[] sort(final int[] data, boolean small_first) {
    // array to store the index of the data
    Integer[] index = new Integer[data.length];
    for (int i=0; i<data.length; i++) {
      index[i] = new Integer(i);
    }

    if (small_first) {
      // sort the index
      Arrays.sort(index, new Comparator() {
        public int compare(Object o1, Object o2) {
          int index1 = ( (Integer) o1).intValue();
          int index2 = ( (Integer) o2).intValue();
          if (data[index1] < data[index2])
            return -1;
          else if (data[index1] > data[index2])
            return 1;
          return 0;
        }
      });
    } else {
      // sort the index
      Arrays.sort(index, new Comparator() {
        public int compare(Object o1, Object o2) {
          int index1 = ( (Integer) o1).intValue();
          int index2 = ( (Integer) o2).intValue();
          if (data[index1] > data[index2])
            return -1;
          else if (data[index1] < data[index2])
            return 1;
          return 0;
        }
      });
    }

    return yi.util.data.Conversion.toIntArray(index);
  }

}