Copying Random Generated Numbers To Selection Sort Method

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


ADVERTISEMENT

Random Array - Only 2 Of 10 Possible Numbers Are Generated

Nov 21, 2014

I am writing a program that creates an array with random numbers. Then the user can choose what number of the array he/she wants to check the occurance of. This works fine, but the numbers generated seems very weird, only 2 of the 10 possible numbers are generated. 0 and one of the other 9 numbers, the 0 is always 10100 and the other one is always 101.

import java.util.Random;
import java.util.Scanner;
public class OppgaveC {
public static void sjekk() {
int randomArray[]=new int[101];
int countArray[]=new int[10];
Random rand = new Random();

[Code] ....

it also says i have a memory leak on my scanner, ow can i close this?

View Replies View Related

Selection Sort Method?

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

Used Selection Sort Method On Array

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

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

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

Selection Sort Method - Sorting Array And Return Result Of Each Step

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

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

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

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

Graphically Displaying Selection Sort

May 20, 2014

I am trying to write a program that graphically displays a selection sort. It needs to sort bars of various heights, and the bars heights are generated from an array of random integers. The program needs to show the bars swapping as they are being sorted, I am having trouble getting the bars to draw, it needs to look like a bar graph. Here is my code thus far (not counting my boiler plate):

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.util.Random;
public class SelectionSortPanel extends JPanel

[Code] ....

Also right now it is giving an error from the compareTo method?

View Replies View Related

How To Do Selection Sort Of ArrayList By Customer Last Name

Nov 8, 2014

I have an arrayList with these values:

Mike Smith with a customer id of 100 has an account number of 1000 and a balance of $5,000.00
Hank Jones with a customer id of 101 has an account number of 1001 and a balance of $45,000.00
Sam Overstreet with a customer id of 102 has an account number of 1002 and a balance of $45,000.00
Hank Jones with a customer id of 101 has an account number of 1003 and a balance of $48,000.00
and so on .....

I am trying to do a selection sort by the account holders last name. I understand how to do if the Arraylist holds integers, but my arraylist holds multiple fields. I am not allowed to use collections as this is a homework assignment.

here is the Account Class

public class Account implements Comparable<Account> {
private int acctNum;
private double balance;
private Customer cust; // note we are putting a Customer object in the Account clas
private static int nextAcct = 1000;// used to keep track of the next available account number

[Code] ....

View Replies View Related

Graphic Array Selection Sort?

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

Selection Sort / Binary Search And Recursion

Nov 26, 2014

Here is the code I have at the moment:

/**
* Use selection sort to sort the tracks by name. Return a new, sorted ArrayList of tracks.
*
* @return an ArrayList containing the tracks sorted by name, or null if no tracks exist
*/
public ArrayList<Track> getTracksSortedByName() {
// YOUR CODE HERE
if(tracks == null)
return tracks;

[Code] ....

Here are my 2 issues (there are 2 lines with compiler errors): I feel like I understand selection sort and binary search, but am not sure how to apply it to the more abstract idea of a Track ArrayList (hence the 2 compiler errors). What should I use to make it work. Lastly, I'm very uncomfortable with recursion, so my guess is there is also probably some logical issue with it in the getTracksSortedByName method.

View Replies View Related

How To Use Selection Sort For Array From Smallest To Largest

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

Passing Parameters - Randomly Generated Numbers Not Appearing

Dec 14, 2014

For some reason, when I test out my code, my randomly generated numbers don't appear. Here is a sample result:

> What do you want to generate, integer, double, or character?

>integer

>What is the upper limit and lower limit of the integers you want to generate?

>1

>10

>How many integers do you want to generate?

>10

>BUILD SUCCESSFUL (total time: 9 seconds)

Is this because my code is not passing my parameters correctly? I'm not sure how to fix this either.

Here is my code for reference (it's not completed at the moment)

import java.util.Scanner;
public class NewNumberCharacter {
/** Main method
* @param args */
public static void main(String[] args) {
int return_int;
double return_double;

[Code] ....

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

Print 2 Lists - 20 Random Numbers And Another List Without Duplicate Numbers

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

Generate Random Numbers Without Certain Numbers In Range

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

How To Sort Random Char Array Using Lambda Expression In Order

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

Sort Numbers Separated By Space

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

Store Numbers In Array Then Sort

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

Sort Numbers Separated By Space Rather Than Lines

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

Sort 5 Numbers In Java From High To Low With If Statements?

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

Selection Of Invocation Of Overridden Method During Constructor Chaining

May 28, 2014

I don't understand, why when in the constructor of the superclass A the method init() is called (line 4), the overridden version of the subclass is invoked (line 26) and not the superclass version (line 7):

class A {
public A () {
init();
}
public void init() {
System.out.println("test");

[Code] ....

I would have guessed that above code prints

test
1

But instead you get a NPE (because when the constructor of B is invoked

public static void main(String[] args) {
new B();
}

Then there is first the implicit call to super:

public B() {
s = " ";
init();
}

Which is the constructor of A:

public A () {
init();
}

But here now this init() method is not the version of A ( public void init() {
System.out.println("test");
}) but the overriden version of the subclass (B): public void init() {
System.out.println(s+=s.length());
}...

Which throws an NPE of course, because the initialization of s has not occured yet (it happens only after the implicit call to super() has finished (see public B() {
s = " ";
init();
}))

View Replies View Related

Read Float Numbers From A File / Put Them In Array And Sort

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

For Loop With Random Numbers?

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







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