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
ADVERTISEMENT
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
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
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
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
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
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
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
Jan 5, 2015
is this program correct? for Implementing two Stack in one array. how can i solve it ?
public class Stackation11 {
int max = 10;
int [] array = new int[max];
int top;
int top1;
Stackation11(){
[code]....
View Replies
View Related
Apr 24, 2014
Having supplied an interface the question arises as to how it should be implemented. There are a number of possibilities including the use of an array list or a linked list; however it is perfectly possible, and in fact instructive, to begin by implementing PhoneDirectory using a simple array of DirectoryEntry objects. This approach is illustrated below:
public class ArrayPhoneDirectory implements PhoneDirectory
{
private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;
// holds telno of directory entries
private int size = 0;
// Array to contain directory entries
[Code] ......
Develop a tester class called ArrayPhoneDirectoryTester . As usual all methods in the class being tested should be executed at least once by the test driver. Supply standard test documentation - Test Plan, Test Data and Test Log.
In addition to the methods described in the interface the class ArrayPhoneDirectory contains three private methods listed below:
// Searches the array of directory entries for a specific name
private int find(String name){}
// Adds a new entry with the given name and telno to the array of
// directory entries
private void add(String name, String telno){}
// Creates a new array of directory entries with twice the capacity
// of the previous one
private void reallocate(){}
These methods can be thought of as "helper" methods in that they assist the work of the public methods of the class. For example, the find() method is used by the lookUpEntry() method to locate the position of the a specific directory entry.
Pseudo-code algorithms for the methods of ArrayPhoneDirectory are listed below
Algorithm for load
create a Scanner to read file
while (not end of file)
use Scanner to read the name
use Scanner to read the telno
create a DirectoryEntry
add the new entry to theDirectory
close file
This method is a good place to start coding but note that load makes use of the add method in case the number of entries stored in the text file has exceeded the initial capacity of the directory. So to implement load fully you will need a working version of the "helper" method add. However you can do this in stages by first developing a basic version of add which does not increase the size of the directory. In reading data from the file you may assume that the file only contains matched pairs of names and numbers. There is no need to consider the case of a name not being associated with a telephone number.
Algorithm for addChangeEntry
call find to see whether name is in directory
if name is in theDirectory
change the telno using setNumber
return the old telno
else
add a new entry to theDirectory using method add
return null
[Code] ....
I don't know how to do any of this at all
View Replies
View Related
Jan 22, 2015
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
{
[Code]......
View Replies
View Related
Jul 8, 2014
I wrote this piece of code, and it is supposed to use bubblesort to sort the array. Yet somehow it re-aranges it, but doesn't exactly sort it. For example, when I enter [3, 5, 6, 9, 8, 4, 7, 5, 6, 2], it prints Done [2, 5, 9, 5, 7, 8, 4, 6, 3, 6]
Java Code:
import java.lang.*;
import java.util.*;
import java.util.Arrays;
public class bubble {
public static int[] array(){
int [] anArray = new int [10];
[Code]...
View Replies
View Related
Jun 29, 2014
Directions: public static void initialize(ArrayList names, ArrayList sores)
You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:
I'm having trouble figuring out how to sort the lists.
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();
[Code] ....
View Replies
View Related
Dec 15, 2014
How I would sort two heaps into one array. I know that in a heap the "parent" is guaranteed to be larger than the "children".
So could I simultaneously traverse both trees in order, and while doing so, store the values of each node, one after another? This way the array would be partially sorted and then I could just use an insertion sort.
Is this logic reasonable or am I looking at it the wrong way?
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
May 21, 2014
This is a lab for one of my CS classes, and the assignment is to create a randomly filled array (values 10-100) and use these values as the height of an array of rectangles (essentially a bar graph)that will be drawn on a page. After that's done, the code should use the selection sort method to sort the bars least to greatest, being repainted as it's sorted.
I'm receiving no errors, the original draws just fine, and the code sorts the first position and then...it just hangs. Like it's in an infinite loop but I have all of the modifiers in place (I think. I've been staring at this code for three days straight and I don't think I really see it anymore). I've tried talking to my professor and I get that her private life is really busy right now, but she just keeps blowing me off and I don't know what to do. Anyway, done with back story and whining so here's the code.
Rectangle class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Rectangle
[code]....
View Replies
View Related
Nov 23, 2014
I am having an issue trying to print different types of arrays using one method. Eclipse tells me to convert my print method for each array type:
change method 'printList <e>' to 'printList(ArrayList<integer>)'
change method 'printList <e>' to 'printList(ArrayList<Double>)'
change method 'printList <e>' to 'printList(ArrayList<Character>)'
change method 'printList <e>' to 'printList(ArrayList<String>)'
I would like to accomplish printing all four different arrays using one print method. How to accomplish it or provide examples or links to examples?
import java.util.ArrayList;
public class SortArrayList {
public static class Sort {
[Code].....
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
Feb 19, 2014
Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.
import java.util.Scanner;
class regnumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("registration number ", 100,1000);
[Code] ....
View Replies
View Related
Jan 10, 2015
I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code.
Java Code:
public class bubbleSort
{
public static void main(String []args)
{
int i;
int array [] = {12,9,4,99,120,1,3,10};
[code]....
View Replies
View Related
Feb 28, 2015
I have read through some of the posts on insert sort /arrays and see close but not exactly what I'm looking for. I don't want to use a library to do work for me, I want to do it myself and understand it.
OrderedArrayList.java
// import static org.junit.Assert.assertEquals;
public class OrderedArrayList<T extends Comparable<T>> {
// T is a variable that contains a class type, array of T things
private T[] arrayA; // changed to private per example
private T[] arrayB;
int numElements = 0;// number of elements full (not null), used in multiple methods. numElement -1 = back
[Code] ....
View Replies
View Related
Nov 19, 2014
I know it is possible to do this using the Array class but I don't want to use that. I am trying to use selection or bubble sort but I believe selection is what I want. This program asks the user to input 2 arrays with 10 integers each, sorts them from smallest to largest, then states whether they are the same or not. Comparing the two arrays.
import java.util.Scanner;
public class SetDifference
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int array1[] = new int[10];
int array2[] = new int[10];
[Code] .....
View Replies
View Related
Nov 27, 2014
I working on a bank program that gets a user id, name and balance from a text file. I need to be able to sort based on each field. I'm using Collection.sort and Comparator. I'm using String[] array = file.toFile().list(); to get the values for the array from the text file. I can't figure out how to make this into an arraylist. I understand the concept when hardcoding the data but having trouble doing it from a text file.
View Replies
View Related
Oct 22, 2014
Use the sort method of the arrays class to sort the values in the array, and print the median value(the 50th value) on the console followed by a blank line. Then, test this enhancement. Print the 9th value of the array on the console and every 9th value after that. Then, test this enhancement.
import java.util.Arrays;
public class ArrayTestApp
{
public static void main(String[] args) {
double [] arrayTest = new double[99];
//adding random number to each element in the array
for(int i=0; i<arrayTest.length; i++)
arrayTest[i] = 100.0*Math.random();
[Code] ....
OUTPUT
run:
The average is: 49.842845462514944
The median is: 49.68753724038633
The 9th value is: 2.599530043466969
The 9th value is: 11.486193141397095
The 9th value is: 20.14206270200648
[Code] ....
View Replies
View Related
Jun 30, 2014
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class SumOfFloats {
public static float sumFloats() throws FileNotFoundException{
[Code[ ....
View Replies
View Related
Dec 7, 2014
I have an array that I filled with 30 random characters, but now I am trying to sort them in ascending order and the descending order using lambda expressions.
public class RandomCharacters {
public static void main(String args[]){
Random r =new Random();
char myarray[] = new char [30];
for (int i = 0 ; i < 30; i++)
[Code] ......
View Replies
View Related