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


ADVERTISEMENT

Find Largest And Smallest Value From Array

Nov 15, 2014

I am trying to display Largest value and Smallest value in an array but when I execute I get Largest value as 0.

Following is the code:

package simpleArray;
import javax.swing.JOptionPane;
class ArrayInteractive {
static String ch = "Q";
static boolean flag = true;
static String a[];

[code]....

View Replies View Related

No Compiler Errors / Finds Largest Number In Array But Not Finding Smallest

Oct 19, 2014

A java program to print 10 random numbers between 512 and 1024 and then finds the smallest and biggest numbers from the list using only one loop

class BSLab4d {
public static void main (String[] args){
int[] nums = new int[10];
int largest = nums[0];
int x = 0;
int smallest = nums[0];
int y = 0;
 
[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

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

Find Both Largest And Smallest Of Several Integers

Apr 9, 2015

Write a program that finds both the largest and smallest of several integers. T. For example, a program that determines the winner and loser of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest; the salesperson who sells the least units loses the contest. Write a pseudocode program, then a Java program that inputs a series of 10 integers and determines and prints the largest integer and the smallest integer. Your program should use at least the following four variables: counter, number , largest and smallest. After i run the code i just get the largest input without the smallest .

This is my code :

import java.util.Scanner; // program uses scanner
// main Class begins
public class Largest12
{
public static void main(String[] args) //main method begins
{
//Initial variables
int counter = 0; // initiate counter
int number=0 ;

[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

Reverse Grades From Largest To Smallest And Corresponding Names

Jan 20, 2015

I want to reverse grades that i have put using JOptionPane from largest to smallest and the corresponding names..

import javax.swing.*;
import java.util.Arrays;
public class ArrayUtils {
public static void main (String[]args){
String length = JOptionPane.showInputDialog("Number of students");

[code]....

View Replies View Related

Make Program That Declare Largest And Smallest Out Of Three Numbers?

Jan 13, 2015

how can i make a program that declare the largest and smallest out of three numbers??

/*
* To change this license header, choose License Headers in Project Properties.
int a;
int b;
int c;

[Code]....

View Replies View Related

Program To Display Both Largest And Smallest Of A Series Of 5 Integers

Apr 27, 2014

Here is a simple program on applets that is giving me a bit of headache since it cannot always display the two numbers that it is supposed to: the largest and smallest of the five input integers.....I cannot see where i went wrong, you can always create separately the HTML file to load the applet in an appletviewer of your won size.

*
23.6 (Largest and Smallest) Write an applet that reads five integers, determines which are the largest and smallest integers in the group and prints them. Draw the results on the applet.
*/
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

[Code] .....

View Replies View Related

Display Largest And Smallest Numbers In A Series Of 10 Inputs

Apr 15, 2014

I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.

import java.util.*;
public class testhw7
{
public static void main (String [] args) {
Scannernumberin = new Scanner (System.in);
doublevalue[];
doubletotal;

[Code]...

View Replies View Related

Sorting ArrayList Of Geometric Objects By Area From Smallest To Largest

Mar 18, 2014

I have to create a method with the following header :

public static <E extends Comparable<E> > void sort ( ArrayList<E> list , int left, int right)

i also had to create a swap cells method and position of max integer method. and also had to read the preserved data file in with a scanner. I implemented the comparable interface I am having difficulty sorting my list by the area. It has to be in descending order.

Geometric Object class: since it has comparator also am interested if i need to change this?

CODE:

Driver:
public static void main(String[] args) throws IOException, ClassNotFoundException {
Circle c1 = new Circle (4, "red", false);
Circle c2 = new Circle (2, "blue", true);
Circle c3 = new Circle (10, "blue", true);
Rectangle r1 = new Rectangle (10, 6, "yellow", true);
Rectangle r2 = new Rectangle ( 5, 11, "green", true);
ArrayList <GeometricObject> list = new ArrayList();

[Code] ....

View Replies View Related

Determine If Lengths Of Three Strings Are In Order By Length / Smallest To Largest

Feb 18, 2015

Suppose s, t, and w are strings that have already been created inside main. Write a statement or statements, to be added to main, that will determine if the lengths of the three strings are in order by length, smallest to largest. That is, your code should determine if s is strictly shorter than t, and if t is strictly shorter than w. If these conditions hold your code should print (the boolean value) true. If not, your code should print false. (Strictly means: no ties)

View Replies View Related

Arrays And Number Generator - Determine And Print Largest And Smallest Values

Dec 4, 2014

/*
Purpose: To write the methods and the rest program. The program should fill a 4 X 4 2 dimensional array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate.
*/

import java.util.Scanner;
import java.util.Random;
public class LNFI_2DArray
{
public static void main(String[] args) {
int guess;
int[] array = new int[4];

[Code] ....

I just had this code working, then all of a sudden i was hit with a 'keyboard leak' error 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

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

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

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

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

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

Smallest Element In Array

Nov 7, 2014

I have been working on this assignment for a bit now. I seem to have most of the logic of it down, as far as I can tell, but I seem to have fallen into a bit of a brain lapse when it comes to invoking methods I've made in my main method. Here's the code:

package Module5;
import java.util.Scanner;
public class Exercise6Nine {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 numbers.");
double[] numbers = new double[10];

[Code] .....

My question is, in my main method, I have code in there to attempt to call my method "min" from below, and to use the result of the min's calculations and print them via my main. Only, I can't seem to properly invoke the method from my main method!

Also, if you could check over my min method and see if anything blatantly wrong is there and let me know, that would be great.

Looking over my question, and it seems there is no EDIT button, but I may as well put the assignment parameters here, since I asked for double checking on my method. Write a method that finds the smallest element in an array of double values using the following header:

public static double min( double[ ] array )

Write a test program that prompts the user to enter 10 numbers, invokes this method to return the minimum value, and displays the minimum value.

View Replies View Related

Print 2 Largest Number In Array

Apr 1, 2014

Thought process : Sort the array and print the last 2 element of any given array.

Note /| Should not use any inbuilt Array.sort()

Java Code:

//Write a code to print the 2 largest numbers from the given Array {2,8,10,5,9}

package arrays;
public class biggest2numbers {
public static void main(String[] args) {
int[] A = {2,8,10,5,9};
//Declaring 2 variable
int Max1,
Max2;

[Code] .....

View Replies View Related

Method That Returns Largest Value Of Array Of Doubles?

Jan 26, 2015

Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.

Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.

package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list

[Code]...

View Replies View Related

ArrayList - Scan Array For Largest Number

Mar 24, 2014

I need to create a program that uses ArrayList to store integers the user inputs, then scan the array for the largest number. I would also like the user to be able to exit this loop if the number 0 is entered.

As you can see below, I'm not sure how to correctly exit the do-while loop. I found this on another forum, but it does not work.

Java Code:

import java.util.*;
public class array {
public static void main(String [] args){
ArrayList<Integer> list = new ArrayList<Integer>();

[Code] ....

View Replies View Related

Trying To Find 3 Largest Numbers In Array Using Single For Loop

Feb 5, 2014

I am trying to find 3 largest numbers in an array using a single For loop but my following code is still showing randomly sorted numbers.

public class largest3 {
static int m, n, o;
static int array[] = new int[100];
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
array[i] = (int) (Math.random() * 100);

[Code] ....

View Replies View Related







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