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


ADVERTISEMENT

Recursive Selection Sort

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;

[code]....

View Replies View Related

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

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

Making Selection Sort Into A Recursive Method

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

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

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 View Related

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

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

Merge 2 CSV Files Containing Some Values

Feb 4, 2014

Have 2 files File1.csv and file2.csv containing some values. Have to read both files and write unique values in to another file.

In the new the there must be unique rows and not duplicate rows (2 files contain some rows same). What is the best approach for doing this?

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

JSF :: Using Merge Method To Update DB Record

Apr 25, 2014

So I have two related tables - Country (Master) Projects (Detail). Country has a OneToMany relationship with Projects. I put a link on my AllProjects.xhtml page. When tyhe link is clicked it uses a data model class to to display the current (clicked) project record (projectRecord object). After editing all the details, the UPDATE button calls a merge method in the DAO class. On the first attempt to update, nothing happens and the record is returned as it is with no errors. If I try updating again, I get:

Error Code: 1062
Call: INSERT INTO projects_beta2b_projects_beta2b (projects_PROJECTID) VALUES (?)
bind => [1 parameter bound]
Query: DataModifyQuery(name="projects" sql="INSERT INTO projects_beta2b_projects_beta2b (projects_PROJECTID) VALUES (?)")

[Code] .....

My classes are:

projects.java

@Entity
@Table(name = "projects_beta2b")
public class Projects implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int projectid;
private String projectName;
private String client;

[Code] ...

Is the 'new' keyword in the entity class causing the Inser statement in the DAO because the JPA thinks I want to create a new object therefore invokes INSERT instead of UPDATE?

View Replies View Related

How To Merge Cell In DefaultTableModel / JTable

Feb 18, 2014

I searched a lot and got some answers for this Q. but many of them referred to links which give 404 error. I want to make table like this:

Can I make this in Java?

View Replies View Related

Recursive Method For A Tree

May 9, 2015

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;

[code]....

View Replies View Related

How To Merge Two SQL Table Columns Into One JTable Column

Sep 5, 2013

I have a database table containing two columns A and B. They both contain integers. I'd like to know if it's possible, when displaying them in a JTable, to be combined in one column X. I still need, however, to be able to distinguish them from each other when selecting a row from the JTable. E.g. to store each of the values in a a separate variable. Maybe I can combine them when reading from the ResultSet and use some sort of delimiter ? But how?

View Replies View Related

Recursive Implantation For Setting Objects

Apr 8, 2015

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.

View Replies View Related

Recursive Function To Test All Combinations?

May 17, 2014

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.

View Replies View Related

Tracing Recursive Method That Calculates GCD

May 3, 2014

I have this code and i want to trace it

public static int GCD ( int x , int y ) {
if ( y == 0 )
return x;
else if ( x >= y && y > 0)
return GCD ( y , x % y );
else return GCD ( y , x );
}

(it is a recursive method that calculates the greatest common divisor using Euclidean method )

while x = 32 and y = 46

I want here to understand how the code work ? Precisely , composition and decomposition operations.

View Replies View Related

Why Recursive Method Continue To Return 0

Nov 13, 2014

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.

View Replies View Related

Recursive Method And Printing Out Stars?

Nov 23, 2014

KtMok1t.jpg

Below is what I go so far, but how to do star C and E.

public class PrintTriangle
{
public static void printStars (int star)
{
for (int number = 0; number < star;number ++)
{
System.out.print("*");

[Code] ....

View Replies View Related







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