-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertionSorter.java
77 lines (67 loc) · 1.67 KB
/
InsertionSorter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.FileNotFoundException;
import java.lang.NumberFormatException;
import java.util.InputMismatchException;
import java.lang.IllegalArgumentException;
/**
*
* @author Stephanie Engelhardt
*
*/
/**
*
* This class implements insertion sort.
*
*/
public class InsertionSorter extends AbstractSorter {
/**
* The two constructors below invoke their corresponding superclass constructors. They
* also set the instance variables algorithm and outputFileName in the superclass.
*/
/**
* Constructor takes an array of points.
*
* @param pts
*/
public InsertionSorter(Point[] pts) {
super(pts);
super.algorithm="insertion sort";
super.outputFileName="insert.txt";
}
/**
* Constructor reads points from a file.
*
* @param inputFileName name of the input file
* @throws FileNotFoundException
* @throws InputMismatchException
*/
public InsertionSorter(String inputFileName) throws InputMismatchException, FileNotFoundException {
super(inputFileName);
super.algorithm="insertion sort";
super.outputFileName="insert.txt";
}
/**
* Perform insertion sort on the array points[] of the parent class AbstractSorter.
*
* @param order 1 by x-coordinate
* 2 by polar angle
*/
@Override
public void sort(int order){
long start=System.nanoTime();
int n=points.length;
this.setComparator(order);
if(order==1)
super.sortByAngle=false;
else
super.sortByAngle=true;
for(int i=1; i<n; i++){
Point temp= points[i];
int j=i;
for(j=i-1; j>=0 && pointComparator.compare(points[j],temp)>0; j--){
swap(j+1,j);
}
}
long end=System.nanoTime();
super.sortingTime=(end-start);
}
}