Java Radix Sort Algorithm

Apr 19, 2014

I got this code from wikipedia when trying to learn about the radix sort algorithm now I understand that the algorithm sorts by significant digits but it's the code that I'm not too sure about for instance the series of for loops at the bottom what exactly is going on there and why is it mod by 10? Also why are there three different integer arrays a, b, and bucket?

public static void radixsort( int[] a, int n) {
int i;
int digit = 1;
int[] b = new int[n+1];
for (i = 1; i < n; i++)
if (a[i] > a[0])
a[0] = a[i];

[Code] ....

View Replies


ADVERTISEMENT

Find Which Algorithm Java Uses For Its Sort Method

Apr 27, 2014

I have been looking around and I cannot seem to find which algorithm java uses for its sort method. Merge, sort etc.

While I am on the topic BinaryTree is a balanced heap right?

Are there any libraries for depth or breath first search?

Is the ArrayList a double linked list?

View Replies View Related

Insertion Sort Algorithm Using Java Codes

Jan 25, 2015

I have to write the Insertion Sort Algorithm using Java codes and at the same time find the time of execution for different sizes of array, filled with random numbers. If I try to show the numbers inserted into the array randomly, they don't appear at the console.

import javax.swing.JOptionPane;
public class Insertion {
public static void main(String[]args){
int SizeArr = new Integer(JOptionPane.showInputDialog("Enter the size of teh array")).intValue();
int [] r= new int [SizeArr];
{for(int d=0; d<r.length; d++)

[Code]...

View Replies View Related

Writing A Recursive Merge Sort Algorithm

Mar 27, 2015

I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].

I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.

Below is what I have along with my JUnit test.

Java Code:

private Queue<T> output = new Queue<T>();
private Queue<T> output1 = new Queue<T>();
private Queue<T> output2 = new Queue<T>();
public Queue<T> mergeSort(Queue<T> queue) {
// TODO 1
if(queue.size() <= 1) {
return queue;

[Code] .....

View Replies View Related

How To Make Insertion Sort Into Non-increasing Algorithm

Aug 29, 2014

In class we were give the algorithm for a non-decreasing algorithm here:

This is pseudo code given for the non-decreasing.

for j = 2 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i -1
A[i = 1] = key

//I was asked to make it non-increasing, so this is what I have.

for j = A.length - 1 to 1
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i+1] = A[i]
i = i + 1
A[i-1] = key

Is there anything wrong with this algorithm? Will it give me the non-increasing sort?

View Replies View Related

Quick Sort Algorithm - Stack Overflow Error

Oct 15, 2014

I have wriiten a quick sort algorithm. I have used the last element as my pivot. The program is running for all sizes except for 100000 and 1000000 elements when they are sorted and unsorted list .

It shows me the error : Exception in thread "main" java.lang.StackOverflowError

I guess the memory gets out of space and we need to increase the stack size in eclipse. How do I increase the stack size in eclipse.? I tried increasing through run--run configurations-- program arguments and typed---- -Xmx4096m but this didn't work in any way.

View Replies View Related

Creating Java Linear Algorithm

Oct 13, 2014

I'm finding common elements in a collection of arrays. This is my code so far.

import java.util.ArrayList;
import java.util.List;
public class CommonElements //< T extends Comparable<T> >
{
Comparable[] comparable;
Comparable[] tempArr;
Comparable[] tempArr1;
Comparable[] newArray = new Comparable[tempArr.length];
int comparisonCount;
 
 [code]....

I'm using this method to sort through my array. The method accepts a collection of arrays (of varying length and of any type) as input, the algorithm input should be no greater than n(k-1). I know I can solve it using a quadratic algorithm, but it won't meet the requirement of the assignment. I did have an idea of storing all my algorithm in one giant array. After storing as one giant array I was going to sort it. After sorting I was going to compare each array side by side and see if they are the same. Here is note my teacher provided.

Note About Testing You will need to develop several sets of test collections for testing your algorithm. The grading rubric mentions covering the case where all the test collections have the same length, as well as covering the case where the test collections are of different lengths. You will also need to think about what constitutes the worst case scenario for this algorithm, since only that scenario will make your analysis of total comparisons performed a meaningful one. You can use the formulas in the grading rubric to tell you how many comparisons you should expect in the quadratic and linear cases. For example, if you have 5 total collections (1 query collection and 4 test collections), each of which contains 10 elements, the total number of comparisons performed in the worst case should be: (k - 1)N2, which for k = 10 and N = 10 is: (5 - 1)102, or 400 comparisons. For the linear algorithm, you should only have N*(k - 1), which is 10*(5 - 1), or 40 comparisons.

Here is Q/A that my teacher provided:

1. Are the elements in the collections already sorted?The input collections may or may not be sorted. Don’t assume that they are sorted.

2. Can I sort the elements in the collections? Am I supposed to sort the elements in thecollections?Yes, you can sort the elements in the collections. It is not required that you do so, however..

3. If I sort the collections, should I count the element comparisons that are performed by the sorting algorithm?No. The only element comparisons you should count are the ones that are directly used to find the common elements. Ignore comparisons performed by any sorting algorithms you use.

4. How do I extract an individual collection from the Object[] collections argument of the findCommonElements method?You will need to typecast each collection as type Comparable[]. For example:Comparable[] currentCollection = (Comparable[])collections[i];

5. Why are we using a one-dimensional Object array instead of a two-dimensional array of type Comparable (e.g., Comparable[][])?In Java, arrays are treated as objects, so only a variable of type Object[] can contain arrays. A variable of type Comparable[] can only contain Comparable elements, not arrays. A variable of type Comparable[][] also can only store Comparable elements. Although it is possible to organize the elements such that all the elements at indexes [1][i], for example, are considered to comprise a collection, this is not the same as having an array that contains other arrays. The additional bookkeeping is required to keep a 2D array organized is more complex than simply performing the typecast described above.

6. Can I use other data structures besides arrays; e.g., HashMaps?No. Although it is easier from a programming perspective to use higher order data structures like HashMaps to find common elements, using these structures simply hides the details. If you were to examine the implementations of those structures, you would probably find that they use relatively inefficient algorithms such as sequential searches to find elements. Additionally, if you use library components, you have no way of counting the number of comparisons performed, since you don’t have access to the source code of those components.

7. Is this a trick question? I can’t think of any way to get below NlogN comparisons.It is not a trick question. It is possible to solve this problem using a number of comparisons proportional to (k – 1) * N.

View Replies View Related

Hash Mapping Algorithm In Java

Apr 13, 2015

I need a Example of a Hash Mapping Algorithm in Java. This Mapping Needs to do the following:

Map an Array of Strings, to integer values which correspond to each fruit

private String[] Fruit = {"Apple","Orange","Pear","Grapes"};

Apple: 1 + 1
---------------
Orange: 1+ 2
---------------
Pear: 1 + 3
---------------
Grapes: 1 + 4
---------------

I not sure where to start at the Moment...

View Replies View Related

Java Algorithm - Convert String Of Numbers Into Text

Feb 18, 2014

I need a Java algorithm that converts a string of numbers into text. It is related to how a phone keypad works where pressing 2 three times creates the letter "c" or pressing 4 one time creates the letter "g". For example a string of numbers "44335557075557777" should decode where 0 equates to a space.

View Replies View Related

Possible To Create Program With JAVA That Uses Algorithm To Summarize Data Input

Jan 23, 2015

So I was just wondering if it's possible to create a program with JAVA that uses an algorithm to summarize data inputed. I've never done something like this before, If this is not possible with Java is it possible with C++ or any other language??

View Replies View Related

Sort Java Array With More Than One Value?

Jan 22, 2015

how can I got about sorting an array that contains more than one value in a single element. Such as my array below has 4 values under one element. I know how to sort elements with single values however, slightly confused on this.

import java.util.Scanner;
import java.util.Arrays;
class Mobile
{

[Code]......

View Replies View Related

How To Create A Sort Of Executable For Java App

Dec 25, 2014

when I am programming let say in VB using Visual Studio, finally I build .exe file that can be run on all Windows by double click.For Java I am using Eclipse and to run those apps I am using run from Eclipse.How I can create a sort of "executable" for my Java app that I would be able to run it by file click on Windows or Linux?

View Replies View Related

Java Merge Sort Not Working

Dec 10, 2014

I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...

public class MergeSort
{
private static int[] local; // for use in copying
private static int[] a;
public static void main(String[] args) {
int[] test = {2,3,1,4,7,8,6,3,9};

[Code] ....

View Replies View Related

Sort 5 Numbers In Java From High To Low With If Statements?

Sep 16, 2014

I am trying to code a program that orders 5 random numbers from high to low with the basic coding i know (im starting to learn theory of methods... so you can imagine) When i run the code i cant get all 5 numbers ordered but my logic says the code is right although it's pretty confusing.

I know you could code in a simpler way but first i wanna get it as it is right now. When i debug (on my own way cause i dont know how to actually use it) shows line 72 with yellow color.

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

[Code]....

View Replies View Related

Merge Sort Java And Linked List?

Dec 11, 2014

I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.

My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.

//defining a job that has priority
public class Job {
private int priority;

[Code]....

The problem I've been trying to solve is that I am getting the stack overflow at line

ListNode<T> h1 = mergeSort(left);

meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.

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

Binary Search Algorithm

Mar 24, 2014

I am trying to make a program that compares the 3 different search algorithms(sequential/binary/hashing). I have already made the sequential search algorithm, binary and hashing part of the code.My program OPENS a data file, and then OPENS a key data file. and then searches through them using both. How to go about the binary and hashing search algorithm (in the driver program).

*I have included a zip file with:
-base program
-driver program
-data file
-key data file

View Replies View Related

Searching And Sorting Algorithm

Jan 2, 2015

I have a code that is meant to read a file and organize all the names from least to greatest salaries. It also allows the user to enter a name to find from the file, and the program finds the name and displays it. I have two errors, and I will show the error in my code

View Replies View Related

Creation Of Algorithm For Log In With Verification

Feb 7, 2015

I'm making a code for a log in system that allow me to verify if username and psw are correct (using a file txt as refer), and then i will add maybe the possibility to sign up.

The fact is that I want this kind of formatting on the .txt

username psw
username psw
username psw

...etc

So I have to read the lines and split to the " " and compare the insert data and the read data.

Here is my code, it star but give me this error when i insert any word

XML Code:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at prova.main(prova.java:20) mh_sh_highlight_all('xml'); Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class prova {
public static void main(String[] args) throws FileNotFoundException {

[Code] .....

View Replies View Related

Difference Between Algorithm And Programming

Feb 21, 2014

What is difference between algorithm and programming?How to develope algorithm knowledge?

View Replies View Related

Text-to-image Algorithm

Oct 4, 2014

Encryptor

Java Code:

Private Sub cmdDeleteMessage_Click()
If txtMessage.Text = "" Then
a = MsgBox("No message to delete!", vbInformation, "")
Else: If MsgBox("Do you want to create a new message?", vbYesNo, "New") = vbNo Then Exit Sub
txtMessage.Text = ""
Pic.Picture = LoadPicture()

[Code] .....

Is these codes of the Block cipher encryptor for text-to-image algorithm (B-TTIE algorithm) by Abusukhon 2013? If not, how to make it the same with B-TTIE algorithm?

View Replies View Related

How To Create Genetic Algorithm

May 23, 2015

how to create a genetic algorithm.I made this class that Is supposed to Generate the chromes or Possible solutions of the program. Im pretty sure It doesn't have any syntax error and I went over the logic a couple time yet Im getting a NumberFormatException Exception Error. What this error means nor why am I getting it. Here's my code:

/* Contains the genes or possible solutions to the problem
*/
public class Genes
{
/* Each element is a binary number that corresponds to index number they have been assigned to, these are the possible genes
* The last 4 elements in the array represent + - * / correspondingly
*/
private String[] encodedNumbers = {"0000", "0001", "0010", "0011","0100", "0101", "0110","0111","1000","1001","1010","1011","1100","1101"};

[code]...

View Replies View Related

Tic Tac Toe Game - Minimax Algorithm

Jan 4, 2014

I have been trying to create an algorithm for the computer in my Tic-Tac-Toe game? So I searched for some good ones, and found about this confusing but effective algorithm, at least if you get it working, called Minimax. I tried implementing it into my game. Let's say it was easier said than done. Sometimes I think it's working, but sometimes it doesn't work at all. How do I get my algorithm working? What the variables BEST_SCORE_COMPUTER and BEST_SCORE_PLAYER in my code should have as starting values.

Java Code:

import java.util.Scanner;
public class TicTacToe{
private static char BOARD[] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; // This is the main board itself.
private static int ROWS[][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}}; // These are all the rows one can win by filling.
private static int SCORE, BEST_MOVE_COMPUTER, BEST_SCORE_COMPUTER = -1, BEST_SCORE_PLAYER = 1; // These variables speak for themselves.
private static byte TURN = 1; // 1 means player's turn, and 2 means computer's turn.

[Code] .....

View Replies View Related

Algorithm That Processes Numbers

May 7, 2014

I have a set of numbers within a text file.

2 6
1 4 4 1

When the program reads these numbers it will start with the first line. It reads 2, this is the number of times the program should loop through the numbers of the second line. Next it reads 6, this is the number that the sum of numbers on line 2 cannot go beyond, it must equal 6 or be less than.

Next it goes to line 2 and processes the numbers, 2 is less than 6 so it gets processed, so is 4 and when added to 2 it equals 6. This passes the rules therefore gets processed and added together and put in a variable. Once 2 and 4 has been processed it gets put at the back of the number queue, so the numbers look like 1 1 2 4.

With one loop done there is only one left, so it processes the next sum of numbers 1 1 2 as these equal 4 and pass the rule. So these get added together and and put to the back of the queue, 4 1 1 2. But all the loops are finished and the sum of the last loop is added on to the sum of the previous loop. 6 + 4 = 10.

What I need the algorithm to do is print the sum of this whole process when it reads these numbers from a file. I am able to read the numbers from a text file but not sure where to proceed from there.

Also the set of numbers vary, for example the next 2 lines of numbers could be,

10 7
3 1 1 1 4 4

View Replies View Related

Enable MD2 Algorithm Only For A Particular Place And Not For Whole JVM

Jun 6, 2014

Is it possible to enable & disable "jdk.certpath.disabledAlgorithms" property programmatically. I want to remove all disabled Algorithms for some time and later I will enable it.

Or is it possible to enable only for a particular place & not in a JVM level. I want that Algorithm need to be enabled for a piece of code, but I don't want it to the remaining part of the application.

View Replies View Related

Genetic Algorithm Programming

Oct 13, 2014

I am looking to program a simple genetic algorithm in Java but where to start.

" In a computer language of your choice, implement a simple genetic algorithm (GA). That is, write code for a generational GA which uses a binary encoding, tournament selection, single-point crossover and bit-wise mutation" ....

View Replies View Related







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