Skip to content

Commit

Permalink
Merge pull request #381 from katarinaking/821
Browse files Browse the repository at this point in the history
MNEMONIC-821: Refactored Linked List Sort
  • Loading branch information
katarinaking authored Mar 21, 2024
2 parents 719c075 + cf0305a commit fd3d35e
Showing 1 changed file with 105 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,89 +21,120 @@
import java.io.BufferedWriter;
import java.io.IOException;

/**
* RegularTestFileSort implements the TextFileSort interface for sorting long values in a text file.
*/
public class RegularTestFileSort implements TextFileSort {

private Node<Long> head;
private long[] sortinfo = new long[3];

public RegularTestFileSort() {
}
private Node<Long> head; // Head node of the linked list
private long[] sortinfo = new long[3]; // Array to store sorting information: [scans, swaps, no swaps]

@Override
public void load(BufferedReader reader) throws NumberFormatException, IOException {
String text = null;
Node<Long> curnode = null;
Long val;
while ((text = reader.readLine()) != null) {
val = Long.parseLong(text);
if (null == curnode) {
curnode = new Node<Long>(val);
this.head = curnode;
} else {
curnode.setNext(new Node<Long>(val));
curnode = curnode.getNext();
}
/**
* Constructs a RegularTestFileSort object.
*/
public RegularTestFileSort() {
}
}

@Override
public void doSort() {
Node<Long> curnode, tmpnode, prevnode;
long cntscan = 0L, cntswap = 0L, cntnoswap = 0L;
boolean changed;
if (null == this.head) {
return;
}
do {
++cntscan;
curnode = this.head;
prevnode = null;
changed = false;
while (true) {
tmpnode = curnode.getNext();
if (null == tmpnode) {
break;
/**
* Loads data from a BufferedReader into the linked list.
*
* @param reader the BufferedReader to read data from
* @throws NumberFormatException if the data read is not a valid long
* @throws IOException if an I/O error occurs while reading
*/
@Override
public void load(BufferedReader reader) throws NumberFormatException, IOException {
String text;
Node<Long> currentNode = null;
Long value;
while ((text = reader.readLine()) != null) {
value = Long.parseLong(text);
if (currentNode == null) {
currentNode = new Node<>(value);
this.head = currentNode;
} else {
currentNode.setNext(new Node<>(value));
currentNode = currentNode.getNext();
}
}
if (curnode.getData().compareTo(tmpnode.getData()) > 0) {
curnode.setNext(tmpnode.getNext());
tmpnode.setNext(curnode);
if (null == prevnode) {
this.head = tmpnode;
} else {
prevnode.setNext(tmpnode);
}
prevnode = tmpnode;
changed = true;
++cntswap;
} else {
prevnode = curnode;
curnode = tmpnode;
++cntnoswap;
}

/**
* Performs bubble sort on the linked list.
*/
@Override
public void doSort() {
Node<Long> currentNode, tmpNode, prevNode;
long scanCount = 0L, swapCount = 0L, noSwapCount = 0L;
boolean changed;
if (this.head == null) {
return;
}
}
} while (changed);
this.sortinfo[0] = cntscan;
this.sortinfo[1] = cntswap;
this.sortinfo[2] = cntnoswap;
}
do {
++scanCount;
currentNode = this.head;
prevNode = null;
changed = false;
while (true) {
tmpNode = currentNode.getNext();
if (tmpNode == null) {
break;
}
if (currentNode.getData().compareTo(tmpNode.getData()) > 0) {
currentNode.setNext(tmpNode.getNext());
tmpNode.setNext(currentNode);
if (prevNode == null) {
this.head = tmpNode;
} else {
prevNode.setNext(tmpNode);
}
prevNode = tmpNode;
changed = true;
++swapCount;
} else {
prevNode = currentNode;
currentNode = tmpNode;
++noSwapCount;
}
}
} while (changed);
this.sortinfo[0] = scanCount;
this.sortinfo[1] = swapCount;
this.sortinfo[2] = noSwapCount;
}

@Override
public void store(BufferedWriter writer) throws IOException {
Node<Long> curnode = this.head;
while (null != curnode) {
writer.write(curnode.getData().toString());
writer.newLine();
curnode = curnode.getNext();
/**
* Stores sorted data from the linked list into a BufferedWriter.
*
* @param writer the BufferedWriter to write data to
* @throws IOException if an I/O error occurs while writing
*/
@Override
public void store(BufferedWriter writer) throws IOException {
Node<Long> currentNode = this.head;
while (currentNode != null) {
writer.write(currentNode.getData().toString());
writer.newLine();
currentNode = currentNode.getNext();
}
}
}

@Override
public long[] getSortInfo() {
return this.sortinfo;
}
/**
* Retrieves sorting information.
*
* @return an array containing sorting information: [scans, swaps, no swaps]
*/
@Override
public long[] getSortInfo() {
return this.sortinfo;
}

@Override
public void clear() {
this.head = null;
}
/**
* Clears the linked list.
*/
@Override
public void clear() {
this.head = null;
}
}

0 comments on commit fd3d35e

Please sign in to comment.