001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package algs12;
import stdlib.*;
import java.util.Arrays;
import java.util.Comparator;

/* ***********************************************************************
 *  Compilation:  javac Interval1D.java
 *  Execution:    java Interval1D
 *
 *  1-dimensional interval data type.
 *
 *************************************************************************/

public class Interval1D {
  public static final Comparator<Interval1D> LEFT_ENDPOINT_ORDER  = new LeftComparator();
  public static final Comparator<Interval1D> RIGHT_ENDPOINT_ORDER = new RightComparator();
  public static final Comparator<Interval1D> LENGTH_ORDER         = new LengthComparator();

  private final double left;
  private final double right;

  public Interval1D(double left, double right) {
    if (left <= right) {
      this.left  = left;
      this.right = right;
    }
    else throw new IllegalArgumentException("Illegal interval");
  }

  // left endpoint
  public double left() {
    return left;
  }

  // right endpoint
  public double right() {
    return right;
  }

  // does this interval intersect that one?
  public boolean intersects(Interval1D that) {
    if (this.right < that.left) return false;
    if (that.right < this.left) return false;
    return true;
  }

  // does this interval contain x?
  public boolean contains(double x) {
    return (left <= x) && (x <= right);
  }

  // length of this interval
  public double length() {
    return right - left;
  }

  // string representation of this interval
  public String toString() {
    return "[" + left + ", " + right + "]";
  }



  // ascending order of left endpoint, breaking ties by right endpoint
  private static class LeftComparator implements Comparator<Interval1D> {
    public int compare(Interval1D a, Interval1D b) {
      if      (a.left  < b.left)  return -1;
      else if (a.left  > b.left)  return +1;
      else if (a.right < b.right) return -1;
      else if (a.right > b.right) return +1;
      else                        return  0;
    }
  }

  // ascending order of right endpoint, breaking ties by left endpoint
  private static class RightComparator implements Comparator<Interval1D> {
    public int compare(Interval1D a, Interval1D b) {
      if      (a.right < b.right) return -1;
      else if (a.right > b.right) return +1;
      else if (a.left  < b.left)  return -1;
      else if (a.left  > b.left)  return +1;
      else                        return  0;
    }
  }

  // ascending order of length
  private static class LengthComparator implements Comparator<Interval1D> {
    public int compare(Interval1D a, Interval1D b) {
      double alen = a.length();
      double blen = b.length();
      if      (alen < blen) return -1;
      else if (alen > blen) return +1;
      else                  return  0;
    }
  }




  // test client
  public static void main(String[] args) {
    Interval1D[] intervals = new Interval1D[4];
    intervals[0] = new Interval1D(15.0, 33.0);
    intervals[1] = new Interval1D(45.0, 60.0);
    intervals[2] = new Interval1D(20.0, 70.0);
    intervals[3] = new Interval1D(46.0, 55.0);

    StdOut.println("Unsorted");
    for (int i = 0; i < intervals.length; i++)
      StdOut.println(intervals[i]);
    StdOut.println();

    StdOut.println("Sort by left endpoint");
    Arrays.sort(intervals, Interval1D.LEFT_ENDPOINT_ORDER);
    for (int i = 0; i < intervals.length; i++)
      StdOut.println(intervals[i]);
    StdOut.println();

    StdOut.println("Sort by right endpoint");
    Arrays.sort(intervals, Interval1D.RIGHT_ENDPOINT_ORDER);
    for (int i = 0; i < intervals.length; i++)
      StdOut.println(intervals[i]);
    StdOut.println();

    StdOut.println("Sort by length");
    Arrays.sort(intervals, Interval1D.LENGTH_ORDER);
    for (int i = 0; i < intervals.length; i++)
      StdOut.println(intervals[i]);
    StdOut.println();
  }
}