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
ADVERTISEMENT
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
Oct 8, 2014
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.transform.sax.TemplatesHandler;
public class SingleLinkedList<E> implements Iterable<E> {
// Instance variables.
private Node<E> head = null;
private int size = 0;
[code]....
Iam unable to display the contents of list. Error in toString().
View Replies
View Related
Aug 29, 2014
I know nothing about algorithms and data structures.Is there any good books where I can start learning it.Should I also learn advanced mathematics?
View Replies
View Related
Nov 25, 2014
how to create new nodes with this values below and insert them in the following order.
55, 2, 76, 2 , 9, 1, 90, 43, 5, 23, 86, 3, 321, 4, 32, 66, 99, 12, 21
I have one Insert method One traversal method, with a post-order( I don't know if I need to use pre-order, I'm trying to use a very poor hash function ℎ(xx)= √xx xKK mod 2 where xx is 2012559 and KK=3.14. If ℎ(xx) = 0, my traversal method is pre-order, and if ℎ(xx) = 1 my traversal will be post-order.I have 4 separated classes, Tree.java, Node.java, Test.java and traversalType.java
package BST;
public class Node {
int data;
Node leftChild;
Node rightChild;
public Node(){
[code]....
View Replies
View Related
Nov 25, 2014
how to create new nodes with this values below and insert them in the following order.
55, 2, 76, 2 , 9, 1, 90, 43, 5, 23, 86, 3, 321, 4, 32, 66, 99, 12, 21
I have one Insert method One traversal method, with a post-order( I don't know if I need to use pre-order, I'm trying to use a very poor hash function ℎ(xx)= √xx xKK mod 2 where xx is 2012559 and KK=3.14. If ℎ(xx) = 0, my traversal method is pre-order, and if ℎ(xx) = 1 my traversal will be post-order.
I have 4 separated classes, Tree.java, Node.java, Test.java and traversalType.java
package BST;
public class Node {
int data;
Node leftChild;
Node rightChild;
public Node(){
[code]....
View Replies
View Related
Nov 22, 2014
I've looked over the concepts of Java programming, tested them in code and i understand most of them. I have a problem when i need to make harder programs , this might be because i dont know design patterns and algorithms.I'm curious what a entry level programmer needs to know to get a job in the field. Right now i was thinking i need to know:
1. The way all big concepts work and most of the keywords.
2. Design patterns.
3. Algorithms.
what i actually need to know for an entry level job and can you tell me which design patterns and algorithms are a must know for that first job. Considering i might have financial problems in the near future this is not a theoretical question, i really need to know what i need to learn in the next 2-3 months to get an entry level job down.
View Replies
View Related
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
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
Jun 30, 2014
I have an array with the following characters {'E', 'L','E','P','H','A','N','T','P','O'}
now, I need an array that will store the first array such that only the occurence occurs e.g {'E','L','P','H','A','N','T','O'} Notice that the characters 'E' and 'P' occur twice and as a result were not repeated the second time in the new array.
How would one go about this using the counting elements technique?
I tried this but not sure how to use the counting elements technique.
char [] arr = new char{'E', 'L','E','P','H','A','N','T','P','O'};
char[] bucket = new char[(arr[0] * arr.length)];
for (int i = 0; i < len; i++)
bucket[arr[i]]++;
View Replies
View Related
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
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
View Related
Feb 16, 2015
I made this "lesson" to get the result of a pyramide which I succeded but not understand why im affraid..The thing is I dont understand exactly how to count it.. Isnt "j" incrementing every loop ?? In my head it should be just one star per line( the height is set to 3)how to count from the first loop to the last?I understand that println change the line though, but not why j would loop more than one time on each line..
for(int i = 1; i <= height; i++){
for(int j = 0; j < i; j++){
System.out.print("*");
}
System.out.println();
}
}
}
result:
*
**
***
View Replies
View Related
Mar 6, 2014
How to do a count up timer in java? like "0:00:00" hh:mm:ss
View Replies
View Related
May 14, 2015
I'm trying to count the number of elements in an ArrayList which also have duplicates. So for example, in an ArrayList of strings which contains cat, cat, dog, horse, zebra, zebra, the answer should be two.
If an element is found to be a duplicate, that element should then be exempt from the search so if that element is found again it should not increase the duplicate count.
Here is my code:
public int countDuplicates() {
int duplicates = 0;
// TODO: Write the code to get the number of duplicates in the list
for (int i = 0; i < list.size()-1;i++) {
boolean found = false;
[Code] ....
I know it's wrong because right now it's still increasing the duplicate count for elements that have already been detected as duplicates. How can I make it stop doing this?
View Replies
View Related
May 2, 2014
I have having some trouble on counting the primitive operations on the pseudocode given below:
Algorithm 4. MaximumArray(Arr)
Input: A 1-D numerical array Arr of size n
1) Let CurrentMax = a0
2) For i = 1 to n-1
3)If ai > CurrentMax Then CurrentMax = ai
4) End For
Output: CurrentMax, the largest value in Arr
As of now, I know that for Line 1 there are 2 operations (one set and one read). I don't know how to figure out the for loop and If statement (line 2 and line 3 too).
View Replies
View Related
Feb 13, 2015
I am currently trying to count and display all the vowels in a set of given strings and can't seem to figure out what to do. I was able to print the line with the most vowels, but i also need to display them. The code is listed below and the given output.
public class Strings
{
public static void main(String[] args) {
String sentence = "I am currently studing Computer Science."
+ "My name is whatever and I am orginaially from the state of Virginia."
+ "I recenetly separated from the Air Force where I served on the Presidental Honor Guard.";
[Code] ....
The output that i am getting is:
I am currently studying Computer Science .
My name is whatever and I am originally from the state of Virginia.
I recently separated from the Air Force where I served on the Presidential Honor Guard.
The line with most vowels is:
I recently separated from the Air Force where I served on the Presidential Honor Guard.
View Replies
View Related
Feb 28, 2014
(Count occurrence of numbers) Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:
Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
Note that if a number occurs more than one time, the plural word "times" is used in the output.
View Replies
View Related
May 25, 2014
I am currently take a class, and trying to increment the count of employees I enter into an ArrayList by 1 for every entry. However, instead of incrementing by 1 each time, the current number of employeeIDs adds itself to itself, then adds 1, giving a non-sequential number (i.e. an employeeID of 75 will add itself to 75 + 1 to get a new employeeID of 151).
The following code is what I have done in terms of incrementing. (More coding has been done, but it is not used to increment, so I have omitted it.
public class Employee
{
// instance variables
private String firstName;
private String lastName;
private int employeeID;
static int newEmployeeID = 0;
[Code] ....
View Replies
View Related
Apr 25, 2014
why exactly this isn't printing out the correct way.I'm supposed to print out 100 elements from an array but assign them random integers between 0 and 9.I've tried troubleshooting the issue and when I do it seems i'm only getting one element assigned.
public class Exercise_6_7{
public static void main(String[] args){
int[] counts = new int[100];
for(int i=0; i<counts.length; i++){
counts[i] = (int)(Math.random()*10);
[code]....
View Replies
View Related
Mar 26, 2014
I am trying to sort an ArrayList of objects with the comparator as I want to sort based on a certain value for each object. I understand I would need to override compareTo() in the objects class, is there any way I can get around also needing to override for all subclasses of the object?
View Replies
View Related
Oct 16, 2014
So we have to ask the user to put in a string of letters, and bring those letters in as cars to where there is a storage area and an assembly area, and we have to sort them from there into the assembly area with the smallest (A) at the head. I think I set up my code pretty well, but when I run it, no matter what I put in it returns CBAo. Say I input KATE, it should return TKEA but instead CBAo or if I input JANICE it should return NJIECA but it just returns EDCBAo. Here's my code:
import java.util.Scanner;
import java.util.Stack;
public class carStacksDessart
{
public static void main(String[] args) {
Stack<Integer> storage = new Stack();
[Code] ....
View Replies
View Related
Nov 30, 2014
I'm trying to create a method that takes a map and sorts it alphabetically. I think I have the logic correct but I'm getting errors when I run it:
private static void sortMapAlphabetically(
Map<String, String> termsAndDefinitions) {
Map<String, String> tempMap = new Map2<String, String>();
[Code].....
View Replies
View Related
Nov 26, 2014
I have to sort out my ArrayList but cannot make it work. Here is
Java Code:
Collections.sort(this.users);
User poor = this.users.get(0);
User ritch = this.users.get((this.users.size()-1));
System.out.println("Poorest user has" + poor.getUsername() + poor.getBalance() );
System.out.println("Ritches user has " + ritch.getUsername() + poor.getBalance() + "
"); mh_sh_highlight_all('java');
The Collections.sort(this.users); does not work.
View Replies
View Related
Apr 12, 2015
I have taken array int[] a = {33,33,5,5,9,8,9,9};
In this array so many values are duplicates means 33 comes twice & 5 also comes twice & 9 comes three times. But I want to count the first value which is duplicate means 33 is first value which comes twice so answer would be 2.
I try:
public class FindFirstDuplicate {
public static void main(String[] args) {
int c=0;
int[] a = {33,33,5,5,9,8,9,9};
outerloop:
for(int i = 0; i < a.length; i++) {
[code]....
Output:
33
Count: 1
View Replies
View Related
Jan 21, 2015
I have 2 classes a main one and another class. In the other class I have a method that has:
String gInfo(){
String[] Behavior = {"Sleeping", "Running home", "Studying", "playing pool", "walking to class"};
int rNum = (int) (Math.random() * Behavior.length);
return Behavior[rNum];
}
I call the gInfo() in the main class and run it through a loop 20 times. What I'm trying to do is get it to count the number of times that each behavior happens. and how to get it to work correctly.
View Replies
View Related