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
ADVERTISEMENT
Nov 26, 2014
for x = 0 to 9
set stars = "*"
set count = 0
while count < x
stars = stars + "*"
count = count + 1
endwhile
display stars
endfor
I need to change this pseudocode to Java, but I'm new, been trying for ages but can't get it right.
View Replies
View Related
Jan 1, 2014
I can't spot where my java implementation of insertion sort differs from the pseudocode here:Well, there is one difference in the parameters used by the insert method, which is inconsistent in the pseudocode.I'm pretty sure it should be calling insert (a,n) instead of (a,i,n);
insert(a,k) i←k
x ← a[k]
while x < a[i − 1]
x ← a[i]
a[i ] ← a[i − 1] i ←i −1
a[i]←x return
insertion-sort(a,n)
m ← select-min(a,1,n) swap(a,1,m)
fori from2upton
insert(a,i,n) return
Here is my attempt at a java implementation, which doesn't actually seem to do anything.I've kept variable names as in the pseudocode. Might technically be bad practice, butI think it should make it easier to follow in this particular scenario.public class InsertionSort {
public static void main(String[] args) {
int[] array = { 99, 2, 44, 14, 14, 44, 11, 33, 14, 14, 51 };
insertionSort(array, 10);
for (int i : array)
System.out.print(i + " ");
[code]...
Why am I so interested in this pseudocode when there are simpler java-ready examples of insertion sort on the internet? Simply because this is the code the professor uses, so I should be able to understand it.
View Replies
View Related
Nov 26, 2014
calculate how long it would take to pay off a loan of 500 pounds if there was 10% interest monthly, and 100 pounds was paid each month. This code is in a pseudocode,
set months = 1
set balance = 500
set totalpaid = 0;
while balance > 100
balance = balance - 100;
set interest = balance * 0.1
balance = balance + interest
totalpaid = totalpaid + 100
months = months + 1
endwhile
totalpaid = totalpaid+balance
display "you paid "+totalpaid
display "It took you "+months+" months"
View Replies
View Related
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
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
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
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
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
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
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
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
Feb 21, 2014
What is difference between algorithm and programming?How to develope algorithm knowledge?
View Replies
View Related
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
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
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
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
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
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
Apr 12, 2015
I have a checker game already and I am trying to make an AI opponent even if not so Intelligent as long as it can move chips on its own until the game is over.
View Replies
View Related
Apr 29, 2015
I am using what is known as a Depth First Search to solve a maze using my own Stack created class for an assignment. The reason I believe I am getting this error is because my program is never meeting the `finishSpot` condition. I have used the debugger and it looks like it is infinitely stuck at the Point before the `finishSpot`. My logic seems to be mostly correct except until the point where my maze solver tries to finish the maze.
This is a sample maze:
*****
*s* *
* * *
* f*
*****
Here is my Main which uses my created Stack class.
//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point currentSpot = findPoint( maze,'s' );
Point finishSpot = findPoint( maze, 'f' );
findPath( maze,currentSpot, finishSpot,stack );
[code]....
View Replies
View Related
Dec 5, 2014
It's about a backtracking algorithm trying to solve a sudoku, represented by an array of integer arrays. For testing matters, the start field is empty.
static boolean solve(int[][] field, int i, int j) {
if (filled(field)) {
return legal(field);
} else {
for (int k = 1; k <= 9; k++) {
field[i][j] = k;
[Code] ....
View Replies
View Related
Aug 13, 2014
I wanna write a program that take an Hexadecimal and convert it to floating-point . what should I do ?
View Replies
View Related
Mar 10, 2014
I have 3 for loops here that do what I need them to do an individual basis. But, I can't seem to get them to work together. I need to calculate y=LN(E-k), compute the error and error squared value for y and print the variable when the value for y gives the lowest error squared value.
Java Code:
//Calculating y=LN(E-k) and Initializing the Array
for(int x=0; x<LNValues.length; x++)
{
//for(int y=0; y<9; y++)
//{
double i = Math.log(eValues[x][0] - kValue);
if(i > 0)
[code]....
View Replies
View Related
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
View Related
Apr 29, 2015
I am using what is known as a Depth First Search to solve a maze using my own Stack created class for an assignment. The reason I believe I am getting this error is because my program is never meeting the `finishSpot` condition. I have used the debugger and it looks like it is infinitely stuck at the Point before the `finishSpot`. My logic seems to be mostly correct except until the point where my maze solver tries to finish the maze, so i just need meeting that last condition that is causing my program to crash.
This is a sample maze:
*****
*s* *
* * *
* f*
*****
Here is my Main which uses my created Stack class.
//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point currentSpot = findPoint( maze,'s' );
Point finishSpot = findPoint( maze, 'f' );
findPath( maze,currentSpot, finishSpot,stack );
[Code] ....
I made a mistake it says my program is crashing which it is not, but simply not meeting the condition.
View Replies
View Related