I'm trying to implement a non-recursive version of the insertion method, but I'm having a bit of trouble. From what I can tell, it's only returning the last two node..
public void insert(Key key, Value val) {
root = insert(key, val, root);
} private Node insert(Key key, Value val, Node x) {
if(x == null) {
x = new Node(key, val, 1);
Im trying to do an insertion sort using ArrayLists and I keep getting this error after the sorting section where it doesnt sort anything at all, but still displays my previous array list.:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 7 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Utilities.insertionSort(Utilities.java:102) at Utilities.main(Utilities.java:66)
My code:
import java.util.*; import java.lang.*; public class Utilities { public static void main(String[] args) { ArrayList<String> equipment = new ArrayList<String>();
I could have copied the code for a standard algorithm such as insertion sort, but I wanted to do it on my own to see how well I think. I came up with a working solution below. This is efficient or not or if I can make improvements. Would this approach ring any alarm bells in an interview ?
public static void insertionSort(int[] unsortedArray) { int[] a = unsortedArray;// alias for the array int s = 0;// index before which all elements are in order. int tmp = 0;// temporary variable int last = a.length - 1;
I can't spot where my java implementation of insertion sort differs from the pseudocode here:Well, there is one difference in the parameters used by the insert method, which is inconsistent in the pseudocode.I'm pretty sure it should be calling insert (a,n) instead of (a,i,n);
insert(a,k) i←k x ← a[k] while x < a[i − 1] x ← a[i] a[i ] ← a[i − 1] i ←i −1 a[i]←x return
insertion-sort(a,n) m ← select-min(a,1,n) swap(a,1,m) fori from2upton insert(a,i,n) return
Here is my attempt at a java implementation, which doesn't actually seem to do anything.I've kept variable names as in the pseudocode. Might technically be bad practice, butI think it should make it easier to follow in this particular scenario.public class InsertionSort {
Why am I so interested in this pseudocode when there are simpler java-ready examples of insertion sort on the internet? Simply because this is the code the professor uses, so I should be able to understand it.
I am having some difficulty adding a new item to the HashTable when a collision occurs. We can only use the basic utilities to code this, so we are coding a HashTable by hand. We have an array of length of 10, which each index holds or will hold a Node for a Linked List.
The code hashes fine, adds the item, but the problem exists when adding items that already been hashed. The project is much bigger, but this is the engine behind the rest, and I figured I would tackle this first.
The items we are adding are objects which are states containing different information, we hash based on the ASCII sum % tableSize.
Here is the code I am testing with to add items:
HashTable ht = new HashTable(10); State az = new State("Arizona","AZ","W",2,"Y",2); State fl = new State("Florida", "FL", "F", 2, "X", 2); State hi = new State("Hawaii", "HI", "H", 3, "Z", 1); State al = new State("Alabama", "AL", "A", 5, "W", 0); ht.insert(hi);
I am working on my generic insertion sort program. When I completed and run my code, I am having trouble with my code. So, I am just trying to see if I get an correct array from a file. However, i just get so many nulls in the array. Therefore, I can't run my insertionSort function because of null values.
im trying to create an insertion sort method for a vector. I know how to insertionsort for an array, but for a vector im having problems
Source code: PHP Code: package test; import java.util.*; import java.io.*; public class LinearSearch { public static void main (String[] args) { Vector myVector = new Vector();
[Code]...
I'm getting errors at lines 38 and 39 "Left-hand side of an assignment must be a variable". "Syntax-error(s) on token(s) misplaces contructor(s)". How can i fix them ??
I'm trying to insert data into an excel sheet with the below Servlet.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
But it is giving me the below Exception and stacktrace
java.text.ParseException: Unparseable date: "2-Apr" at java.text.DateFormat.parse(DateFormat.java:357) at Serv1.doPost(Serv1.java:53) at javax.servlet.http.HttpServlet.service(HttpServlet.java:644) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
[Code] ....
i found that the problem is within the date field(, if the problem is with the date, how i can fix it and where am i going wrong.
I have to write the Insertion Sort Algorithm using Java codes and at the same time find the time of execution for different sizes of array, filled with random numbers. If I try to show the numbers inserted into the array randomly, they don't appear at the console.
import javax.swing.JOptionPane; public class Insertion { public static void main(String[]args){ int SizeArr = new Integer(JOptionPane.showInputDialog("Enter the size of teh array")).intValue(); int [] r= new int [SizeArr]; {for(int d=0; d<r.length; d++)
Operator is undefined for argument type. Error is located at the end of the binary search method array[position] < key
import java.util.Arrays; public class binarySearch { public static <T extends Comparable<T>> int binarysearch(T key, T[] array) { int start = 0; int end = array.length - 1; int position =-1; while (start <= end && position == -1) {
We have an autosys job running in our production on daily basis. It calls a shell script which in turn calls a java servlet. This servlet reads these files and inserts the data into two different tables and then does some processing. Java version is 1.6 & application server is WAS7 and database is oracel-11g.
We get several issues with this process like it takes time, goes out of memory etc etc. Below are the details of the way we have coded this process.
1. When we read the file using BufferedReader, do we really get a lot of strings created in the memory as returned by readLine() method of BufferedReader? These files contain 4-5Lacs of line. All the records are separated by newline character. Is there a better way to read files in java to achieve efficiency? I couldnt find any provided the fact that all the record lines in the file are of variable length.
2. When we insert the data then we are doing a batch process with statement/prepared statement. We are making one batch containing all the records of the file. Does it really matter to break the batch size to have better performance?
3. If the tables has no indexes defined nor any other constraints and all the columns are VARCHAR type, then which operation will be faster:- inserting a new row or updating an existing row based upon some matching condition?
As one of the methods of my IntTree tree I have to implement a method that multiplies the level number with the sum of the nodes on the current level. So far I have this, and it doesn't work. What I am wondering is am I on the right track at all with the second return statement?
public int depthSum(){ return depthSum(overallRoot); } private int depthSum(IntTreeNode root) { if(root==null) return 0; int level = 0;
I have a requirement where I have a class as Page which itself contains ArrayList<Page>.Here ArrayList<Page> is nothing but the pages which are accessible from the base Page.I know the depth level ( reading from file) which means how many level I need to go to identify more pages.BUT the problem is how to set the base Page class. I need to set the base Page class but for that I need the objects for the subsequent pages and hence an iterative type of implementation.
I am not sure how to add all the possibilities of elements in an array and find the greatest sum. I want to do this recursively. I don't need any code. How I would do it.
I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below
public static double Series(int n){ if (n==0)return 0; else return (n/(n*2+1)) + Series(n - 1); }
However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.
I recently wrote a simple recursive program that chooses K objects out of N (I was asked to use the variables N choose the R, however) total objects. Here is the code:
int n = 0; int r = 0; //the total number of objects defaults to 0 String nChoice = JOptionPane.showInputDialog(null, "How many objects are there to choose from?"); String rChoice = JOptionPane.showInputDialog(null, "How many object are to be chosen from this group?"); try { n = Integer.parseInt(nChoice);
[Code] ....
It works fine, however in my class we were given two different formula to implement into our code. I used the one above, obviously. However, the second formula we were given was:
C(n,R) = n! -------(R!(n-R)!)
I had to get the spacing right.
How do I read this formula? How could it be implemented? What are the benefits (if there are any) from using one method over the other? Which method of calculating N choose K (or, in my case, N choose R) would be more widely accepted?
I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?
XML Code:
package recursivecall; import java.util.Scanner; /** * Author: <<Conrado Sanchez>> Date: Task: */ public class RecursiveCall {
I wrote this tail recursive function that mirrors the iterative version, except that the loop in the iterative version is replaced by an if statement here and then a recursive call. Is this truly recursive? I have seen the fibo(n-1) + fibo(n - 2) version, but is this also an acceptable recursive solution? Why is it never solved this way?
public class FiboRecursive { public static int fibo (int n) { int sum = 0; int n1 = 1; int n2 = 1; if (n == 1 || n == 2) { sum = 1;