Java,Selection Sort, Insertion Sort, Merge Sort, Quick Sort。
Project Overview
In computational geometry, algorithms often build their efficiency on processing geometric objects in certain orders that are generated via sorting. For example, Graham’s scan constructs the convex hull of an input set of points in the plane in one traversal ordered by polar angle; intersection of a large number of line segments is performed using a sweeping line which makes stops at their endpoints as ordered by x-coordinate or y-coordinate.
In this project, you are asked to sort an input set of points in the plane using four sorting algorithms (already or to be) presented in class: selection sort, insertion sort, mergesort, and quicksort. Point comparison is based on either the x-coordinate or the polar angle with respect to some reference point. Your code should provide both options for comparison.
We make the following two assumptions:
a) All input points have integral coordinates ranging between -50 and 50 inclusive.
b) The input points may have duplicates.
Integral coordinates are assumed to avoid issues with floating-point arithmetic. The rectangular range [-50, 50] × [-50, 50] is big enough to contain 10,201 points with integral coordinates. Since the input points will be either generated as pseudo-random points or read from an input file, duplicates may appear.
Point Class and Comparison Methods
The Point class implements the Comparable interface. Its compareTo() method compares the x-coordinates of two points. If the two points have the same x-coordinate, then their y-coordinates are compared.
Point comparison can be also done using an object of the PolarAngleComparator class, which you are required to implement. The polar angle is with respect to a point stored in the instance variable referencePoint. The compare() method in this class must be implemented using cross and dot products not any trigonometric or square root functions. You need to handlespecial situations where multiple points are equal to lowestPoint, have the same polar angle with respect to it, etc. Please read the Javadoc for the compare() method carefully.
Sorter Classes
In this project, selection sort, insertion sort, mergesort, and quicksort are respectively implemented by the classes SelectionSorter, InsertionSorter, MergeSorter, and QuickSorter, all of which extend the abstract class AbstractSorter. There are two constructors of this abstract class that await your implementation:
1 2
|
protected AbstractSorter(Point[] pts) throws IllegalArgumentException protected AbstractSorter(String inputFileName) throws FileNotFoundException, InputMismatchException
|
The first constructor takes an existing array pts[] of points, and copy it over to the array points[]. It throws an IllegalArgumentException if pts == null or pts.length == 0.
The second constructor reads points from an input file of integers and stores them in points[]. Every pair of integers represents the x and y-coordinates of some point. A FileNotFoundException will be thrown if no file by the inputFileName exists, and an InputMismathException will be thrown if the file consists of an odd number of integers. (There is no need to check if the input file contains unneeded characters like letters since they can be taken care of by the hasNextInt() and nextInt() methods of a Scanner object.)
Each of the four subclasses SelectionSorter, InsertionSorter, MergeSorter, and QuickSorter has two constructors that need to call their corresponding superclass constructors above.
For example, suppose a file points.txt has the following content:
0 0 -3 -9 0 -10 8 4 3 3 -6 3 -2 1 10 5 -7 -10 5 -2 7 3 10 5 -7 -10 0 8 -1 -6 -10 0 5 5
There are 34 integers in the file. A call AbstractSort(“points.txt”) will initialize the array points[] to store 17 points below (aligned with five points per row just for display clarity here):
(0, 0) (-3, -9) (0, -10) (8, 4) (3, 3) (-6, 3) (-2, 1) (10, 5) (-7, -10) (5, -2) (7, 3) (10, 5) (-7, -10) (0, 8) (-1, -6) (-10, 0) (5, 5)
Note that the points (-7, -10) and (10, 5) each appear twice in the input, and thus their second appearances are duplicates. The 15 distinct points are plotted nicely in Fig. 1 by Mathematica. (In this project, you are provided an implemented class Plot using the Java Swing to plot sorting results.)
Besides having an array points[] to store points, the AbstractSorter class also includes six instance variables.
- algorithm: type of sorting algorithm. Initialized by a subclass constructor.
- sortByAngle: sorting by polar angle or x-coordinate. Set within sort().
- outputFileName: name of the file to store the sorting result in: select.txt, insert.txt, merge.txt, or quick.txt. Set by a subclass constructor.
- sortingTime: sorting time in nanoseconds. It can be set, for instance, within sort() using the System.nanoTime() method.
- pointComparator: comparator used for point comparison. Set by calling setComparator() within sort().
- lowestPoint: lowest point in the array points[]. Initialized by a constructor of AbstractSorter.
In the previous example, two points (-7, -10) and (0, -10) tie for the lowest point. The variable lowestPoint is set to the first point because it is to the left. After sorting by increasing x-coordinate, the array points[] will store the 17 points in the following order:
(-10, 0) (-7, -10) (-7, -10) (-6, 3) (-3, -9) (-2, 1) (-1, -6) (0, -10) (0, 0) (0, 8) (3, 3) (5, -2) (5, 5) (7, 3) (8, 4) (10, 5) (10, 5)
The lowestPoint is at index 0. Also, note that the three points (0, -10), (0, 0), (0, 8) have the same x-coordinate 0. Their order is determined by their y-coordinates. The same applies to (5, -2) and (5, 5).
After sorting by increasing polar angle, the same array will store the points in a different order below:
(-7, -10) (-7, -10) (0, -10) (-3, -9) (-1, -6) (5, -2) (10, 5) (10, 5) (7, 3) (8, 4) (5, 5) (3, 3) (0, 0) (-2, 1) (0, 8) (-6, 3) (-10, 0)
Among them, (-1, -6) and (5, -2) have the same polar angle with respect to (-7, -10). They are thus ordered by distance to this point.
Compare Sorting Algorithms
The class CompareSorters executes the four sorting algorithms on points randoml††⁴††⁴㰠⽴㹥൳㰠㍬㵩≴䑨䑵⵰≅㹡䑣䐠㱬⽯㍳㸺഼ਯ㱰㸍†⁃⁹††⡢⤩†䅨ⱳ†⠬⤠⁉Ɐ†㩵㱩⽣㹓൯ੲ㱴㹲㰬㵢≪≳㹳ഠ੦㱲㹥ഠ⁶㰠㸠൯ੲ††⁰⁵㱴㹩൬‼ ‾ ††††㰼‾㵶≥≯㹵൲ਠ⁴†㱣㹣㱡㵴∨≴㹨ㅯ㱤⼠㹳㱴⼠㹴㱨⽥㹲ੳ⁵⁴††⁰㱴⽳㸮഼ਯ‾ ‼ ⁵‾ 㰼⁂㵩≳≡㹭൰੬†⁸⁵⁴†㱳㹥㱮㵮≮≴㹩⸠䙥⠠Ⱙ†Ɐ†䍡⡨⥥⸠⠠⥥⥡㭣㱨⼠㹩㱮⽬㹧㱯⽲㹭ഠ੩†⁴†⁴㰼⼯㸍ഊ਼⁰‾⁙⁵†㱯⽤㹮†⁴㱯⼠㸠൯ੵ㱴⼠㹡൭㰠⽴㹳㱳⽡㹥൳ਠ㱦㹲☠㬠㱩⽮㹥൲㱣㹩⁅††⁴†Ⱨ⸠䉤•Ⱪ⁴⸊‼⁴䍵⡤⥥⸬⡹⤠†䙵‰※‵†ⱹⱳ†⁰⁹†⁵⁴⁛ⱡ†⁴䉡䅫䄮⹎㱥⽸㸬ഠ㱥㍬㴠≡䑮ⵡ≢㹪䑥 㰾⼼㍩㹧൵ੲ㱥㹣⡡⥶†‾†ⴠ⸊††ㄠ㐠†‽⁴††㐾⸼ㅳ⁰⸾‱⁰☠㭲‾†‼††††‼⡳⵰ㅡのⰠぬ⥡‾††⡮ㄠっⱬ㕳⥳⸽•⁷ⰼ †䙤⸨
㌻‼⠯‼†䴯†††‼ ⤠ⰼ †⁴ ⁴⁵‾‼⸼㱰⼾㹨੮㰬㹴䥨†⁰‼ⰽ•⁴‼†⁴‾† ††㱴⽥㸢ാ㰊㸠††•⁰ ††††⠠ⴠ㜠Ⱐ†ⴠㄠ〠⤠⸼⁴ †㈠㜠†⁰㐽⸢ㅬ⁸⁴☠㬽‱…†‰‾⁰ 䵰††䘠⸼ 㑴㰾⼍㸠ഠਠ㰠㸠 †⁴‼⁰…⁰‾‵⁶⡭ⵥ㜠Ⱪ⁴ⴠㅩび⤠⁴⁰⸠⁒′‼⁔††⡧ⵥ㜠ⱓ⁷ㅮで⤮†††䥴ⱔ⁹Ᵽ⁴⁰†ⱴ††⁰⁴⸼㰯⽰㸍
-
The JFrame class is a top level container that embodies the concept of a “window”. It allows you to create an actual window with customized attributes like size, font, color, etc. It can display one or more JPanel objects in the same time.
-
JPanel is for painting and drawing, and must be added to the JFrame to create the display. A JPanel represents some area in a JFrame in which controls such as buttons and textfields and visuals such as figures, pictures, and text can appear.
-
The Graphics class may be thought of like a pen that does the actual drawing. The class is abstract and often used to specify a parameter of some method (in particular, paint()). This parameter is then downcast to a subclass such as Graphics2D for calling the latter’s utility methods.
-
The paint() method is called automatically when a window is created. It must be overridden to display your drawings.
The results of the four sorters are displayed in separate windows. For display, a separate thread is created inside the method myFrame() in the class Plot.
Please do not modify the Plot class for better display unless you understand what is going on there. (Anyway, the quality of display will never match that created by software like Mathematica or Matlab.)
The class Segments has been implemented for creati