Creating Merge Sort For String Arrays

Mar 31, 2014

I'm having a problem printing out the descending order of my array. The array order goes like (Title,Studio,Year). I try to create to ints with the compareTo method but when the program is run the I get array out of bounds. Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?

public class Movie2
{
// instance variables
private int year;
private String Title;
private String Studio;

[code]...

View Replies


ADVERTISEMENT

Merge Sort With Random Numbers

Mar 2, 2015

How do i make this merge sort take on random numbers in an array instead of hard coding the numbers

Integer [] a = {8, 2, 6, 7, 5, 4, 3, 1};
mergeSort(a);
System.out.println(Arrays.toString(a));
}
public static void mergeSort (Comparable [] a){

[code]....

View Replies View Related

Java Merge Sort Not Working

Dec 10, 2014

I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...

public class MergeSort
{
private static int[] local; // for use in copying
private static int[] a;
public static void main(String[] args) {
int[] test = {2,3,1,4,7,8,6,3,9};

[Code] ....

View Replies View Related

Implementing Merge Sort On Given Array

Jun 15, 2014

Merge sort implementation on the given array i listed in the code below. I know Arrays.sort() or Collections.sort() could do the trick but i want to achieve it through merge sort.

import java.util.*;
public class Merge{
public static void main(String[] args){
String[] myStringArray=new String[]{"z","a","z","b","c","e","z","f"
,"g","a","w","d","m" ,"x","a","R"
,"q","r","y","w","j","a","v","z","b"};
}
}

View Replies View Related

Doing Merge Sort - Incompatible Type

Apr 10, 2014

I am new in java.. I am having a problem when doing merge sort. This is my coding...

Java Code:

import java.util.*;
public class Person
{
public static void main(String[] args)
{
String[] list = {"

[Code] ....

the error is incompatible type at 'sorted = merge(left,right);'

View Replies View Related

Merge Sort Java And Linked List?

Dec 11, 2014

I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.

My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.

//defining a job that has priority
public class Job {
private int priority;

[Code]....

The problem I've been trying to solve is that I am getting the stack overflow at line

ListNode<T> h1 = mergeSort(left);

meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.

View Replies View Related

Merge Sort Data - Time Complexity

Mar 16, 2014

Suppose i have written a method like this

public void myWork(){
mergeSortMyData(); O(nlgn)
....
someProcessing(); O(n^2)
someprocessing2(); O(n^2)
}

then what will be time complexity of my code...according to me it will be O(nlgn + n^2) ...is it correct ?

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

Passing 2 Sub Arrays Into Sorting Thread Then To A 3rd Thread Merge

Oct 17, 2014

im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,

Runnable InSortlist1 = new sortList(data2p1);
Runnable InSortlist1 = new sortList(data2p1);
Thread th1 = new Thread (IntSortlist1);
Thread th2 = new Thread (IntSortlist2);
try
{
th1.start();
th1.join();

[code]....

View Replies View Related

Sort A Set Of Parallel Arrays

Mar 30, 2014

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;

[code]....

View Replies View Related

Sort And Match Items In A Collection Of Arrays - Index Out Of Bounds

Apr 8, 2015

I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I would expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.

package testing;
public class TestMatchMain {
public static void main(String[] args) {
Comparable[] innerCollection0 = {1,2,3,4,5};
Comparable[] innerCollection1 = {1,1,5,6,7};

[Code] .....

View Replies View Related

Creating Flowchart For Bubble Sort

Apr 27, 2015

I have to make a flowchart that shows a bubble sort. Here is the exact directions: provide a flowchart for a bubble sort, and write the pseudo-code that corresponds to the flowchart. You can assume the existence of a swapElement method (ie. use the predefined process symbol).one that does one pass through the array and returns a boolean if items were swapped (I'll call this the one pass method) another method that repeatedly calls the one pass method until that method returns false.This means that your flowchart will have two separate pieces for the one pass method and for the overall flow of the program (this one would use the predefined process symbol for the one pass method).

I have looked up what a bubble sort. It says it is another type of sort that involves repeatedly stepping through the array comparing adjacent items and swapping them if they are in the wrong order.The sort steps through the list multiple times until no swaps are needed, at which point the list is sorted.

View Replies View Related

NullPointException - Creating Pairs From Arrays

Oct 9, 2014

So in my java class we are suppose to ask for how many names are going to be entered, create an array of that length, and then store that many inputted names. We then take those names, alphabetize them, and create a set of pairs. For example, it might look like this:

How many people are there? 4
Please enter the names of those people:
Dana
Bob
Alice
Charlie

There are 6 possible pairings. They are:
Alice & Bob
Alice & Charlie
Alice & Dana
Bob & Charlie
Bob & Dana
Charlie & Dana

We are also suppose to account for less than two names (both 1 and 0 as well as negatives).

I keep getting a null point exception error and I can't figure out why. Even debugging in Eclipse doesn't give any pointers except that it is originating in line 76.

import java.util.Scanner;
import java.util.Arrays;
class partners
{
public static int numNames;
public static int numPairs;

[Code] .....

View Replies View Related

Creating A Scanner And Putting Input Into Arrays

Apr 15, 2014

I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit".

import java.util.*;
public class arrayScanner {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> doubleList = new ArrayList<Double>();
ArrayList<String> otherList = new ArrayList<String>();

[code].....

View Replies View Related

Creating Methods Using Arrays - Fraction Class

Sep 27, 2014

I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.

package fractions;
import java.util.Scanner;
import java.util.Arrays;
public class FractionArrays {
public static void main(String[] args) {
Fraction completeFraction = new Fraction(5,6);

[Code] ....

I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?

View Replies View Related

Creating Palindrome Program - Arrays And For Statements?

Feb 14, 2015

I am trying to create a program that reads a sentence, such as: "abba is running to the radar" scans this sentence, and then prints out all the palindromes. I am running into issues with my arrays and for statements. Here is my code:

static String palindrome, backwardsLower,
palindromeLower, palindromeClean, backwards2, backwards = "";
static String[] words;
static int counter;
public static void main(String[] args) {
palindrome = JOptionPane.showInputDialog("Please enter a phrase. " +

[Code] ...

I am aware that there is a few "useless" variables in there at the moment, I will clean them up (as well as some useless statements, I see those too). The issue comes at about line 17. The variable backwards REMOVES all the spaces from the array, so when it comes time to compare the strings, it is comparing individual words to the ENTIRE string, thus no words will ever be a palindrome.

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Sort String Method

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

Program To Accept 10 Name In String And Sorting In Bubble Sort Alphabetically

Jun 10, 2014

import java.io.*;
Class bubble
{
public static void main()throws IOException
{
BuffetedReader br=new BufferedReader (new InputStreamReader (System.in));
int j, k, temp;
String st[]=new string [10];

[Code] .....

View Replies View Related

How To Access String Arrays From Console List

Apr 8, 2014

We were suppose to make a program for an assignment and the prof provided some codes to start of. What does this basically mean? How do i access the string arrays from consolelist?

class ConsoleInfo {
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
private static String empPassword;
ConsoleInfo(String title, double price, int qty,String pic)

[Code]...

View Replies View Related

Sort String List Alphabetically With CompareTo Method / If Else Statements - Logic Errors

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

Creating Final Arrays With Final Elements

Aug 2, 2013

I want to create a final array with final elements
 
{code}
final int[] array = {0, 1, 2, 3, 4, 5};
{code}
 
But here only the reference of the array is final , not it's elements ... So how can I do that??

View Replies View Related

Why Does String Split Method Producing Arrays With Whitespace Variables

Jan 17, 2015

I used the string split method in the following lines of code like this:

String[] numbers = out.split("[^d.]", 0);
String[] operations = out.split("[.d()]", 0);

When out is equal to the String "2x2.5", the array operations ends up looking like this when it is printed using the toString method:

[, , , x]

As you can see, before the array element x, there are three String variables which only contain whitespace. Why does this occur, and how can I prevent this from happening?

View Replies View Related

Creating A Backwards String?

Nov 11, 2014

I am working on a class project where I have to give the program a predefined String, have it output the first char, then the second char, then the String backwards, and then the String as it originally was. The problem is that it's not out putting all of the information that I need it to. Here's my code:

Here is the class that prints the first char, second char, etc.:
 
public class Word { 
private String word;
 public Word() {
 }
 public Word(String s) {
 setString(word);

[code]....

Here's the main class:
 
public class WordRunner { 
public static void main(String[] args) {
 Word run = new Word();
 run.setString("Hello");

[Code] ....

The output is supposed to look like this:

H
o
olleH
Hello

But this is what I get:

olleH

View Replies View Related

Creating Enum With String And Method

Nov 9, 2014

How would I go about and make an enum, that has Strings and Methods?I want to make a class called GraphicalEffects, this class and be instanstiated and it has a method to apply graphicalEffects AS methods or some type of references to methods in an ArrayList.

View Replies View Related

What Is The Difference Between Regular Arrays And Multi Dimensional Arrays

Jun 28, 2014

I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.

View Replies View Related







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