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.
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 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 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++)
calculate how long it would take to pay off a loan of 500 pounds if there was 10% interest monthly, and 100 pounds was paid each month. This code is in a pseudocode,
set months = 1 set balance = 500 set totalpaid = 0; while balance > 100 balance = balance - 100; set interest = balance * 0.1 balance = balance + interest totalpaid = totalpaid + 100 months = months + 1 endwhile totalpaid = totalpaid+balance display "you paid "+totalpaid display "It took you "+months+" months"
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);
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'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.
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?
How can i sort my ArrayList, which contains cars, with year and used year, i want to sort them first from year, and then from used year . what should i use?
public class Person implements Comparable<Person> { // the age of the person private int age; //the name of the person private String name; //the Integer object to wrap the age value; private Integer ageWrap;
[Code] .....
The collection library has a class named TreeSet, which is an example of a sorted set. Elements in this set are kept in order. Carefully read the description of this class, and then write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.
This is the exercise I am trying to solve. And this is as far as I have gotten to. Is it possible to sort my setOfPersons TreeSet directly? I tried to use
Collections.sort(setOfPersons)
method but it wont compile, and I realized that it is not applicable to TreeSet. So I made the
sortByAge()
method to do it indirectly...
I am puzzled though because in the exercise it states
write a class Person that can be inserted into a TreeSet, which will then sort the Person objects by age.
meaning that the TreeSet will sort the Person Objects and not my class..
I am getting incombatable types, I do not know why I am getting them..why I am getting the error?
The Error I am getting: stringSort.java:26: error: incompatible types if(myArray[j].compareToIgnoreCase(myArray[i].toString())){ ^ required: boolean found: int */
However, whenever I run the method, the element that should go last, Zachary, in this case, ends up getting moved to the front for some reason. I'm not sure why.
I tried changing what the first element was initialized to, to the variable i as that would logically work as well, but it ends up missing the first element in the list.
Java Code:
public static void selectionStringAscendingSort (String[] words){ int i, j, first; String temp; for ( i = 1; i < words.length; i++ ) { first = 0; //initialize to subscript of first element for(j = i; j < words.length; j ++){ //locate smallest element between positions 1 and i. if( words[ j ].compareTo(words[ first ]) <0 ) first = j; } temp = words[ first ]; //swap smallest found with element in position i. words[ first ] = words[ i ]; words[ i ] = temp; System.out.println(Arrays.toString(words)); } System.out.println(Arrays.toString(words)); } mh_sh_highlight_all('java');
how can I got about sorting an array that contains more than one value in a single element. Such as my array below has 4 values under one element. I know how to sort elements with single values however, slightly confused on this.
import java.util.Scanner; import java.util.Arrays; class Mobile {
I am trying to sort a set of parallel arrays. I really believe that the code is correct, but it is not working out as expected.This is the specific code for the sort:
Java Code: for (int y = 1; y < (dataArray.length + 1); y++) { for (int x = 0; x < dataArray.length - 1 ; x++) { if ((dataArray[x][1]) <= (dataArray[x + 1][1])); { tempOpen = dataArray[x][1]; dataArray[x][1] = dataArray[x + 1][1]; dataArray[x + 1][1] = tempOpen;