Sorting an array using two different criteria - java

I am trying to sort an array using the insertion sort algorithm. The array is filled with WordNode elements that include a word field (inputted from a text file) and a frequency field (to measure the number of times the particular word appears in the text file). I have implemented the sort so that words are sorted by frequency (from lowest to highest), but I also want to sort alphabetically if frequencies are equal. How can I sort using two different criteria at the same time? Below is my sort code.
public static void sort(ArrayUnorderedList<WordNode> array) {
//create stacks for insertion sort
LinkedStack<WordNode> sorted = new LinkedStack<WordNode>();
LinkedStack<WordNode> temp = new LinkedStack<WordNode>();
//while the array has elements to be sorted
while(!array.isEmpty()) {
//remove current element from array
WordNode currentNode = array.removeFirst();
//while the sorted stack meets sorting criteria
while((!sorted.isEmpty()) && (sorted.peek().getFrequency() < currentNode.getFrequency())) {
//push elements to temp stack
temp.push(sorted.pop());
}
//push current element to sorted stack
sorted.push(currentNode);
//while the temp stack has elements to be replaced
while(!temp.isEmpty()) {
//push elements to sorted stack
sorted.push(temp.pop());
}
}
//replace sorted elements in array
while(!sorted.isEmpty()) {
array.addToRear(sorted.pop());
}
}

AppClay's answer is absolutely correct, but if you are interested in "tidying it up", create a helper that implements Comparator.
class WordNodeComparator implements Comparator<WordNode> {
#Override
public int compare(WordNode lhs, WordNode rhs) {
int result = lhs.getFrequency() - rhs.getFrequency();
if (result == 0) {
return lhs.getWord().compareTo(rhs.getWord());
}
else {
return result;
}
}
}
Then you simply create an instance of it, and use it in your loop:
while((!sorted.isEmpty()) && (nodeComparator.compare(sorted.peek(), currentNode) < 0)
Not only does this make the code easier to read and test, it's now trivial to swap out different Comparator implementations as needed.

Update this line:
while((!sorted.isEmpty()) && (sorted.peek().getFrequency() < currentNode.getFrequency())) {
to:
while((!sorted.isEmpty()) && (sorted.peek().getFrequency() < currentNode.getFrequency() || (sorted.peek().getFrequency() == currentNode.getFrequency() && sorted.peek().getWord().compareTo(currentNode.getWord()) < 0))) {

Add this to your code:
int cmp = sorted.peek().getFrequency() - currentNode.getFrequency();
if (cmp == 0)
cmp = sorted.peek().getWord().compareTo(currentNode.getWord());
while((!sorted.isEmpty()) && cmp < 0) {
//push elements to temp stack
temp.push(sorted.pop());
cmp = sorted.peek().getFrequency() - currentNode.getFrequency();
if (cmp == 0)
cmp = sorted.peek().getWord().compareTo(currentNode.getWord());
}
I'm assuming that getFrequency() returns an integer and that the actual word in a WordNode is accessed by the getWord() method. Using the above we compare first by frequency, and if both frequencies are equal then we compare alphabetically
EDIT :
A nicer solution, define this helper method:
private static boolean compare(LinkedStack<WordNode> sorted, WordNode current) {
int cmp = sorted.peek().getFrequency() - current.getFrequency();
if (cmp == 0)
cmp = sorted.peek().getWord().compareTo(current.getWord());
return cmp;
}
And then change the first inner loop in your code to this:
while (!sorted.isEmpty() && compare(sorted, currentNode) < 0) {
//push elements to temp stack
temp.push(sorted.pop());
}

Use Guava lib:
public static List<WordNode> sort(List<WordNode> src){
List<WordNode> result = Lists.newArrayList(src);
Collections.sort(result, new Comparator<WordNode>(){
#Override public int compare(WordNode w1, WordNode w2) {
return ComparisonChain.start()
.compare(w1.frequency, w2.frequency)
.compare(w1.word , w2.word)
.result();
}});
return result;
}

Related

Implement Queue using fixed size array

I came across below interview question and I am working on it:
Build a queue class with the enqueue and dequeue methods. The queue
can store an UNLIMITED number of elements but you are limited to
using arrays that can store up to 5 elements max..
Here is what I was able to come up with. Is this the right way to do it in the interview or is there any better way we should implement in the interview?
class Solution {
private final List<List<Integer>> array;
public Solution() {
this.array = new ArrayList<>();
}
public void enqueue(int value) {
if(array.isEmpty()) {
List<Integer> arr = new ArrayList<>();
arr.add(value);
array.add(arr);
return;
}
if(array.get(array.size() - 1).size() != 5) {
array.get(array.size() - 1).add(value);
return;
}
List<Integer> arr = new ArrayList<>();
arr.add(value);
array.add(arr);
return;
}
public int dequeue() {
if(array.isEmpty()) {
return -1;
}
for(List<Integer> l : array) {
for(int i=0; i<l.size(); i++) {
return l.remove(i);
}
}
return -1;
}
}
As I mentioned in comments, your solution doesn't really solve the problem because the outer array of 5-element arrays can have more than 5 elements.
Instead, you can implement the queue as a linked list of 4-integer nodes, using the 5th element for a reference to the next array. But there's no reason to assume the elements are integers. This turns out to be pretty simple.
public class SillyQueue<T> {
private static final int MAX = 5;
private Object [] head = new Object[MAX], tail = head;
private int headPtr = 0, tailPtr = 0;
void enqueue(T x) {
if (tailPtr == MAX - 1) {
Object [] a = new Object[MAX];
tail[MAX - 1] = a;
tail = a;
tailPtr = 0;
}
tail[tailPtr++] = x;
}
T dequeue() {
if (headPtr == MAX - 1) {
head = (Object[]) head[MAX - 1];
headPtr = 0;
}
return (T) head[headPtr++];
}
}
Your answer uses ArrayList instead of true arrays, and worse, uses an unlimited arraylist to put those arrays in. I think that the interviewers expected you to implement a singly-linked list of 5-element arrays:
/**
* A singly-linked list node with an array; supports popping its 1st elements,
* and adding elements at the end, possibly by creating a new node
*/
public class ListNode {
final int MAX = 5;
private int contents[] = new int[MAX];
private int size = 0; // valid elements
private ListNode next = null;
private ListNode(ListNode next) {
this.next = next;
}
public boolean isEmpty() { return size == 0; }
public ListNode addLast(int value) {
ListNode next = this;
if (size == MAX) {
next = new ListNode(this);
}
next.contents[next.size ++] = value;
return next;
}
public int removeFirst() {
if (size == 0) {
throw new NoSuchElementException("empty queue");
}
int value = contents[0];
size --;
for (int i=1; i<size; i++) contents[i-1] = contents[i];
return value;
}
}
/**
* A simple queue on top of nodes that keep arrays of elements
*/
public class ListArrayQueue {
ListNode first = new ListNode();
ListNode last = first;
public void enqueue(int value) {
last = last.addLast(value);
}
public int dequeue() {
if (first.isEmpty() && first != last) {
first = first.next;
}
return first.removeFirst();
}
}
Performance-wise, this can be improved: you can avoid keeping the size in each ListNode, since only the 1st and last nodes can be non-full. You can also avoid the loop in removeFirst, but that would entail replacing size by firstIndex and lastIndex; which could again be moved into the ListArrayQueue to save space in each node.
If they has asked you to build an unlimited array out of 5-element array pieces, you would have had to implement something similar to a b-tree. Which, without handy references, would be quite hard to pull off during an interview.
You can use a 1-D array and use Wrap-around indexing to implement the queue with the limitation that queue can contain maximum 5 elements.
For checking the condition of empty queue, maintain a variable that counts the number of elements present in the queue.
Is this the right way to do it in the interview…?
Presenting uncommented code is never right, let alone in an interview.
In an interactive interview, it would be your task to find out whether you can/should use an unlimited number of arrays.
If not, you have to negotiate a way to handle an enqueue() to a queue filled to capacity in addition to a dequeue() to an empty one.
Fix the type of items the queue can hold.
Agree upon the parameters of the enqueue and dequeue methods.
The task is to Build a queue class, Solution is a bad choice for a name - array for something to access the items is no better.
In a language providing arrays, I'd take limited to using arrays literally - if using something more, why not an implementation of java.util.Queue?
The empty queue handling is entirely redundant: in enqueue(), you could have used
if (!array.isEmpty() && array.get(array.size() - 1).size() < 5); in dequeue() you can just drop it.
Instantiating List<Integer>s, you know there won't be more than five items at a time: tell the constructor.
dequeue() leaves empty List<Integer>s in arrays, giving rise to the current nested loop that desperately needs a comment.
(For the second part of the question, I second Rajkamal Tomar.)
Trade Off - Organize Fixed Size Arrays
Manage arrays based on ArrayList
enqueue - append new fixed size array - O(1)
dequeue - remove first fixed size array - O(n)
Manage arrays based on LinkedList
enqueue - append new fixed size array to linked list - O(1)
dequeue - remove first fixed size array - O(1)
cons - extra next pointer to setup linked list
public class FixedArrayQueue<T> {
private Node<T> head, tail;
private int front, rear, size;
private final int SIZE;
public FixedArrayQueue(int n) {
SIZE = n;
head = tail = new Node<T>(SIZE);
front = rear = size = 0;
}
public void enqueue(T t) {
tail.array[rear++] = t;
if (rear == SIZE) {
rear = 0;
append();
}
size++;
}
public T dequeue() {
if (size == 0) {
throw new EmptyQueueException();
}
T ret = head.array[front++];
if (front == SIZE) {
front = 0;
remove();
}
size--;
return ret;
}
private void append() {
tail.next = new Node<T>(SIZE);
tail = tail.next;
}
private void remove() {
head = head.next;
}
private boolean isEmpty() {
return size == 0;
}
private int size() {
return size;
}
}
class Node<T> {
T[] array;
Node<T> next;
public Node(int n) {
array = (T[]) new Object[n];
}
}

Trying to compare the contents two Iterators, how?

EDIT: With your help I managed to fix my problem. I have edited my code to now show how I had to have it set up to get it working.
Currently I am having trouble coding a part which compares the content of two iterators. As part of the requirements for my assignment, I need to use a linkedlist to store the individual characters of the entered String. I have gotten to the point where I have two iterators which would contain the input one way and the reverse way.
String palindrom = input.getText();
String [] chara = palindrom.split (""); //this is successfully splitting them, tested.
int length = palindrom.length( ); // length == 8
System.out.println (length); //can use this for how many checks to do?
LinkedList ll = new LinkedList(Arrays.asList(chara));
Iterator iterator = ll.iterator();
Iterator desIterator = ll.descendingIterator();
/*while(iterator.hasNext() ){
System.out.println(iterator.next() );
}
while(desIterator.hasNext() ){
System.out.println(desIterator.next() );
}*/
boolean same = true;
while(iterator.hasNext()){
if(!iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
And using the System.out I can see that they are being stored correctly, but I don't know how to check if the iterators store the same contents. What would be one of the simplest methods to compare the two iterators or convert them into something I can compare? To clarify I want to verify they contain the same elements in the same order.
boolean same = true;
while(iterator.hasNext()){
if(!desIterator.hasNext() || !iterator.next().equals(desIterator.next())){
same = false;
break;
}
}
System.out.println(same);
You need to iterate over both iterators simultaneously, i.e. with one loop. Here is a general comparison function (0 when equal, < 0 when A < B, > 0 when A > B):
static <T extends Comparable<S>, S> int compare(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
int comparison = a.next().compareTo(b.next());
if (comparison != 0) {
return comparison;
}
}
if (a.hasNext())
return 1;
if (b.hasNext())
return -1;
return 0;
}
To just check if they are equal, this can be simplified:
static <T, S> boolean equals(Iterator<T> a, Iterator<S> b) {
while (a.hasNext() && b.hasNext()) {
if (!a.next().equals(b.next())) {
return false;
}
}
if (a.hasNext() || b.hasNext()) {
// one of the iterators has more elements than the other
return false;
}
return true;
}
Guava implements this as Iterators.elementsEqual.
In both answers throw NullPointerException, if iterator.next() == null. This method is more optimal.
public static boolean equals(Iterator i1, Iterator i2) {
if (i1 == i2) {
return true;
}
while (i1.hasNext()) {
if (!i2.hasNext()) {
return false;
}
if (!Objects.equals(i1.next(), i2.next())) {
return false;
}
}
if (i2.hasNext()) {
return false;
}
return true;
}

Verify if an object exist in a List

What I need is: Verify if an object exist in a List comparing some attributes.
I'm in a trouble here with Collections and Comparator. I'm trying to do the verify with this Binary Search:
Collections.binarySearch(listFuncionarioObs2, formFuncionarioObsIns, formFuncionarioObsIns.objectComparator);//Binary search of an object in a List of this Object.
With this comparator:
public int compare(FuncionarioObs func, FuncionarioObs funcToCompare) {
int testCodigo = -1;
if(null != func2.getCodigo()){
testCodigo = func.getCodigo().compareTo(funcToCompare.getCodigo());
}
int testData = func.getData().compareTo(funcToCompare.getData());
int testEvento = func.getEvento().compareTo(funcToCompare.getEvento());
int testAndamento = func.getAndamento().compareTo(funcToCompare.getAndamento());
if(testCodigo == 0 && testData == 0 && testEvento == 0 && testAndamento == 0){
return 0;
}else if(testData == 0 && testEvento == 0 && testAndamento == 0) {
return 0;
}
return -1;
}
But I'm a little bit lost, this is not working and I don't know the best way to do this. Someone can turn on a light for me?
Best regards,
Edited.
I'm sorting the List before the Binary Search with this code:
List<FuncionarioObs> listFuncionarioObsBD = funcionarioObsDAO.getFuncionarioObsById(sigla);
Collections.sort(listFuncionarioObsBD);
The comparator to the sort is:
#Override
public int compareTo(FuncionarioObs func) {
if(this.getCodigo() > func.getCodigo()){
return 1;
}else if(this.getCodigo() == func.getCodigo() ) {
return 0;
}else{
return -1;
}
}
CompareTo
Your compare wont work correctly. Right now it is only comparing the references of the objects. You will have to change this to compare the objects values:
#Override public int compareTo(Account aThat) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;
//this optimization is usually worthwhile, and can
//always be added
if (this == aThat) return EQUAL;
//primitive numbers follow this form
if (this.fAccountNumber < aThat.fAccountNumber) return BEFORE;
if (this.fAccountNumber > aThat.fAccountNumber) return AFTER;
//booleans follow this form
if (!this.fIsNewAccount && aThat.fIsNewAccount) return BEFORE;
if (this.fIsNewAccount && !aThat.fIsNewAccount) return AFTER;
.
.
.
//all comparisons have yielded equality
//verify that compareTo is consistent with equals (optional)
assert this.equals(aThat) : "compareTo inconsistent with equals.";
return EQUAL;
}
from here
Finding the object
Now comes the next part. As CrtlAltDelete has hinted it dependends of whether your list is sorted or not.
If its sorted ascending: iterate through the objects till you either find one which compareTo returns a Zero (== success) or a One ( == fail).
For an unsorted list you will have to iterate through all objects in search for one that returns a Zero.

How would I make my custom generic type linked list in Java sorted?

I am writing my own linked list in java that is of generic type instead of using the java collections linked list. The add method for the linked list is made up of the following code:
public void add(T item, int position) {
Node<T> addThis = new Node<T>(item);
Node<T> prev = head;
int i;
if(position <= 0) {
System.out.println("Error: Cannot add element before position 1.");
}
else if(position == 1) {
addThis.setNext(head);
head = addThis;
} else {
for(i = 1; i < position-1; i++) {
prev = prev.getNext();
if(prev == null) {
System.out.println("Cannot add beyond end of list");
}
} // end for
addThis.setNext(prev.getNext());
prev.setNext(addThis);
}
} // end add
How would I make it so that when I add a new item, the item is compared to another item and is inserted alphabetically? I have looked into using compareTo but I cannot figure out how to do it.
Thanks
EDIT:
I have various classes: I have a class called Dvd which has methods and variables for a title(string) and number of copies of that title(int). I also have a linked list class, a listinterface, a node class, and a main class.
Does your implementation extend the java.util.List interface?
Can you simply add the object to the list, then sort the list using Collections.sort()?
You mention using generics but then mention sorting them alphabetically. Generics are not necessarily character strings, they are used to represent any type, while a sort property like alphabetically implies alphabetic characters. My Answer assumes you are expecting generic objects of type T which have an alphabetic nature to them. In my example I exclusively use a String
You can set you code up to search for the position to add itself instead of providing it.
public void add(T item) {
Node<T> addThis = new Node<T>(item);
Node<T> itr = head;
while (itr.hasNext()) {
if (addThis.compareTo(itr.getNext()) <= 0) { // itr > addThis
addThis.setNext(itr.getNext());
itr.setNext(addThis);
return;
}
itr = itr.getNext();
}
addThis.setNext(null);
itr.setNext(addThis);
return;
} // end add
Then in your Node class, you can implement the Interface Comparable . I'm assuming you store a string since you asked about alphabetizing. This Question Explains comparing strings alphabetically.
class Node implements Comparable<Node> {
String value; // ASSUMING YOU ARE USING A STRING AS YOUR GENERIC TYPE T
#Override
public int compareTo(Node otherNode) {
int i;
String thisString = this.getValue();
String otherString = otherNode.getValue();
int minSize = ( otherString.length() > thisString.length() ? thisString.length() : otherString.length() );
for (i = 0; i < minSize; i++) {
if (thisString.charAt(i) > otherString.charAt(i)) {
return 1;
} else if (thisString.charAt(i) < otherString.charAt(i)) {
return -1;
}
}
if (otherString.length() > thisString.length()) {
return 1;
} else if (otherString.length() < thisString.length()) {
return -1;
} else {
return 0;
}
}
// OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
}
In order to do this with simply generics, you would need to implement you Node class with the type T implementing Comparable like so:
class NodeNode<T extends Comparable<T>> implements Comparable {
T value;
#Override
public int compareTo(Node otherNode) {
return this.getValue().compareTo(otherNode.getValue());
}
// OTHER CLASS CONSTRUCTORS, VARIABLES, AND METHODS
}
I finally figured it out by using an insertion sort:
public void add(Dvd item) {
DvdNode addThis = new DvdNode(item);
if(head == null) {
head = addThis;
} else if(item.getTitle().compareToIgnoreCase(head.getItem().getTitle()) < 0) {
addThis.setNext(head);
head = addThis;
} else {
DvdNode temp;
DvdNode prev;
temp = head.getNext();
prev = head;
while(prev.getNext() != null && item.getTitle().compareToIgnoreCase
(prev.getNext().getItem().getTitle()) > 0) {
prev = temp;
temp = temp.getNext();
}
addThis.setNext(temp);
prev.setNext(addThis);
}
}

Improving the performance of merging two ArrayLists

I am merging two ArrayLists with the following code. The code is working and giving me the desired result, but I want a more efficient version. Here are the conditions.
Method accepts two lists, and both list have elements in decreasing order (5,4,3,2)
Method accepts an integer to decide the size of the resulting ArrayList.
First input list size is never greater than the size of the resulting ArrayList.
Code:
public ArrayList<Integer> mergeList(ArrayList<Integer> first,ArrayList<Integer> second, int n){
//case 1: when both list are null.
if(first == null && second == null )
return null;
//case 2: when first list is null but second list have elements
else if( first == null && second != null){
return second.size() >=n ? new ArrayList<Integer>(second.subList(0, n)) : second;
}
//case 3: when first list have record and second list is null
else if(first != null && second == null){
return first;
}
//case 4: when both list have elements
else {
first.addAll(second);
Collections.sort(first);
Collections.reverse(first);
return first.size()>=n ? new ArrayList<Integer>(first.subList(0, n)) : first;
}
}
}
It depends on what you mean by "More efficient".
In terms of what? Memory, CPU, readability?
Based on your code above, I'm making the following assumptions:
Readability is more important than pure performance/memory consumption without any profiling measurements/requirements "The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." — Michael A. Jackson
Prefer the null object pattern over returning null
Duplicate elements are desirable/required
Use a Comparator to perform a reverse
sort
private List<Integer> mergeList(List<Integer> list1, List<Integer> list2, final int newSize) {
// Enforce null object pattern
if (list1 == null) {
list1 = Collections.emptyList();
}
if (list2 == null) {
list2 = Collections.emptyList();
}
// If duplicates are not desirable, a TreeSet would perform automatic sorting.
List<Integer> result = new ArrayList<Integer>(list1);
result.addAll(list2);
Comparator<Integer> reverseSortComparator = new Comparator<Integer>() {
#Override
public int compare(final Integer o1, final Integer o2) {
return o2.compareTo(o1);
}
};
Collections.sort(result, reverseSortComparator);
if (result.size() > newSize) {
return result.subList(0, newSize);
} else {
return result;
}
}
It looks like you are trying to preserve the contents of first and second. If you are not, then this will do just fine for you and will make your code both faster and more readable:
public ArrayList<Integer> mergeList(ArrayList<Integer> first,ArrayList<Integer> second, int maxLength){
//case 1: when both list are null.
if(first == null && second == null )
return null;
//case 2: when first list is null but second list have elements
else if( first == null && second != null){
return second;
}
//case 3: when first list have record and second list is null
else if(first != null && second == null){
return first;
}
//case 4: when both list have elements
else if(first != null && second != null){
first.addAll(second);
Collections.sort(first); //want to merge these two line into one
Collections.reverse(first);
}
return (ArrayList) first.size() > maxLength ? first.subList(0, n) : first;
}
The reason why this is faster is because with each addAll(), Java must iterate through all the items, copying them into tempList. I preserved the Collections.reverse call because it seems like you need to have your data in reverse sorted order.

Categories