Sorting Arrays And Counting Number Of Swaps

Nov 2, 2013

I need to modify modules used in the book that run a bubble sort, selection sort, and insertion sort on an integer array such that each module keeps a count of the number of swaps it makes.

How do I code for this?

Then we have to design an application that uses 3 identical arrays of at least 20 integers. That calls each module on a different array, and display the number swaps made by each algorithm.

View Replies


ADVERTISEMENT

Sorting 10 Double Arrays

Sep 17, 2014

I'm trying to sort 10 inputted numbers (double precision) using the Array.sort() method. I can get the 10 numbers inputted, but the output is ten 0.0s; now (and this how I know I am learning some things) I'm fairly certain that the variable number is not storing the numbers inputted by the user otherwise I wild be seeing the program work correctly.

So my question is why isn't number storing the inputs?

import javax.swing.JOptionPane;
import java.util.Arrays;
public class KrisFrench3 {
public static void main(String[] args) {
double[] number = new double[10];
for(int i = 1; i <= i; i++) {

[Code] .....

View Replies View Related

Stuck Sorting Arrays

Jul 1, 2014

Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3,22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls is Sorted(list1) and isSorted(list2)should return false and true respectively. Assume the array has at least one element. A one-element array is considered
to be sorted. public class thirfd {

public static void main(String[] args) {
double[] arr1 = {16.1, 12.3,22.2, 14.4};
double[] arr2 = {1.5, 0.3, 7.0, 19.5, 25.1, 46.2};
isSorted(arr2);
System.out.println(isSorted(arr2));

[code]...

View Replies View Related

Searching And Sorting Arrays

Nov 2, 2014

My code is not working properly. The ascending and descending numbers are not showing up. I believe what it is printing is the memory location. In this lab you will be coding a program that will make use of functions to search and sort an array. There will also be a print method, again complete with a full menu system. The Menu options are listed below in the section labeled menu.You will need to set up a hundred (100) position integer (int) array that is defined in main. You will also need an integer (int) variable called size. By doing this, you will have to pass the array and the size to each method you write.

Menu:

The menu should have the following eight options:
1. Fill the array with random numbers (1 -100)
2. Print the array
3. Sort the array in ascending sequence
4. Sort the array in descending sequence
5. Sequential search of the array for a
6. Binary search of the array for a target
7. Exit (this can be option zero if you prefer)

From these seven Options, one can see that six methods will be needed. Each of the six main functionalities above will need a function that does what they say. When printing the array, it is required to print the position number alongside the value. Please start your positions at zero, and not one. When doing the sorting methods, please use two different sorting algorithms. (ie, use a Min Max sort for ascending and an enhanced bubble for descending.)

For the Searching methods: you should ask for the target (number the user is searching for) in the dispatch method. Then pass the target to the search method. The search method should return the position it was found at (0 - Size) OR -1 if it was not found. Then have the appropriate messages print inside of the dispatch method.You could write another function that does this part if you wish to keep your dispatch method cleaner and more organized. But that is up to you.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int [] values = new int [100];

[code]....

View Replies View Related

Eclipse - Sorting BigInteger Arrays

Mar 23, 2014

I am currently working on Project which involves me needing a BigInteger array and sorting it somehow. I have been able to do

* Array.sort(arrayname); *

In past codes, but Eclipse is telling me that I can't do this with my BigInteger array. I have already imported java.util.Arrays

View Replies View Related

Sorting Arrays In Descending Order With Collections Class

Jun 28, 2014

I am trying to create a java program to sort an array in ascending and descending order. Here is the program I created :

import java.util.Arrays;
import java.util.Collections;
public class ArraySort
{
public static void main(String [] args) {
int [] num = {5,9,1,65,7,8,9};
Arrays.sort(num);

[Code]...

BUT I GET THE FOLLOWING EROOR ON COMPILATION

ArraySort.java:12: error: no suitable method found for reverseOrder(int[])
Arrays.sort(num,Collections.reverseOrder(num));
^
method Collections.<T#1>reverseOrder(Comparator<T#1>) is not applicable

[Code] .....

What's wrong with my program?

View Replies View Related

Sorting User Input Arrays Into Ascending Order

Oct 15, 2014

I am writing a program that grab user input number which represent beats per minute separated by commas. It then parses the numbers and reorders them from smallest to largest and then outputs the average, medium, maximum and minimum number all in separate lines. I am having trouble getting the array to sort the input from smallest to largest. It is currently only working for 3 numbers inputted. Anything more will not reorder it.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package heartrate;
import java.util.Scanner;
import java.util.Arrays;

[Code] ....

View Replies View Related

Arrays With User Input - Sorting Information Correctly?

Oct 12, 2014

I am new to using arrays. I need to collect user input for book title, author, and # of pages... store that in an array... and then I'm going to need to be able to sort that array. The dialog boxes come up prompting the user for 5 sets of title, author, and # of pages, but when I try to display that information, it isn't working. I am assuming this means that it's not storing the information correctly... so I want to get this corrected before I even try to sort??

import javax.swing.*;
import java.util.*;
class LibraryBookSort
{
public static void main(String[] args)
{
LibraryBook[] someBooks = new LibraryBook[5];

[Code] ....

View Replies View Related

Sorting Arrays - Order From Greatest To Smallest Based On Frequency

Feb 12, 2014

These are all arraylists and not arrays

I have currently two arrays.

Java Code:

String[] chunks = {"a","b","c"};
int[] freq = {2,4,3}; mh_sh_highlight_all('java');

I am looking for a way to order these from greatest to smallest based on frequency.

The resulting arrays should be:

Java Code:

chunks = {"b","c","a"};
freq = {4,3,2}; mh_sh_highlight_all('java');

Because b is most frequent, and a is least frequent.

One way I can think of doing this is a two dimensional String array

Java Code: String[][] fullHold = {{"b","c","a"},{"4","3","2"}}; mh_sh_highlight_all('java');

Then sort based on the character values of the second row.

How can I sort a two dimensional array based on a single row (where it will rearrange the other rows in accordance).

View Replies View Related

Counting Number Of Paragraphs In A Text?

Apr 19, 2014

How do I count the number of paragraphs using a separate method?

import java.util.*;
import java.io.*;
public class WordStats1 {
public static void main(String[] args) {
try {
Scanner input = new Scanner(new FileReader("data.txt"));
PrintWriter output = new PrintWriter(new FileOutputStream(

[code].....

View Replies View Related

Counting Number Of Steps Throughout The Circle

Jan 27, 2015

The method public static int steps(int posts, int stride) calculates how many strides can be taken to get back to posts. Let's say if the method is (12, 4), it takes only three steps. Now let's say the method has parameters (12,5), so it should be (5, 10, 3, 8, 1, 6, 11, 4, 9, 2, 7, 12). My method works for such examplse as (12, 4) or (12,3) or (6,2)... but how can I figure out (12,5)?

Java Code:

public static int steps(int posts, int stride) {
int countSteps = 0;
int result = 0;
do {
result += stride;

[Code] ....

View Replies View Related

Counting Number Of Paragraphs In A Text

Apr 19, 2014

Why my code wont compile the correct number of paragraphs .

import java.util.*;
import java.io.*;
public class WordStats1 {
public static void main(String[] args) {

[Code] ....

View Replies View Related

Counting Number Of Words From User Input

Jan 2, 2015

I tried the following code, OK:

Java Code:

String temp = "hi this sf hello is new what is this";
String[] cmd = temp.split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

However, when i get the input from user , i didnt get the expected result:

Java Code:

System.out.println("Enter the input string to count the words: ");
String[] cmd = new Scanner(System.in).next().trim().split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

Now Result for above code:

Enter the string to count the words:

hi this is new

words are: 1

View Replies View Related

Counting Number Of Bits For Char Representation?

Mar 14, 2014

i need to show that there are 16 bits for a char variable.

i tried converting it to an int and then apply the bitCount method but that gave an output of 3.

what method should i use?

View Replies View Related

Counting Total Number Of Searches - Calling Price Method

Apr 23, 2015

How would I program a counter that will return the total number of search, failed searches and correct searches. The second thing is how can a return the price of the search methods with out calling for it specifically. Here is my current code.

package stu.paston.program7;
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
InventoryClass productData= new InventoryClass();

[Code] ....

View Replies View Related

I/O Arrays - Count Number Of Repetitiveness In String For The Number

May 19, 2014

//read the file
//make the numbers 1 string line
//count the number of repetitiveness in the string for the numbers
//display four lowest ones

import java.io.*;
import java.util.*;
public class Lottery2

[Code] ....

when I run it the array gets sorted but how do i keep the data in other words

what it is supposed to do is grab numbers from a file, and give me the lowest 4 numbers back. I know the numbers using the array but how do i pull out the lowest 4 and keep the data true no matter what numbers or how many of them are in the file.

View Replies View Related

Read Picture And Print Out Number Of Blocks - Counting In Multidimensional Array

Dec 19, 2014

I have to write a program that will read a picture and then print out the number of blocks inside it.

I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.

- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.

We only take into account the horizontal and vertical adjacency but not diagonal.

INPUT:

In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.

The OUTPUT only print the number of blocks in the picture.

Example:

INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100

OUTPUT:
6

THIS IS WHAT I CAME UP SO FAR:

import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();

[Code] ....

View Replies View Related

Sorting Objects By A Number Of Fields

Mar 17, 2014

I am trying to find a concise way to write the sort methods for my class. I am supposed to make a program that can sort objects by a number of fields: year, rank, artist and title.

I used an idea from this thread : java - Sorting a collection of objects - Stack Overflow

And I am trying to use the custom comparator for my sort methods. However for some reason, the sortingBy variable fails to recognize any of the enum types.

Whenever I try to set the sortingBy variable equal to one of them, for example:

Java Code:

private Order sortingBy = Year; mh_sh_highlight_all('java');

I get a "Year cannot be resolved to a variable" error.

What I want to be able to do is make it so every time a specific method is called, say, for example sortTitle(), sortingBy will change to Title, then the SongComparator will sort using the case Title.

Is it possible to do this? I can't figure out how to modify SongComparator's object variables that way.

Java Code:

import java.util.Comparator;
public class SongComparator implements Comparator<Song> {
public enum Order {Year, Rank, Artist, Title}
public Order sortingBy;

[Code] .....

View Replies View Related

Number Sorting Having Prefix Letter

Dec 24, 2014

I had a list of some numbers having a prefix A like A12 , A55, A76 ,A111 ,A888, A88 ,A880 A111 , A11,A1

I need to sort this list so the result would be A1,A11,A12,A55,A76....

How to do this. Can I use arrays.sort method to achieve the same or any other way.

View Replies View Related

Passing 2 Sub Arrays Into Sorting Thread Then To A 3rd Thread Merge

Oct 17, 2014

im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,

Runnable InSortlist1 = new sortList(data2p1);
Runnable InSortlist1 = new sortList(data2p1);
Thread th1 = new Thread (IntSortlist1);
Thread th2 = new Thread (IntSortlist2);
try
{
th1.start();
th1.join();

[code]....

View Replies View Related

Sorting Rows (lower To Higher Number) From A Group Of Integers

Apr 24, 2015

I have to organize the rows from the lower to the higher number, the problem is, what I have only organizes the first 3 rows even If I insert 3,4,5...or 10 rows.

int arg[][] = {
{26, 39, 3, 13},
{22, 97, 17, 123},
{46, 19, 63, 123},
{1, 37, 90, 32},
{17, 37, 90, 32}};

for (int i = arg[0].length - 1; i >= 0; i--) {
for (int j = 0; j < i ; j++) {
for (int k = 0; k < i ; k++) {
if (arg[k][j] > arg[k][j + 1]) {
int temp = arg[k][j];

[Code] ....

View Replies View Related

Random Number Generation And 2D Arrays

Dec 7, 2014

The problem you have is that it doesn't produce a 4x4 array. For that you need more nested for loops.This is what I have tried so far:

import java.util.Scanner;
import java.util.Random;
public class challenged2e
{
static Scanner console = new Scanner(System.in);
public static int i=0;
public static void main (String args[])

[code]...

I get 21 errors and they all have to do with i and j.

View Replies View Related

How To Reduce Number Of Parallel Arrays In Code

Mar 8, 2015

I'm building a text based "game" where you are communicating with this android creature called Gargoid , at a VERY primitive level . how it works is, simply the user type in a sentence which is decoded for meaning by comparing it with a built in list of words in order to figure out what the user is saying, and then reply with a a relevant response also from a list of built in words. the whole thing would look something like this,

user: what is your name
Gargoid : my name is Gargoid, nice to meet you
user: how is the weather
Gargoid: the weather is wonderful

so far I have 11 arrays which are the following

String[] for user typed in words used for comparison to find meaning ..An Array of String[] , 7 so far, to hold what I call the Gargoid dictionary for example String[] greeting={hi,hello,aloha}, words that indicates greeting int[] called frequency to determine which of the 7 arrays have the greatest "relevancy" to what is being said. and finally another String[] for responses here's the actual code, I want you guys to tell me if there's a way to reduce all this never ending number of arrays? and also is this code a good application of object oriented programming?

MainClass
public class GargoidMain {
public static void main(String[] args) {
TheKeyBoard keyboard=new TheKeyBoard();
TheTranslator translator=new TheTranslator();
TheBrain brain=new TheBrain();
translator.translate(keyboard.userSaysWhat());
brain.respond(translator.userSays());

[code]....

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

Tic Tac Toe Game - Random Number Generator And Two Dimensional Arrays For Game Board

May 9, 2015

Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.

import java.util.Random;
import java.util.Scanner;
 public class TicTacToe {
 private static Scanner keyboard = new Scanner(System.in);
private static char[][] board = new char[3][3];
public static int row, col;
 
[Code] ....

View Replies View Related

Parallel Arrays - User Input Integer 1-12 / Output Name Of Month And Number Of Days In That Month

May 11, 2015

I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far

import java.util.*;
public class DaysMonth
{
public static void main (String args[]) {
Scanner keyIn = new Scanner(System.in);
int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

[Code] ....

After I enter the user int it just ends.

View Replies View Related







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