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
ADVERTISEMENT
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
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
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
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
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
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
View Related
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
Nov 14, 2014
class GVector {
// TODO: declare a private array to save the vector coordinates
// Creates a mathematical vector of d dimensions, initialized at 0
public GVector(int d) {
// TODO: implementation
[Code] ....
I'm confused with what type of array I need to use to save the vector coordinates and what to put in Gvector. Is it a constructor?
View Replies
View Related
Dec 25, 2014
when I am programming let say in VB using Visual Studio, finally I build .exe file that can be run on all Windows by double click.For Java I am using Eclipse and to run those apps I am using run from Eclipse.How I can create a sort of "executable" for my Java app that I would be able to run it by file click on Windows or Linux?
View Replies
View Related
Mar 30, 2015
I am writing a program which writes down all possible equation y=a+b+c values from min to max (in reality this equation would more difficult, but here is just short example).
The problem is that my sorting code can't get access to full array in loop.
Is there any way to pass array to sorting code, or somehow change sorting code?
package pkg06;
public class Main {
public static void main(String[] args) {
double aS =-1;
double aE = 3;
[code]...
View Replies
View Related
Aug 7, 2014
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
*/
[code]....
View Replies
View Related
Mar 5, 2014
I'm trying to make a selection sort method that will sort a list of Strings alphabetically.
I have a test list of Strings like so:
Joe, Steve, Oscar, Drew, Evan, Brian, Ryan, Adam, Zachary, Jane, Doe, Susan, Fred, Billy-Bob, Cindy, Mildred, Joseph, Hammer, Hank, Dennis, Barbara
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');
View Replies
View Related
Nov 11, 2014
I have a question about selection sort. If I had an array of numbers, 1 2 3 4 100 0, and used the selection sort method (below) on the array,would the 0 slowly work it's way down to the end?
Nest loop = full rotation of nest loop
Nest loop 1: 1 2 3 4 100 0
Nest loop 2: 1 2 3 4 100 0
Nest loop 3: 1 2 3 4 100 0
Nest loop 4: 1 2 3 4 100 0
Nest loop 5: 1 2 3 4 0 100
Nest loop 6: 1 2 3 0 4 100
etc.
METHOD:
static void selectionSort(int[] arr){
int n = arr.length;
for(int i = 0;i<n;i++){
int min = i;
for(int j = i+1;j<n;j++){
if(arr[j]<arr[min]){
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
View Replies
View Related
Mar 16, 2015
I have an ArrayList, based on the class which stores cricket players, their names and runs scored.When I use the Collections.sort() method my arraylist is sorted alphabetically by forename.how to OverRide the comparing method to sort by runs, and thus the code I use to sort the list?
View Replies
View Related
Oct 21, 2014
How would I modify this version of a selection sort into a recursive method?
public static void selectionSortRecursive(Comparable [] list, int n)
{
int min;
Comparable temp;
for(int index =0; index < n-1; index++){
min = index;
[code]....
View Replies
View Related
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
Jan 21, 2014
I'm doing an exercise we're you're supposed to sort strings in alphabetical order, without importing anything , not using the Arrays.sort() method.
I think I got the method down partially right, or it is on the right track, but it is completely not being applied to my answer. All it prints out in the console is the actual String array twice, without sorting anything.
public class arrayofstrings {
public static void sort(String[] a) {
String temp= "";
int min;
int i= 0;
for (int j=0; j<a.length-1; j++) {
[Code] ....
View Replies
View Related
Mar 19, 2015
Java Code:
import java.util.ArrayList;
import java.util.Random;
public class NumSorting {
public static int numOfComps = 0,
numOfSwaps = 0;
public static void main(String[] args)
[Code] ....
What is wrong with my code in this sorting program? It won't copy the random generated numbers to the sort method. How to get the random generated numbers to copy to the sort method. Below is what the program is displaying.
Original order : 3 2 5 4 1
Selection Sort
Original order:
[I@7a84e4
Number of comps = 10
Number of swaps = 0
Sorted order : 0 0 0 0 0
View Replies
View Related
Sep 14, 2014
So I'm trying to implement a quick sort method for an ArrayList of Strings and right now I'm getting the compiler error message: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space. I don't know what that error means nor how to fix it. I've marked in my code where the error seems to be occurring.
import java.util.ArrayList;
public class quickSort
{
// constructor
public quickSort()
[code]....
View Replies
View Related
Nov 20, 2014
This time I am having difficulties with selection sort method, but not with the method itself (I think). So I need to sort an array and return the result of each step.
This is the main code:
public class Functionality {
public static int[][] selctionsort(int[] a) {
for (int i=0; i<a.length; i++) {
int least = i;
for (int j=i+1; j<a.length; j++)
[Code] ....
And this is the Test folder:
import static org.junit.Assert.assertArrayEquals;
public class PublicTests {
public static void main(String[] args) {
int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
int[][] b = new int[][] { new int[] { 7, 35, 63, 42, 24, 21 },
[Code] ....
Now I am not sure what should I write in return since the 2nd (test) project has int[][] and in my main project I am working with int [].
View Replies
View Related
Oct 6, 2014
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: ");
//input.nextLine();
string1 = input.nextLine();
System.out.println("Enter the second statement: ");
string2 = input.nextLine();
System.out.println("Enter the third statement: ");
string3 = input.nextLine();
[Code] ....
View Replies
View Related
Mar 29, 2015
I want to convert this following piece of c++ code into java...but i cannot find suitable substitute for vector pair...what shall i do?
#include <cmath>
#include <cstdio>
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
bool compare_polar (int x1, int y1, int x2, int y2) { // if p2 > p1 return true
[Code] .....
View Replies
View Related
Nov 11, 2014
I'm having some issues, trying to solve this problem in java. I want to print some election results, and i have to loop through a vector of objects and retrieve the partial sums of each party's seats for each constituency and the national results for each party. For now i can print the results per contituency, but i'm having problems in getting the national results. Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.
This is what i have.
Java Code:
while (i < h.geral.size()) {
show += "Constituency - "
+ ((Party) h.geral.elementAt(i)).getConstituency() + "
[Code] .....
View Replies
View Related
Sep 23, 2014
I understand how vectors work I'm currently using one to store my id's from my txt file but how do you put them in a defaultComboBoxModel?
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel();
//declare a vector object that will hold a string (only thing that works with comboboxmodel
Vector<String> myVector=new Vector<String>();
//try statement
try{
FileReader fr = new FileReader(file);
[Code]...
Any example of how a vector is used with a defaultComboBoxModel so I can then use that to populate my JComboBox?
View Replies
View Related
Jan 26, 2015
When I input "the" or "and" I always receive "its not here" as the output. Why is this? I also tried printing the values of v.get(i).other and the system printed null twice.
public class Translator {
public String other;
public Translator(String x) {
x = other;
[Code].....
View Replies
View Related