How To Make Insertion Sort Into Non-increasing Algorithm

Aug 29, 2014

In class we were give the algorithm for a non-decreasing algorithm here:

This is pseudo code given for the non-decreasing.

for j = 2 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i -1
A[i = 1] = key

//I was asked to make it non-increasing, so this is what I have.

for j = A.length - 1 to 1
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i+1] = A[i]
i = i + 1
A[i-1] = key

Is there anything wrong with this algorithm? Will it give me the non-increasing sort?

View Replies


ADVERTISEMENT

Insertion Sort Algorithm Using Java Codes

Jan 25, 2015

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++)

[Code]...

View Replies View Related

Insertion Sort Using ArrayList

Sep 27, 2014

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>();

[Code] ......

View Replies View Related

Is Insertion Sort Inefficient

Jul 20, 2014

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;

[code]....

View Replies View Related

Insertion Sort Pseudocode

Jan 1, 2014

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 {

public static void main(String[] args) {
int[] array = { 99, 2, 44, 14, 14, 44, 11, 33, 14, 14, 51 };
insertionSort(array, 10);
for (int i : array)
System.out.print(i + " ");

[code]...

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.

View Replies View Related

Insertion Sort Based On Two Attributes

Feb 4, 2014

public class Employee {
private String firstName;
private String eeid;
}

I want to sort using empoyee firstName.. If two employee names are same, i need to sort based on eeid.. using insertion sort

if (key == "name") {
for (int i = 1; i < list.length; i++) {
Employee t = list[i];
int j;
for (j = i - 1; j >= 0 && t.getLastName().compareTo(list[j].getLastName())<0; j--)
list[j + 1] = list[j];
list[j + 1] = t;
}
}

It will sort using names.. But if two names are same, it wont sort based on eeid.. I want to achieve only using insertion sort..

EX:

vivek 8
broody 2
chari 3
vivek 5
chari 1

output:

broody 2
chari 1
chari 3
vivek 5
vivek 8

View Replies View Related

Generic Insertion Sort Program?

Apr 14, 2015

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.

Here is my code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;

[Code].....

View Replies View Related

How To Create Insertion Sort Method For A Vector

Jun 9, 2014

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 ??

View Replies View Related

Java Radix Sort Algorithm

Apr 19, 2014

I got this code from wikipedia when trying to learn about the radix sort algorithm now I understand that the algorithm sorts by significant digits but it's the code that I'm not too sure about for instance the series of for loops at the bottom what exactly is going on there and why is it mod by 10? Also why are there three different integer arrays a, b, and bucket?

public static void radixsort( int[] a, int n) {
int i;
int digit = 1;
int[] b = new int[n+1];
for (i = 1; i < n; i++)
if (a[i] > a[0])
a[0] = a[i];

[Code] ....

View Replies View Related

Find Which Algorithm Java Uses For Its Sort Method

Apr 27, 2014

I have been looking around and I cannot seem to find which algorithm java uses for its sort method. Merge, sort etc.

While I am on the topic BinaryTree is a balanced heap right?

Are there any libraries for depth or breath first search?

Is the ArrayList a double linked list?

View Replies View Related

Writing A Recursive Merge Sort Algorithm

Mar 27, 2015

I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].

I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.

Below is what I have along with my JUnit test.

Java Code:

private Queue<T> output = new Queue<T>();
private Queue<T> output1 = new Queue<T>();
private Queue<T> output2 = new Queue<T>();
public Queue<T> mergeSort(Queue<T> queue) {
// TODO 1
if(queue.size() <= 1) {
return queue;

[Code] .....

View Replies View Related

Quick Sort Algorithm - Stack Overflow Error

Oct 15, 2014

I have wriiten a quick sort algorithm. I have used the last element as my pivot. The program is running for all sizes except for 100000 and 1000000 elements when they are sorted and unsorted list .

It shows me the error : Exception in thread "main" java.lang.StackOverflowError

I guess the memory gets out of space and we need to increase the stack size in eclipse. How do I increase the stack size in eclipse.? I tried increasing through run--run configurations-- program arguments and typed---- -Xmx4096m but this didn't work in any way.

View Replies View Related

Increasing Array Size Automatically

Dec 8, 2014

The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it .I am using a course class and here is the code for the class

public class Course {
private String courseName;
private int numberOfStudents;
private String[] students = new String[100];
public Course(String courseName)

[Code] ....

As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

View Replies View Related

Automatically Increasing Array Size?

Feb 5, 2014

I'm working on an assignment that says the following.

" The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.Implement the dropStudent method.Add a new method named clear() that removes all students from the course.

Write a test program that creates a course, adds three students, removes one, and displays the students in the course."

10.6 Listing

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

My Test Code based off of book

public static void main(String[] args) {
Course course1= new Course("Business");
course1.addStudent("Jay");
course1.addStudent("Silent Bob");
course1.addStudent("Dante");
course1.dropStudent("Jay");
 
[Code]....

My adjusted 10.6

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.

I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha.

My *best* attempt at the loop so far has been

if (students == students.length){
int bigArray = 2*students.length;
String increaseArray()= new String[students];
System.arraycopy(students, 0, increaseArray, 0, students.length);
students= increaseArray;

but,yeah... Doesn't seem to be right.

View Replies View Related

Increasing / Decreasing Size Of Array

Jul 11, 2014

I am trying to make a code that copies the users String to a char array. However, I am in a predicament: since I would not know the exact size of the users String I am left with the options of either making my array large by default, filled in with, lets say 25, empty spaces per index OR starting out with a default size of 1, with an empty space, and then some how increase the size from there.

At this moment I am leaning on the first option, creating a large default array and then deleting the unused values. However, this brings me to my actual question: if I set the non used indexes to null, if that wont give me an error, would that change the size of my array?

Ex:
//lets say i finally copied all of the values and this is the result
char[] word = {'b', 'o', 'b', ' ', ' '};
for(int i = word.length(); i > 0; i--){
if(word[i] == ' ')//delete the value so the size decreases
word[i] = null;//if possible
}

View Replies View Related

Increasing Counter When User Inputs C / D Or E

Feb 15, 2015

We are writing our own classes and methods. My instructor has provide the code

/*-------------------------------------------------------------------------
import java.util.*;
public class Assignment5 {
public static void main (String[] args) {

[Code].....

I am having trouble with the question counter. I need the counter to increase when c, d or e are entered. I think I need to set up a if or while loop but I'm not sure how to setup the variables. This is what I have for the counter so far.

[public int getNumberOfQuestions(){
numQuestions = 0;
numQuestions ++;
return numQuestions;
}

View Replies View Related

Consider Sequence Of Digits From 1 Through N In Increasing Order

Sep 3, 2014

Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N

Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.

Example: Enter a number: 7
1+2-3+4-5-6+7=0
1+2-3-4+5+6-7=0
1-2+3+4-5+6-7=0
1-2-3-4-5+6+7=0

View Replies View Related

Increasing Java Heap Size

Nov 21, 2013

I am using a 64 bit Win 7 Pc with 64-bit JVM and we get the error: Java heap space. So we want to increase the Java heap size but not for one application but for every application or in general.

We tried with the java -xmx command but it didn't work...

We tried setting the system variable JAVA_OPTS but again it didn't work...

View Replies View Related

Finding Longest Increasing Sequence Not Giving Proper Result

Jan 28, 2015

Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.

So if the file was this, (this is Ass1Q2_test4.txt)

97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24

the output should be,

(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96

Greatest path is of length 10.

Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.

So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.

97 47 56
35 57 41
89 36 98

Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.

In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.

I've also attached Ass1Q2_test4.txt

import java.util.*;
import java.io.*;
public class MaxIncreasingSub {

[Code].....

View Replies View Related

Non-recursive BST Insertion?

Apr 6, 2014

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);

[code].....

View Replies View Related

Program That Asks User For Integer And Prints Out All Its Factors In Increasing Order

Nov 18, 2014

Write a program that asks the user for an integer andthen prints out all its factors in increasing order. Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorTester whose main methods reads a user input, constructs a FactorGenerator object and prints the factors.

Here is what I have so far for my main class and tester class

public class FactorGenerator
{
int factor;
int number;
int x;
FactorGenerator(int numberToFactor)
{
number = numberToFactor;
}

[Code]....

View Replies View Related

Insertion Of HTML Code At A Particular Position

Jul 8, 2014

I have to insert certain lines of html code in already existing file of html using java.I have to add certain things in head and some things in body.

View Replies View Related

HashTable Insertion - Duplicate Records

Apr 20, 2015

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);

[Code] ....

View Replies View Related

Data Insertion Into Excel Sheet With Servlet?

Apr 2, 2014

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.
*/

import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

[Code] ....

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.

View Replies View Related

Insertion Points In Array Using Binary Search Using Comparable

Feb 24, 2014

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) {

[Code]....

View Replies View Related

How To Improve File Reading Efficiency And Its Data Insertion In Java

Feb 8, 2014

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?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved