-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsort.java
115 lines (101 loc) · 3.7 KB
/
Isort.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
import java.io.*;
public class Isort {
private ListNode head = null;
private long numberComparisons = 0;
public Isort() {
// Reads in values from System.in
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
add(input.nextInt());
}
long start = System.nanoTime();
sort();
long end = System.nanoTime();
long sortTimeInNano = end - start;
double sortTimeIn10thSeconds = (double) sortTimeInNano / Math.pow(10, 8);
System.err.println("Time after sorting list in 10th of second: " + sortTimeIn10thSeconds);
System.err.println("Number of Comparisons: " + numberComparisons);
// Printing out values of list to System.out
while (head != null) {
System.out.println(head.data);
head = head.next;
}
}
public static void main(String[] args) {
new Isort();
}
/*
* The idea of insertion sort is that it will go through each element in the
* list. At each element, it will then look at each element backwards to see if
* it is less. It will keep moving back for each element it is less than and
* then be inserted at the final position where it will shift all the other
* elements down. This algorithm is normally time consuming if you are using an
* array and need to shift each element down the array to insert, for this
* situation we used a linkedlist so all we had to do was unlink the pointers
*/
public void sort() {
ListNode start = head;
ListNode position1;
ListNode position2;
// Going through each element of the array from the second element to the end
while (start.next != null) {
start = start.next;
position1 = start;
position2 = position1.previous;
// Checks if previous is null and keeps swapping elements backwards till there
// is an element that is bigger than the original element
while (position2 != null && (position1.data < position2.data)) {
swap(position1, position2);
numberComparisons++;
position1 = position2;
position2 = position1.previous;
}
}
}
// Adds a number to the front of the list
public void add(int num) {
ListNode newNode = new ListNode(num);
if (head == null) {
head = newNode;
} else {
head.previous = newNode;
newNode.next = head;
head = newNode;
}
}
// Function that would swap the values of two nodes
public void swap(ListNode position1, ListNode position2) {
int temp = position1.data;
position1.data = position2.data;
position2.data = temp;
}
// Helper function that would print out the list
public void printList() {
ListNode currentNode = head;
while (currentNode != null) {
// Print the data at current node
System.out.print(currentNode.data + " ");
// Go to next node
currentNode = currentNode.next;
}
System.out.println();
}
private class ListNode {
/** public field that holds the data of the linked list */
public int data;
public ListNode next;
public ListNode previous;
/**
* Constructor method that takes in one parameter that creates a node with
* information but the previous and next links are null
*
*
*/
public ListNode(int data) {
this.data = data;
this.next = null;
this.previous = null;
}
}
}