Errors implementing quicksort - java

My code is below. It is a sorting method included in a project instead of bubblesort for efficiency of code. My problem is that I keep getting errors which are:
array required, but java.util.List<Inpatient> found
&
QuickSort(java.util.List<Inpatient>,int,int) in UtilitiesInpatient cannot be applied to (int,int)
I've tried doing some research but a lot of algorithms vary a lot depending on string or integer sorting and also, research on the errors themselves were highly unhelpful. Many thanks for any help or tips!
public void QuickSort (List<Inpatient> inpatientArrayListIn, int first, int last)
{
// Quick Sort
List<Inpatient> pivotValue = new ArrayList<Inpatient>();
List<Inpatient> lowerPointerValue = new ArrayList<Inpatient>();
List<Inpatient> upperPointerValue = new ArrayList<Inpatient>();
int pivotIndex = first;
Inpatient tempPatient = (inpatientArrayListIn.get(pivotIndex));
String pivot = tempPatient.getSurname();
int upperPointer = first;
int lowerPointer = last;
while (upperPointer < lowerPointer) {
while ((inpatientArrayListIn.get(upperPointer).getSurname().compareToIgnoreCase(pivot) <= 0) && (upperPointer < last)) {
upperPointer++;
}
while (((inpatientArrayListIn.get(lowerPointer).getSurname()).compareToIgnoreCase(pivot) > 0) && (lowerPointer > first)){
lowerPointer--;
}
if (upperPointer < lowerPointer) {
for (int i = 0; i <= inpatientArrayListIn.size(); i++) {
upperPointerValue[i] = ((inpatientArrayListIn.get(upperPointer)));
lowerPointerValue[i] = ((inpatientArrayListIn.get(lowerPointer)));
}
/* defaultTable.removeRow (upperPointer);
defaultTable.insertRow (upperPointer, lowerPointerValue);
defaultTable.removeRow (lowerPointer);
defaultTable.insertRow (lowerPointer, upperPointerValue);
*/
++upperPointer;
--lowerPointer;
}
}
if ((inpatientArrayListIn.get(lowerPointer).getSurname()).compareTo(pivot) < 0) {
for (int i = 0; i <= inpatientArrayListIn.size(); i++) {
pivotValue[i] = inpatientArrayListIn.get(pivotIndex);
lowerPointerValue[i] = (inpatientArrayListIn.get(lowerPointer));
}
/*
defaultTable.removeRow (pivotIndex);
defaultTable.insertRow (pivotIndex, lowerPointerValue);
defaultTable.removeRow (lowerPointer);
defaultTable.insertRow (lowerPointer, pivotValue);
*/
}
// Value in lowerPointer is now the pivot
if (first < (lowerPointer-1))
{
QuickSort (first, (lowerPointer-1));
}
if ((lowerPointer+1) < last)
{
QuickSort ((lowerPointer+1), last);
}
}

In this portion:
if (first < (lowerPointer-1))
{
QuickSort (first, (lowerPointer-1));
}
if ((lowerPointer+1) < last)
{
QuickSort ((lowerPointer+1), last);
}
You are missing the first argument, List<Inpatient> inpatientArrayListIn, exactly as the error message describes. Also, you are trying to use List as an array, as in:
upperPointerValue[i] = ((inpatientArrayListIn.get(upperPointer)));
If we look at the documentation for List, the method you want is set(int index, E element), so the above would be:
upperPointerValue.set(i, inpatientArrayListIn.get(upperPointer));
There really is not much more one can say about the problems you are facing that the compiler isn't already telling you.

Related

Incorrect output for Project Euler #14 output

I'm trying to implement the algorithm to solve Project Euler Problem #14, which asks to find a number in a given range that outputs the largest Collatz conjecture sequence length. My code is below:
import java.util.ArrayList;
class Collatz {
private static ArrayList<ArrayList<Long>> previousNums = new ArrayList();
public static int seqLen(int x) {
ArrayList<Long> colSeq = new ArrayList();
long val = x;
colSeq.add(val);
while (val > 1) {
if (val%2 == 0) {
val/=2;
if (val < previousNums.size()) /*used to check if index exists*/{
colSeq.addAll(previousNums.get((int)val));
break;
}
else colSeq.add(val);
}
else {
val = 3*val + 1;
if (val < previousNums.size()) {
colSeq.addAll(previousNums.get((int)val));
break;
}
else colSeq.add(val);
}
}
previousNums.add(colSeq);
return colSeq.size();
}
public static void main(String[] args) {
int greatestNum = 0;
long totalVal = 0;
for (int i = 0; i<=1000000; i++) {
int collatz = seqLen(i);
if (collatz > totalVal) {}
greatestNum = i;
totalVal = collatz;
}
System.out.println(greatestNum + " " + totalVal);
}
}
The output I get is
1000000 153
While this is not the correct answer, 153 is the correct sequence length for 1 million. Based off of this, I could assume that my Collatz conjecture algorithm works, but not the comparison part. However, I can't really find anywhere else I could modify the code. Any ideas? Thank you and please pardon the possibility of this being a duplicate (not many other posts had the same problem).
Wow, a mere syntax error was the issue. Looks like I didn't pay attention to:
if (collatz > totalVal) {}
greatestNum = i;
totalVal = collatz;
Yup, didn't enclose the code with the braces.

Adding an object to a sorted array into the correct spot

So I have this project and im writing the add method for my catalog class and this add method needs to add an item to a sorted array into the right place using insertion sort, unless the array has nothing in it in that case i just want to add it in normally. this whole project must use an array I cannot use an arraylist or anything else.
The problem I am having here is that the way my program currently is, its only adding one object to my array and each time i try to add a new one during run tine it jst replaces the item already in there. I know that my problem is something in the body of my while loop and the way i initialize my position variable.
here is the method im having trouble with.
public void addItem(Item theItem)
{
int position = size;
if(size != 0){
while (position > 0 && theItem.compareTo(items[position - 1]) < 0){
items[position] = items[position - 1];
position--;
}
items[position] = theItem;
}
else{
items[size] = theItem;
size++;
}
here is my compareTo method
public int compareTo(Item other){
if(this.getItemType().equals(other.getItemType())){
return this.itemnum - other.itemnum;
}
//item types are not equal
else
return this.getItemType().compareTo(other.getItemType());
//try writing code to compare by price as well
}
The most likely problem in your code is this line:
items[position-1] = items[position];
This will copy an item in you array from the current position to the position to the left of it.
When you insert a new item you want to copy items from the left to the current position to make room for the new item to the left.
Change it to
items[position] = items[position-1];
A size++ is also missing after the while block, inside the first if block.
I realized this when adding a second call to addItem in my test code below.
You could also put a single size++ statement outside of the if statement.
A Complete, Minimal, Reproducible Example that I used trying to fix it. I have used Integer instead of Item to avoid having to add more classes.
public class Main {
private int size = 0;
private Integer[] items = new Integer[20];
public static void main(String... args) {
new Main().execute(); // Moving us into a non-static context
}
public void execute() {
System.arraycopy(new Integer[] {1,2,3,4,6,7,8,9}, 0, items, 0, 8);
size = 8;
// items = [1,2,3,4,6,7,8,9,null,null,...]
addItem(5);
addItem(5); // test adding a second item
// items = [1,2,3,4,5,6,7,8,9,null,null,...]
for (Integer i : items) {
System.out.println(i);
}
}
public void addItem(Integer item) {
int position = size;
if (size != 0) {
while (position > 0 && item.compareTo(items[position - 1]) < 0) {
// items[position-1] = items[position]; // Result [1,2,3,4,5,null,null,...]
items[position] = items[position-1]; // Result [1,2,3,4,5,6,7,8,9,null,null,...]
position--;
}
items[position] = item;
size++; // this line was missing as well
} else {
items[size] = item;
size++;
}
// or a single size++; here, removing the other two
}
}
The ugly solution by making new array
public int[] addItem(int item, int[] items){
int[] tempArr = new int[items.length + 1];
boolean hasAlready = false;
for(int i = 0 ; i < items.length; i++){
if(hasAlready)tempArr[i + 1] = items[i];
else if(item < items[i]){
tempArr[i] = item;
tempArr[i + 1] = items[i];
hasAlready = true;
}else {
tempArr[i] = items[i];
}
}
//items = tempArr; if items is global variable
return tempArr;
}
One can use existing utility functions, Arrays.binarySearch, and System.arraycopy. Your loop was 1 off.
public void addItem(Item theItem) {
Comparator<Item> comparator = Comparator.comparing(Item::getItemType)
.thenComparingInt(it -> it.itemnum);
int position = Arrays.binarySearch(items, 0, size, theItem, comparator);
// If position >= 0 the item was found (maybe no need to insert?)
if (position < 0) {
position = ~position; // Insert position of not found item
}
System.arraycopy(items, position, items, position + 1, size - position);
items[position] = theItem;
size++;
}
Binary search results in the non-negative index when found, or the negative ~index when not found. Here binary search is done on a subarray from 0 upto size (excluded).
Same as Roger Gustavsson
public class Main {
private int size = 0;
private Integer[] items = new Integer[20];
public static void main(String... args) {
new Main().execute(); // Moving us into a non-static context
}
public void execute() {
System.arraycopy(new Integer[] {1,2,3,4,6,7,8,9}, 0, items, 0, 8);
size = 8;
// items = [1,2,3,4,6,7,8,9,null,null,...]
addItem(5);
// items = [1,2,3,4,5,6,7,8,9,null,null,...]
for (Integer i : items) {
System.out.println(i);
}
}
public void addItem(Integer item) {
if (size == 0) {
items[size] = item;
size++;
return;
}
int position = size;
while (position > 0 && item.compareTo(items[position - 1]) < 0) {
items[position] = items[position - 1];
position--;
}
items[position] = item;
size++;
}
}
on what you are trying to achieve, i think next solution will be starting point from where you can build your own solution depending your specific needs. i have Changed your main method a little bit, and i do not know if your classes implements comparable /Comparator or not.
public void addItem(Item theItem) {
int position = position(items, theItem); // position is a method that finds best position for inseriton
if (items[position] == null){ // if items at best position is null then add new element there
items[position] = theItem;
} else{
items[size] = theItem; // if not add element at last position
swapUp(size); // and swap them up to perfect position.
}
size++;
}
method that find best position looks like this.
private static int position(Item[] items, Item newItem) {
if (isEmpty(items))
return 0;
int pos=0;
int target=items.length-1;
while(pos < target){
int m = pos+(target-pos)/2;
if (items[m] !=null){
if(newItem.getNumber()>items[m].getNumber()){ // comparing depending on item number
pos=m+1;
}else{
target=m;
}
}else{
target = m;
}
}
return pos;
}
as you can see method is looking for position depending on item number, you can change this with your type, or do both type and number comparison. Swaping up is handled by thus 2 method.
private void swapUp(int lastPosition){
if (lastPosition == -1){
return;
}
Item lastItem = items[lastPosition];
Item p = items[lastPosition-1];
if (lastItem.getNumber() < p.getNumber())
replace(lastPosition, lastPosition-1);
else
lastPosition = 0;
swapUp(lastPosition-1);
}
private void replace(int from, int to){
Item temporary = items[from];
items[from] = items[to];
items[to] = temporary;
}
and again i'm doing comparison of numbers you can implement any kind of comparison you want. i saw your previous question and modeled your classes
Music{number=1111, name='White and Nerdy', price=2.5, pro='"Weird Al" Yankovic'}
Music{number=2222, name='Amish Paradise', price=2.22, pro='"Weird Al" Yankovic'}
Music{number=3333, name='The Saga Begins', price=2.0, pro='"Weird Al" Yankovic'}
Movie{number=4444, name='UHF', price=9.99, pro='"Weird Al" Yankovic'}
Movie{number=5555, name='The Dark Crystal', price=8.99, pro='"Jim Henson'}
Movie{number=6666, name='Die Hard', price=13.99, pro='Bruce Willis'}
Movie{number=6969, name='The Adventures of Mr. Winky', price=9.99, pro='Richard Dickinson'}
Book{number=7777, name='When I Grow Up', price=7.98, pro='"Weird Al" Yankovic'}
Book{number=8888, name='The Chronicles of Pern: First Fall', price=5.99, pro='"Anne McCaffrey'}
Book{number=9999, name='Get gud you scrub', price=2.5, pro='Steve "Troll" Rathier'}
as you can see they are in sorted order.

MergeSort Algorithm only returning half of the inputted array

Having to learn different sorting methods for a school class but have encountered issues with coding MergeSort. Hoping someone can find what is causing the problem.
public static Movie[] mergeSortYears(Movie[] M)
{
Movie[] toReturn = new Movie[M.length];
if(M.length>1)
{
int length = M.length/2;
Movie[] LeftAr = Arrays.copyOfRange(M,0,length-1);
Movie[] RightAr = Arrays.copyOfRange(M,length,M.length-1);
Movie[] Left = mergeSortYears(LeftAr);
Movie[] Right = mergeSortYears(RightAr);
return mergeYears(Left,Right);
}
return M;
}
public static Movie[] mergeYears(Movie[] LeftAR, Movie[] RightAR)
{
Movie[] Left = LeftAR;
Movie[] Right = RightAR;
int Total = LeftAR.length + RightAR.length;
int i,li,ri;
Movie[] M = new Movie[Total];
i = 0;
li = 0;
ri = 0;
while(i< Total)
{
if((li < Left.length) && (ri<Right.length))
{
if(Left[li].Year < Right[ri].Year)
{
M[i] = Left[li];
i++;
li++;
}
else
{
M[i] = Right[ri];
i++;
ri++;
}
}
else
{
if(li >= Left.length)
{
while(ri<Right.length)
{
M[i] = Right[ri];
i++;
ri++;
}
}
if(ri >= Right.length)
{
while(li < Left.length)
{
M[i] = Left[li];
li++;
i++;
}
}
}
}
return M;
}
Yes I do admit it isn't exactly 'graceful' but it was the best I could do. But the issue arises when I call the function. I pass in an array containing 8 'Movie's (to pertain with the assignment) and it returns them properly sorted but only 4 of them. The thing that is really tripping me up is that I cannot figure out how it is losing those 4. It isn't like "The First 4" or "The Last 4" going missing. It is exactly the same 4 every time.
Movies do contain the 'Year' variable which contains their year of production, this is what they are sorted by in this instance. These do repeat in the majority of the movies but that doesn't appear to be causing the issue
(Can confirm it is the code presented causing the issue, earlier sections of this assignment had me sort with other methods and all those still work)

Convert a complex method into Lambda Expression

i want to write my code below, with new stuff. I want to use Java8 stream and functional programming.
private static void algoritmoSolC(List<Storage> freeSpaces, Double dimPacket, Double nPackets,
int storageIndex) {
if (nPackets == 0)
return;
List<Storage> list = new ArrayList(freeSpaces) {
public Object get(int index) {
if (index < 0) {
index = Math.abs(index);
} else if (index >= size()) {
index = index % size();
}
return super.get(index);
}
};
for (int i = 0; i < nPackets; i++) {
Storage storage = list.get(storageIndex);
if (storage.getFreeSpace() > dimPacket) {
storage.setFreeSpace(storage.getFreeSpace() - dimPacket);
++storageIndex;
} else {
++storageIndex;
++nPackets;
}
}
}
I think if I convert code in functional programming, I spent less time for result.
Can anyone help me to convert this snippet of code?
Thanks in advance
Didnt really tested it but it could go about this:
IntStream
.range(storageIndex,Integer.MAX_VALUE)
.mapToObj(i-> freeSpaces.get(Math.abs(i) % freeSpaces.size()))
.filter(storage -> storage.getFreeSpace() > dimPacket)
.limit(nPackets)
.forEach(storage.setFreeSpace(storage.getFreeSpace() - dimPacket))
Looking at this it is really surprisingly more elegant thant your code :-)

Java Sorting "queue" list based on DateTime and Z Position (part of school project)

For a school project i have a list of 50k containers that arrive on a boat.
These containers need to be sorted in a list in such a way that the earliest departure DateTimes are at the top and the containers above those above them.
This list then gets used for a crane that picks them up in order.
I started out with 2 Collection.sort() methods:
1st one to get them in the right X>Y>Z order
Collections.sort(containers, new Comparator<ContainerData>()
{
#Override
public int compare(ContainerData contData1, ContainerData contData2)
{
return positionSort(contData1.getLocation(),contData2.getLocation());
}
});
Then another one to reorder the dates while keeping the position in mind:
Collections.sort(containers, new Comparator<ContainerData>()
{
#Override
public int compare(ContainerData contData1, ContainerData contData2)
{
int c = contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());
int p = positionSort2(contData1.getLocation(), contData2.getLocation());
if(p != 0)
c = p;
return c;
}
});
But i never got this method to work..
What i got working now is rather quick and dirty and takes a long time to process (50seconds for all 50k):
First a sort on DateTime:
Collections.sort(containers, new Comparator<ContainerData>()
{
#Override
public int compare(ContainerData contData1, ContainerData contData2)
{
return contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());
}
});
Then a correction function that bumps top containers up:
containers = stackCorrection(containers);
private static List<ContainerData> stackCorrection(List<ContainerData> sortedContainerList)
{
for(int i = 0; i < sortedContainerList.size(); i++)
{
ContainerData current = sortedContainerList.get(i);
// 5 = Max Stack (0 index)
if(current.getLocation().getZ() < 5)
{ //Loop through possible containers above current
for(int j = 5; j > current.getLocation().getZ(); --j)
{ //Search for container above
for(int k = i + 1; k < sortedContainerList.size(); ++k)
if(sortedContainerList.get(k).getLocation().getX() == current.getLocation().getX())
{
if(sortedContainerList.get(k).getLocation().getY() == current.getLocation().getY())
{
if(sortedContainerList.get(k).getLocation().getZ() == j)
{ //Found -> move container above current
sortedContainerList.add(i, sortedContainerList.remove(k));
k = sortedContainerList.size();
i++;
}
}
}
}
}
}
return sortedContainerList;
}
I would like to implement this in a better/faster way. So any hints are appreciated. :)
I think you probably want to sort with a single Comparator that compares on all of the criteria. E.g.:
compareTo(other)
positionComparison = this.position.compareTo(other.position)
if positionComparison != 0
return positionComparison
return this.departureTime.compareTo(other.departureTime)

Categories