Determining Average Number Of Comparisons For Quicksort?

Feb 24, 2014

My objective is to execute quick sort ( i was told to convert the pseudocode from the Cormen book) using arrays of increasing sizes and find the average number of comparisons for each of those sizes over 100 iterations. This is a school project and the numbers I am getting are far larger than those of my friends, so I am clearly doing something wrong. I believe it must be in the way that I am collecting and averaging my number of comparisons. I will first give the method in which most of that calculating is done, then I will include the whole program.

public static void tests(int arraySize) {
long numComparisons = 0;
long averageComparisons = 0;
long[] numComparisonsArray = new long[100];
  for(int i = 0; i<100; i++) {
int[] array= genRandomArray(arraySize);

[code]....

I was simply not zeroing out one of my variables

View Replies


ADVERTISEMENT

Determining User Input To Be Prime Number

Feb 12, 2015

Any better way to write a program that takes a user number input and the program determines whether or not the number is prime or not. It was suppose to be a number between 0 and 8,000,000.

import java.util.Scanner;
public class prime1
{
public static void main(String args [])

[Code].....

View Replies View Related

Number Closest To Average Sum For Given Array

Oct 19, 2014

Find the numbers which is closest to the average sum for a given array of N (1<=N<=50) of natural numbers. If there are two numbers who meet the requirement, return the smaller of the two.

For example for the array of: 1,2,3,4,5,6 the average sum is 3.5, so both 3 and 4 are the closest to that, but the program has to return 3, because it's smaller than 4.

The array can also contain duplicates. First we type the number of elements in the array, then in each line we add the numbers. Name of the class: Array

**Note**: Create a data structure array and use it.

And this is what they have given to me, I just need to type the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Array<E> {
public static int closestNumber( ) {

[Code] .....

View Replies View Related

Calculate SUM And AVERAGE Of ODD Number Digits Between 25 And 75

Jun 19, 2014

You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive).

The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.

The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately

a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.

View Replies View Related

Average Program - Number Format

May 5, 2015

I am writing a program using a given test drive... I am supposed to write two classes according to the test drive. The calculated correct answer should be 115.50 but i keep getting 115.52 instead and i just can't figure out what i'm doing wrong.

Here is the test drive code:

import java.text.NumberFormat;
public class FinalTester
{
public static void main(String[] args)
{
double avgPurchase;
Order[] lastYear = new Order[4];

[Code] ....

View Replies View Related

Find The Column With Highest Average Number

Feb 14, 2015

code a 10 * 10 matrix, with random numbers from 1 to 100,

i) Find the most repeated number.
ii) Use the Binary Sort, to sort the matrix in ascending order.
iii) Find the Column, with the highest average number.

View Replies View Related

Math On Three Numbers - Get Average / Sum And Number Entered By User

Feb 25, 2015

Java program : For this assignment you are going to write code for the following class:

MathOnThreeNumbers

Here are the specifications of class MathOnThreeNumbers:

Methods of class MathOnThreeNumbers:

1. inputThreeNumbers
2. getAverage
3. getSum
4. getNumberOne
5. getNumberTwo
6. getNumberThree

Constructor of class MathOnThreeNumbers

write a constructor that initializes the first, second, and three numbers to 1, 2, and 3 respectively.

Specs for the methods methods:

1.
name: inputThreeNumbers
accessibilty: public
arguments: none
what it does: asks the user for three numbers of type double

2.
name: getAverage
accessibilty: public
arguments: none
what it does: returns average of the three numbers

3.
name: getSum
accessibilty: public
arguments: none
what it does: returns sum of the three numbers

4.
name: getNumberOne
accessibilty: public
arguments: none
what it does: returns the first number entered by the user

5.
name: getNumberTwo
accessibilty: public
arguments: none
what it does: returns the second number entered by the user

6.
name: getNumberThree
accessibilty: public
arguments: none
what it does: returns the third number entered by the user

Here is an example of how the class MathOnThreeNumbers works. The following code produces the output displayed after the code.

MathOnThreeNumbers mm = new MathOnThreeNumbers();
System.out.println("first: " + mm.getNumberOne());
System.out.println("second: " + mm.getNumberTwo());
System.out.println("third: " + mm.getNumberThree());
mm.inputThreeNumbers();

[Code] .....

View Replies View Related

Parallel Program - Calculate SUM And AVERAGE Of Odd Number Digits Between 25 And 75

Jun 11, 2014

You are to write a parallel program that will receive a large array of unsorted integers containing digits from 0 to 99. The program will have to calculate the SUM and AVERAGE of ODD number digits between 25 and 75 (inclusive). The user may specify the size or the array and the total processor that the machine has. The size of the array must be divisible by number of processor. Based on the given size, the computer will generate random integer numbers automatically to populate the array.

The program should display the status of the calculation for each processor. The result should be displayed after all calculations are completed.Error messages should be displayed appropriately

a) Write a sequential (non-parallel) program that will accomplish above task.
b) Write a concurrent (parallel) program that will produce the result of the above task.

View Replies View Related

Determining Big O Notation

Apr 12, 2014

Compute the total number of operations for each of the following algorithm and define the value of Big Oh in its worst-case.

i.int jum = 0;
for (int x = 1; x<= n; x++) {
for (int y = 1 ; y <= 10; y++) {
for (int k = 0; k<= n*n; k++) {
jum = jum * x + y;

[Code] .....

What it actually means by compute the total number of operation ? My answer is

(i) 2 assignment , 1 addition and 1 multiplication
(ii) 4 assignment , 1 multiplication , 2 addition

The value of Big O is

(i) 10n^3 , so the answer is n^3
(ii) n^4+1 , so the answer is n^4

May i know is my answer is correct ??

View Replies View Related

Determining When JAVA Thread Is Finished

Jul 27, 2014

I have been new to JAVA programming language and have been using it for generation of a GUI (with NET BEANS).

My problem is, when i start a thread within GUI, that takes some amount of time to get finished (around 5min to 20min cpu time),

I need an a way to detect when the thread is done and accordingly some action to be taken afterwards.

I tried with isAlive, but to no avail.

Example is, I press an JButton, thread starts, when finished Jbutton get colour changed.

View Replies View Related

Determining Letter Grade Of Students

Apr 14, 2014

The purpose of this project is to determine the letter grade of students. The program needs to accept two command line arguments:

The first being the name of a disk file that contains the names of students, and their test scores, separated by commas followed by one or more spaces. Each line in the file will contain scores for one student.

The second argument is the name of an output disk file. The program is supposed to create a new output disk file using that name.

The number of students in the input file is unknown during compile time. The name of input and output files could be anything and only known during run time. Additionally, the average scores, along with the minimum and maximum scores for each test are to be displayed in the console.

Calculation: Final Score = quiz1 * .10 + quiz2 * .10 + quiz3 * .10 + quiz4 * .10 + midi * .20 + midii * .15 + final * .25
Final Score >= 90% then letter grade is A, 80%-89% B, 70%-79% C, 60-69% D, <= 59% F

input_data.txt:

firstName lastName, 100, 90, 80, 100, 89, 99, 88
firstName lastName, 90, 90, 100, 100, 99, 100, 95
firstName lastName, 100, 90, 100, 70, 78, 78, 80
firstName lastName, 80, 90, 90, 100, 89, 99, 85
etc.

output_data.txt

firstName lastName: B
firstName lastName: A
firstName lastName: F
firstName lastName: B
firstName lastName: C

averages (to appear in console)
Q1 Q2 Q3 Q4 MidI MidII Final
Average: 82.25 80.38 82.85 83.88 81.38 84.13 78.63
Minimum: 60 54 38 62 62 60 50
Maximum: 100 90 100 100 99 100 95

Press ENTER to continue...

Here's what I have so far :

import static java.lang.System.out;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
public class LetterGrader {

[Code] .....

View Replies View Related

Hash Key Comparisons For Two Objects

Apr 17, 2014

I want to generate a Hash key for a [ Nested Hierarchical nodes]. Any lightweight non-cryptographic hash map functions which generate a key, so that I can use this hashkey to comparison purposes.

NOTE: Security is not a concern for me, I just want to create a Unique ID.

View Replies View Related

Date Comparisons Not Working

Sep 24, 2014

I am having a hard time getting some comparisons with Date to function correctly.I have a driver program and two classes, ZodiacDriver , ZodiacSign and ZodiacTable. In the driver program I build an arraylist of zodiacsigns in zodiactable. That all appears to be working I then pass a birthdate and the zodiac table to a method, determineSign. This is where I am having trouble. Right now Im just trying to match the incoming birthdate with a zodiac's date in the table. however no matter what date I enter, i get pisces, which happens to be at the bottom of the list. If I remove pisces from the list I get the current last zodiac. Here is my code:

ZodiacDriver
Java Code: package zodiac;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

[code]....

View Replies View Related

Pseudocode For Quicksort Algorithm

May 6, 2015

I have been given this Pseudocode for the Quicksort algorithm:

quicksort(a[], p, r)
if r>p then
j=partition(a[], p, r)
quicksort(a[], p, j-1)
quicksort(a[], j+1, r)

[code]....

I was wondering why:

- why the condition left < right needs to be true for the while loop in the function Partition
- what the while loop does in the function Partition

View Replies View Related

Quicksort Implementation With Median Pivot

May 19, 2014

I am currently writing the Quick Sort implementation where the pivot is chosen to be the medianOf3 element in the sub array. The program should output the total number of comparisons (excluding the ones needed to compute the median itself).I cannot spot any mistake anywhere - yet I am getting the wrong output in the end.

Input file (100.txt) attached.
Current output: 513. Correct output: 518. Where are the missing 5 comparisons? />

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

[code]....

View Replies View Related

String Comparisons / HashSet And Duplicates

Apr 20, 2014

I have a HashSet, which I created to prevent duplicates upon output, but of course it's printing duplicates(or else I wouldn't be posting this). The order of my output does not matter, nor the input. The data type is String in the format (x + "," + z), where x and z are integers, creating a collection of coordinate sets. So to prevent the output of duplicates, I'm trying to get rid of the duplicates before they are added to the collection.

I've tried doing a '.equals()' string comparison but what happens is, since my string is added via one variable, it compares itself to itself and if itself equals itself it won't be added to the collection. I really need to keep this as a comparison of a single variable, because creating a key for each value would be sooo ridiculous for this volume of inputs.

So, with that being said, I would like to add one copy of the string, discard the duplicates, and do this thousands of times..

View Replies View Related

Count Comparisons And Assignments In Three Sorting Algorithms

Feb 5, 2015

I have three sorting algorithms in which I must count the number of swaps/copies and comparisons. I am meant to only count the swaps and comparisons that involve anything other than indexes as they are too fast to really matter (according to the professor). My counters are in the right position or not. I keep coming up with swaps/comparisons that don't necessarily match the formulas I'm finding for best/worst case. Makes me think that my counters are somehow out of place or that I don't have enough of them.

Insertion Sort:

public class InsertionSort {
public static void insertionSorter(int[] array) {
int firstValue; // first value in array
int scan; // scan array through the array
int moves = 0; // number of moves

[Code] ....

View Replies View Related

Characters From String In Alphabetic Order Using Quicksort

Oct 1, 2014

I am trying to do a program that takes all of the chars from a string and orders them in alphabetical order. It works fine, but when a is a last letter of a string it isn't being sorted.

Example: bcba = bbca, omnibus = bimnous (here u is in wrong place)

Here is my code:

public class sorty{
public static void sort(char[] a, int low, int high){
int i = low;
int j = high;
if (j - i < 2) return;
int m = (j+i)/2;
char p = a[m];

[Code] .....

View Replies View Related

Java QuickSort Stack Overflow Error

Jan 19, 2014

I have an error using quicksort and this is a project ... The error occurs for numbers such as 7500 and bigger ...

Exception in thread "main" java.lang.StackOverflowError
at QuickSort.QuickSort(QuickSort.java:45)
at QuickSort.QuickSort(QuickSort.java:46)
at QuickSort.QuickSort(QuickSort.java:46) ...

Java Code:

import javax.swing.JOptionPane;
public class QuickSort{
public static void main(String[] args){
int p=new Integer(JOptionPane.showInputDialog("Jepni numrin e kufizave: "));
int[] ListaNumrave= new int[p];
//QuickSort Zbrites

[Code] .....

View Replies View Related

Count Comparisons In Java Quick Sort

Oct 3, 2014

I have to count the comparisons in the quick sort. I have done it by using a global variable. But I am stuck in doing it recursively.

This is my code.

import static java.time.Clock.system;
import java.util.Arrays;
public class test {
/**
* The swap method swaps the contents of two elements in an int array.
*
* @param The array containing the two elements.
* @param a The subscript of the first element.
* @param b The subscript of the second element.
*
*/

[Code] .....

View Replies View Related

Quicksort With Single Linked List / 1 Node Parameter

Nov 12, 2014

I have a custom linkedList(single) class that uses the provided node class. Now I have another class to QuickSort this.(left out for brevity, i just wanna focus on editing the L.head). However, instead of passing the quicksort method the entire linkedList, I want to pass it just the head from the linkedlist.

My problem is accessing this head node and changing it from the quckSort method/class, and I dont want to delete it or simply just change the element value

Main:

public class TestLinkedList {
public static <E extends Comparable<E>> void main(String[] args) {
MyLinkedList<Integer> L = new MyLinkedList<Integer>();
L.add(3);
L.add(1);
L.add(2);
System.out.println("Initial=" + L);
MySort.quickSort(L.head);
System.out.println("After ="+L);
}
}

QuickSort:

public class MySort {
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {
list = list.next;
}

Node Class:

public class MyNode<E extends Comparable<E>> {
E element;
MyNode<E> next;
public MyNode(E item) {
element = item;
next = null;

[code]....

View Replies View Related

GPA And Average Program

Mar 25, 2014

I am new on this and I will be working in a gpa and average program. This is my code so far: Every time that I try to add the gpa part it gives me errors or just duplicates whatever I've in average and displays it in gpa

/**
* This program shows a database of students in a school.
* It gathers information as name, classes, grades, and calculates average and gpa average.
*
*/
import javax.swing.JOptionPane;
 
[code]....

View Replies View Related

Compute Average Of The Sum

Dec 30, 2014

I'm having trouble compiling this program. It is supposed to compute the average of the sum:

import java.util.Scanner;
public class ComputeAverage
{
public static void main(String[] args) {
Scanner bucky = new Scanner(System.in);
double fnum;
double snum;

[Code] ......

View Replies View Related

Java Odd And Even Average

Apr 22, 2014

In the class, you will have three instance variables,

1) array to hold the numbers,
2) the odd average, and
3) the even average.

The constructor should accept as input the array from the tester class and assign it to the instance variable. You should then have a method, findAverage, that will read the array and then calculate the average of the even input values and the average of the odd input values. (Hint: You can use % (mod) to determine if a number is even or odd). Your program will need a getOddAverage and a getEvenAverage method.

public class oddEvenAverage {
int [] myArray = new int[10];
public double oddAverage;
public double evenAverage;
public oddEvenAverage(int number){
int myArray = number;

[Code] .....

What am i doing wrong here? I can't get it working!

View Replies View Related

How To Get Average Of Strings Using Array

Apr 21, 2014

I'm trying to calculate the average of grades that a user puts in. Here is my code so far:

public static void main(String[] args) {
Scanner input = new Scanner (System.in); 
System.out.println("Please enter an array of grades seperated bt a comma.");
input.nextLine();
String arrayOfGrades = "100,50,100";
String[] grades = arrayOfGrades.split(",");
 
[Code] .....

I think I'm on the right track, the only big error I'm really getting is the line: sum += grades[i]. It's saying string can not be converted into a double.

View Replies View Related

Calculating Average Of Values

Apr 21, 2014

I'm struggling with this assignment I was given:

Given list of positive integer values, write a program to calculate average of the values. List terminates with -1.

View Replies View Related







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