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? />
So first of all we use textpad in our java course (idk why) and the Easy.In method for our input. Any way I have this assignment and we're ask to make a program that input
1) how many numbers were input 2) the largest value 3)smallest value 4) average value and 5) middle/median value.
I manage to figure out the code for the other except the median value. Here's my code
class project2 { public static void main(String[] args) { System.out.println("How many numbers you want to input"); int maxItems = EasyIn.getInt();
I'm trying to find the median of a set of numbers inputted into an array and I wanted to know how I would pass the array to the median calculator after I use selection sort to organize the numbers.THIS IS THE CODE THAT ORGANIZES THE ARRAY:
public void selectionSort(double[] myArray) { for (int i = 0; i < myArray.length - 1; i++) { int index = i; for (int j = i + 1; j < myArray.length; j++)
if (myArray[j] < myArray[index])
[code]....
The code that finds the median will only work if the array being used is already organized. How do I pass the new sorted array to the code that finds the median?
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];
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);
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
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;
a simple Java program for finding the median value in a list of values with the following requirements:
- Create an array with an even number of values in it (an odd number of values is little bit trickier, so if you want a challenge, do it for either an even or odd number of values)
- Find the value with an equal number of values greater than the value as there are values less than the value
- Your solution must not require a sorted list of values - Output the median value
This assignment is intended to get you to demonstrate basic knowledge of arrays, and to create methods with both input and output.
Use the sort method of the arrays class to sort the values in the array, and print the median value(the 50th value) on the console followed by a blank line. Then, test this enhancement. Print the 9th value of the array on the console and every 9th value after that. Then, test this enhancement.
import java.util.Arrays; public class ArrayTestApp { public static void main(String[] args) { double [] arrayTest = new double[99]; //adding random number to each element in the array for(int i=0; i<arrayTest.length; i++) arrayTest[i] = 100.0*Math.random();
[Code] ....
OUTPUT
run:
The average is: 49.842845462514944 The median is: 49.68753724038633 The 9th value is: 2.599530043466969 The 9th value is: 11.486193141397095 The 9th value is: 20.14206270200648
The only problem I have now is getting a method to return the median element of a LinkedList without using loops of any kind or by using a global counter anywhere.
I've pretty easily figured out how to get the index value for the median number (there is some lee way allowed. If the list has an even size, any of the middle values are accepted) but I can't figure out how to print it without loops.
I'm sure I need to make a method that finds an element at the given index value, but I don't know how to do it without loops.
Here's all of my code. Inside is my Assignment3 class I use for testing, StudentList which contains the LinkedList head and other List methods, and StudentNode which is obviously, the Node class. Also I've attached the first test1.txt file as well.
import java.io.FileNotFoundException; import java.util.*; public class Assignment3 { public static void main (String []args){ StudentList<StudentNode> myList = new StudentList<StudentNode>();
[Code] .....
I tried making a method that basically counts up the list recursively then a second method that counts down recursively and is supposed to stop once it hits the middle number, then print that node.
1. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox1, chkbox2), and click on submit, corresponding two text fields (chkbox1,chkbox2) will have to appear in the next jsp i.e., jsp 2.
2. In jsp 1 , i have 3 checkboxes(chkbox1,chkbox2,chkbox3) , if i select check boxes (chkbox2, chkbox3), and click on submit, corresponding two text fields(chkbox2,chkbox3) will have to appear in the next jsp i.e., jsp 2.
Like this, which ever checkbox i select, corresponding text fields should appear in the subsequent jsp.
In the above program even if i comment out the Hashcode method , i believe it is still taking the memory address values from the native hashcode method of Object class. but the equals override implentation says that i have two insertions which are same . So as per my logic it should not allow the duplicate element to enter.but its not so ...the duplicate element is well inserted without hashcode .
How to implement GridLayout. In my applet, I want to make a grid of 2 rows and 2 columns. In each grid I want to add a Label and a TextField. I want the background to be red.
So my code would be?
import java.awt.*; import java.applet.*; import java.awt.event.*; public class GridLayoutApplet extends Applet implements ActionListener{ // construct components Label fNameLabel = new Label("First Name"); TextField fNameField = new TextField(20);
[Code] .....
I have read about panels and frames but, it is all confusing to me. How can you add a label and a TextField to one square of the grid?
Not necessarily Java related, more a general programming issue I have come a long and I am currently using java to mock up a project that will eventually get moved over to C++.
I am trying to implement FFT Convolution into a java project and have things partially working, I do not know if it is a coding issue, logic issue or if I simply know nothing of FFT and convolution.
For starters I got an FFT library that I do have working, if I create a small array pass it into the forward FFT, do a little bit of bit manipulation (converting a complex array of size n*2 into a re[] and im[] of size n), run the inverse FFT and do the same bit manipulation again I get back where I started.
However if I take two arrays, a Dirac Delta function (an array with a 1 followed by zeros for the rest of the array) and a stepping kernel (ex. {1, 2, 3, 4 etc}), I would expect to get the forward FFT of both arrays, bit manipulation, multiply them together, Inverse FFT, and bit manipulate again and the result would be the kernel. I have not had such luck.
This is the gist of the program:
// bit manipulation, converts complex array to two re and im arrays public void bitTwiddle(double [] real, double [] real2, double [] imaginary){
for (int idx = 0; idx < (N * 2); idx++){ realCopy[idx] = real [idx];
[Code] ....
This is the result:
Quote ===================================================================== Dirac Delta In Time Domain: Real:1.000.000.000.00 Imag:0.000.000.000.00 ===================================================================== Dirac Delta Frequency Domain Real:0.500.500.500.50 Imag:0.000.000.000.00 ===================================================================== Kernel In Time Domain Real:1.002.003.004.00 Imag:0.000.000.000.00 ===================================================================== Kernel In Frequency Domain Real:5.00-1.00-1.00-1.00 Imag:0.00-1.000.001.00 ===================================================================== Result: Dirac Delta x Kernel Real:2.50-0.50-0.50-0.50 Imag:0.00-0.000.000.00 ===================================================================== Result In Time Domain Real:0.501.501.501.50 Imag:0.000.000.00-0.00
Something about implementing Comparator interface isn't very clear to me: overriding the compare method.
Like here for example:
//This sorts a list of objects holding information based on age: the name and the age of the person
public class Person { String name; int age; public Person (String name, int age) { this.name = name; this.age = age;
[Code] ....
What exactly is happening behind the scenes? I don't understand mostly the part where it returns a 0, a 1, or a -1. After it returns one of those values, what really happens next?
For the displaying of the list, is the method toString() being accessed to output the list in the System.out.println statement?
The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c".
I don't understand why since I expected it should be null.
The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.
Therefore, I expected head.next.next should be a null element, but the result is not like that.
public class ImmutableQueue<E> { private int size =0; public Queue<E> head; public Queue<E> tail; public ImmutableQueue(){} private ImmutableQueue(Queue<E> hd, Queue<E> tl){ head=hd; tail=tl;
I want to implement new locking mechanism similar to like how threads locking or synchronized does the operation. Any inputs to implement out own locking mechanism ?
I am in the process of implementing Priority queue, as I understand that there are many data structures you could use to implement. I implemented it with the an array, which it works absolutely fine. However I have limitations on what collections I can use from the collections classes. I fact I cant use any of the collections classes. Meaning I cant use array.
I’m trying to implement Priority Queue using heap. And implementing heap using binary trees. But however I have a few questions which I need to clarify and I cant think of any other way of resolving it. Ofcourse I can implement my own simple array class using linked list.
Inserting into heap would be quite simple, as I just need to find the right last position from left to right leaf to insert the node into the tree. However after inserting, you may want to make sure that leaf node values are > than root node. Therefore, the root node will always be with the highest priority.
I call these steps where you compare from top down as bubbledown and bubbleup. To do this I really need a for each node within the treee node to have attribute pointing to its root node. So in case of bubbleup I always have a pointer for a given node to its root, without it would mean I would to traverse through the entire tree to identify its root. Which I believe is very inefficient.
Or have I taken this completely wrong? Or is it the case that heap are only best with arrays and therefore use array (by implement it using linked list?)
I have to write a code for File Transfer Protocol! It says that I only need to write code for client, but it also says that I have to run the program and send some files to server. I guess than I also need server side as well. I have to write my own classes, I cannot import some existing classes. I have to connect to the server with password and username and after that send some files to the server. I don't know anything about FTP. Can i maybe do this by Sockets and ports? I worked something with that.