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
ADVERTISEMENT
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
Apr 7, 2014
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;
[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
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
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
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
Feb 1, 2015
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){
System.out.println("Twenty random integers: ");
int max = 20; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int[] all = new int[20];
[Code] ....
View Replies
View Related
Jul 31, 2014
I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?
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
Feb 10, 2015
I am working on my homework, and everything is fine. But I was wondering if I can fix one thing. basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{
static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
System.out.print("Enter the first name of the file: ");
String input = kb.readLine();
int[] firstArray = readFile(input);
firstArray = bubbleSort(firstArray);
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
Feb 10, 2015
Basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{
static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
System.out.print("Enter the first name of the file: ");
String input = kb.readLine();
[Code] ....
As you you may figure, it works if I put numbers separated by each lines. I was wondering if I can change it so that it works when I put numbers separated by space rather then each lines.
View Replies
View Related
Sep 16, 2014
I am trying to code a program that orders 5 random numbers from high to low with the basic coding i know (im starting to learn theory of methods... so you can imagine) When i run the code i cant get all 5 numbers ordered but my logic says the code is right although it's pretty confusing.
I know you could code in a simpler way but first i wanna get it as it is right now. When i debug (on my own way cause i dont know how to actually use it) shows line 72 with yellow color.
public class NuevaCalculadora {
import java.util.Scanner;
public static void main(String[] args) {
[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
Jul 5, 2014
Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3,inclusive.The numbers represent the type of vehicle as stated below:
NumberVehicle Category
1Car
2Motor bikes
3Trucks
Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle?
The answer should be something like
Number of cars = X
Number of motor bikes = Y
Number of Trucks = Z
but i'm getting
Total number of vehicle:
cars
motorbikes
motorbikes
cars
Trucks
Trucks
motorbikes
motorbikes
Trucks
cars
public static void main(String[] args) {
[code]....
View Replies
View Related
Feb 4, 2015
I have a class project working on that is supposed to ask the user for input and create a random set of #'s in 4 rows. However it compiles but doenst run At one point Eclipse was telling me Scanner not closed?
import java.util.Scanner;
public class Matrixbuilder {
// input a number that is used to build a matrix. ie = 4 rows and 4 colums
//get the number
//build the matrix of random integers
//print the matrix
[code]...
View Replies
View Related
Mar 2, 2014
I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not.
public class DoLottery {
public static void main (String args[]) {
int max= 10;
int random;
int random2;
int random3;
[code]...
I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.
View Replies
View Related
Jun 17, 2014
Writing a Java program to find sum of numbers in a random string.
(Input=abc235^%$q!12&3 output=250)
If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)
View Replies
View Related
Jan 13, 2014
I've been trying to find the easiest way to write to generate a number which is between intervals of a arbitrary min and a max value.I've been searching for this but I don't find this particular thing.I've found that this combination works:
Java Code: int guess = rand.nextInt(max - (min - 1)) + min; mh_sh_highlight_all('java');
But I wonder, is this really the easiest way of writing it?
View Replies
View Related
Nov 10, 2014
For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?
View Replies
View Related
Feb 12, 2015
I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=(int)(Math.random()*(9-0));
int num2=(int)(Math.random()*(9-0));
int num3=(int)(Math.random()*(9-0));
[Code] ....
View Replies
View Related