Problems with 3 way sort in Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I think there may be a problem with my 3 way sort algorithm in the following Java program, also any suggestion on optimizing or just a simpler it would be greatly appreciated. The objective of the sort is to have the minus numbers first then zeros and then positive numbers
class ThreeWaySort
{
public static void main(String[] args)
{
int location = 0;
int[] sArray = new int[50];
for (int a = 25; a<= -24; a--)
{
sArray[location] = a;
location++;
}
int i = 0; int j = 0; int k = 50;
while (j!=k)
{
if (sArray[j]==0)
{
j++;
}
else if (sArray[j]<0)
{
int t = sArray[i]; sArray[i] = sArray[j]; sArray[j] = t; // case (ii)
i++; j++;
}
else
{
k--;
int t= sArray[j]; sArray[j] = sArray[k]; sArray[k] = t;
}
}
for (int a = 0; a <= 49; a++)
{
if(sArray[a] >-1)
{
System.out.println();
System.out.println();
System.out.println();
}
if(sArray[a] > 0)
{
System.out.println();
System.out.println();
System.out.println();
}
System.out.print(sArray[a] + " ");
}
}
}
When i run the program as is it costantly print out a zero followed by three line instead of what I'm expecting to be, Numbers below zero in a line, followed by 3 blank lines then any zeros in the array, 3 blank lines, positive numbers in the array.

The loop that populates your array is incorrect:
for (int a = 25; a<= -24; a--)
The variable a starts at 25, which is not less than or equal to -24, so the loop never executes. You should use >=.

Related

Trying to figure out how can I make a program to identify equal numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
so my real question is, how can i make this code identify all the "look alike" numbers while theire running from 1 to 99, for example :11,22,33,44,...
and while the program identify them it sends a message.
package doodle;
int num2=11;
for (int i=1; i<100; i++) {
System.out.println(i);
int num1=i;
if(num1==num2) {
System.out.println("WOW");
}
}
Thanks
I would do using a String
for (int i = 11; i < 100; i++) {
StringBuffer orig = new StringBuffer();
String left = orig.append(i).toString();
if (orig.reverse().toString().equals(left)) {
System.out.println(left);
}
}
or if you really wanted to use an int with flaky logic
int start = 11;
for (int i = 11; i < 100; i++) {
if (i == start) {
System.out.println(start);
start += 11;
}
}
Edit
As #mark has rightly pointed out, these solution only work whilst the range is up to 100
int num2=11;
for (int i=1; i<100; i++) {
if(i%num2==0) { //<---- look alike
System.out.println("WOW");
}
I would do it using String conversion and codePoint comparison
for (Integer number = 0; number < 1000; number++) {
System.out.println(number);
String stringnumber = String.valueOf(number);
if (stringnumber.length() > 1 && stringnumber.codePoints().allMatch((digit) -> digit == stringnumber.codePointAt(0))) {
System.out.println("WOW");
}
}
length check (length() > 0) is needed to exclude all numbers with only one digit, otherwise, the program would print "WOW" for all numbers from 0 - 9 too.
All numbers from 0 to Integer.MAX_VALUE can be handled.

I have a function in java which may have bug and I can not find it [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this code which suppose to do the following task:
For example , given M=3, and array built like this:
A[0]=1
A[1]=1
A[2]=3
A[3]=3
A[4]=5
A[5]=1
A[6]=3
the function may return 1 or 3
import java.util.*;
class Solution {
int solution(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
what could be the problem because it is not always working and I can see there is a bug in my program.
1 1 1 1 5 5 5 5 5 this is a case where your code may fail. Check and update the max occurance variable outside the loop too. Above case give enough justice to my point, i hope.

How to add and print prime numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The idea is to type two numbers in the command line. The first being the start and the second being the number of prime numbers after the first number are displayed. For instance if the input was 16 3 the output would be 17 19 23
These numbers would be stored into an array. I've been trying many different things but they haven't worked so far.
I only have this right now
{
public static void main (String[] args)
{
int start =Integer.parseInt(args[0]);
int count =Integer.parseInt(args[1]);
int[] prime = new int[count];
public static boolean check(int a)
{
if (a%2==0)
return false;
for(int i=3;i*i<=a;i+=2)
{
if(a%i==0)
return false;
}
return true;
}
}
Your check function is great. What you need is just a while loop to add prime number to prime[]
public static void main (String[] args)
{
int start =Integer.parseInt(args[0]);
int count =Integer.parseInt(args[1]);
int[] prime = new int[count];
int i = 0;
while (i < count){
if(check(start)){
prime[i] = start;
i++;
}
start++;
}
for (int p : prime){
System.out.println( p);
}
}
If you want a program to do something N times, you can use a loop.
Since this feels like HW I will provide an analogous problem. Count N odd numbers:
int numbersToCount = 5;
int count = 0;
int start = 1;
while (count < numbersToCount) {
if (start % 2 == 1) {
count = count + 1;
// opportunity to print number you are interested in
}
start = start + 1;
}

displaying a reversed array? java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
It prints
Flip Method (only prints 9 to 5 ) but I want it to print from 9 to 0
http://imgur.com/gggiJwn
public static void flip(int[] flp){
System.out.println("This is the flip method");
for ( int i = 0; i < flp.length; i++){
int e = flp.length-1;
int temp = flp[e-i];
flp[i] = flp[e-i];
flp[i] = temp;
e--;
System.out.println("Index"+(i)+" :"+flp[i]); //is this the problem?
}
}
}
You need to change your logic to following to reverse the full array
public static void flip(int[] flp){
System.out.println("This is the flip method");
for ( int i = 0; i < flp.length/2; i++){
int e = flp.length-(1+i);
int temp = flp[i];
flp[i] = flp[e];
flp[e] = temp;
// remove print from here. else you will get half of the array
// since flp.length/2
}
}
Add that in a separate method.
for(int i = 0; i < flp.length; i++){
System.out.println("Index"+(i)+" :"+flp[i]);
}
The problem is that you are printing half of the elements
i < flp.length/2
That should be
i < flp.length
Actually, your for loop is fine - but you should remove the printing from inside that loop, and have it in a seperate loop after the flip loop
for ( int i = 0; i < flp.length; i++){
System.out.println("Index"+(i)+" :"+flp[i]);
}
public static void flip(int[] flp) {
System.out.println("This is the flip method");
int e = flp.length - 1;
for (int i = 0; i < flp.length / 2; i++) {
int temp = flp[i];
flp[i] = flp[e];
flp[e] = temp;
e--;
}
for (int i = 0; i < flp.length; i++) {
System.out.println("Index" + (i) + " :" + flp[i]);
}
}
Your for loop prints 9 to 5 because this is how you have configured your loop to iterate till i < flp.length/2 .
Change it to:
i < flp.length
from
i < flp.length/2

This program in java doesn't stop executing [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have written this java program for sorting some numbers. But it doesn't stop executing. Can you help me with it?
package inplacesort;
import java.util.*;
public class InplaceSort
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Vector <Integer> intList = new Vector <Integer> ();
//getting the numbers from the user
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
System.out.print("Enter a Number: ");
intList.addElement(console.nextInt());
System.out.print("Do You Want to Continue?(Y/N)");
ans = console.next().charAt(0);
}
System.out.println("Before Sorting the Numbers: " + intList);
for (int i = 1; i < intList.size(); i++)
{
int j = i - 1;
while (j > 0 && intList.elementAt(i) < intList.elementAt(j))
{
j--;
}
for (int k = intList.size() - 1; k >= j; k--)
{
intList.insertElementAt(intList.elementAt(k),k + 1);
}
intList.insertElementAt(intList.elementAt(i+1),j);
intList.removeElementAt(i+1);
}
System.out.print(intList);
}
}
Your problem is with intList.size() in the k & i loops. This is not be as what you would expect it. When I debugged your code the value of k was 425996.
Edit :
When i debugged it more I saw that because of you mutating the vector within it self it keeps increasing in size. If you let your program run for a few minutes you will get out of memory error.
Please don't mutate the object you are looping though it. Either make a copy of it and the loop though one of them and mutate another or start with a fresh one & keep adding the values to it while looping over the older one.
System.out.println("Before Sorting the Numbers: " + intList);
List<Integer> sortList = new ArrayList<Integer>();
int minVal;
int index=0;
int size = intList.size();
for (int i = 0; i < size; i++)
{ minVal=Integer.MAX_VALUE;
for (int j = 0; j < intList.size(); j++)
{
if(intList.get(j) < minVal){
index=j;
minVal=intList.get(j);
}
}
intList.remove(index);
sortList.add(minVal);
}
System.out.print("After Sorting the Numbers: "+ sortList);
The reason is because your value for j is ALWAYS 1 less than the value for i. Therefore, infinite while loop.

Categories