Trouble with recursive merge sort bug - sorting

I am new to Java and have tried to implement mergesort in Java. However, even after running the program several times
this is method mergeSort
public static int[] mergeSort(int[] array) {
int size = array.length;
if (size < 2)
return array;
else {
int mid = array.length / 2;
int leftSide = mid;
int rightSide = size - mid;
int[] left = new int[leftSide];
int[] right = new int[rightSide];
for (int i = 0; i < mid; i++)
left[i] = array[i];
for (int i = mid; i < size; i++)
right[i - mid] = array[i];
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
}
and this is method merge
private static int[] merge(int[] left, int[] right) {
int[] sum = new int[left.length + right.length];
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[i]) {
sum[k] = left[i];
i++;
k++;
} else {
sum[k] = right[j];
j++;
k++;
}
}
while (i < left.length) {
sum[k] = left[i];
k++;
i++;
}
while (j < right.length) {
sum[k] = right[j];
k++;
j++;
}
return sum;
}
that's all
i'm tired
please help me find it out

i find out the bug
here it is
while (i < left.length && j < right.length) {
if (left[i] < right[j) {
sum[k] = left[i];
i++;
k++;
} else {
sum[k] = right[j];
j++;
k++;
}
}

Related

Find unique number in array in fill list with this unique numbers using for loops

I'm trying to use for loop inside for loop but it doesn't working i also tried while loop but...
int[] numArray = new int[10] {1,2,2,3,3,4,4,5,5,9};
List<Int32> uNum = new List<Int32>();
/*Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}*/
for (int i = 0; i < numArray.Length; i++)
{
if (numArray[i] != numArray[i])
{
for (int j = 0; j < numArray.Length-1; j++)
{
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
You have an error in this line:
if (numArray[i] != numArray[i])
This condition will always return False because a number is always equal to itself.
Do something like this:
for (int i=0; i<numArray.Length; i++)
{
int j;
for (j=0; i<numArray.Length; j++){
if (i != j){
if (numArray[i] != numArray[j])
{
uNum.Add(numArray[i]);
}
}
}
}
int[] numArray = new int[10];
List<Int32> uNum = new List<Int32>();
Random rnd = new Random();
for (int i = 0; i < numArray.Length; i++)
{
int randomNumber = rnd.Next(0, 10);
numArray[i] = randomNumber;
}
for (int i = 0; i < numArray.Length; i++)
{
int num = numArray[i];
int count = 0;
for (int j = 0; j < numArray.Length; j++)
{
if (numArray[j] == num)
{
count++;
}
}
if (count == 1)
{
uNum.Add(num);
}
}

Algorithm for traversing all lines in the n×n grid?

Use a two-dimensional array to represent a nxn grid.
var grid = new int[n,n];
Note that there are two more diagonal lines.
If i will solve this problem. I will make so.
Create extension method for Int[] (So, you can create your own class. But it's another way. I want to show light waight solution)
public static class IntAsMatrixExtensions {
public const int MatrixColumsCount = 3;
public static int At(this int[] matrix, int i, int j)
{
return matrix[i * MatrixColumsCount + j];
}
public static int[] Create()
{
var grid = new int[MatrixColumsCount*MatrixColumsCount] {
1,2,3,
4,5,6,
7,8,9
};
return grid;
}
}
Then first you should print matrix:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
for(int j = 0; j < IntAsMatrixExtensions.MatrixColumsCount; j++)
{
Console.Write(grid.At(i, j));
}
Console.WriteLine();
}
Then print transponated matrix:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
for(int j = 0; j < IntAsMatrixExtensions.MatrixColumsCount; j++)
{
Console.Write(grid.At(j, i)); //!!! i and j is swithed
}
Console.WriteLine();
}
Then print diag:
//Print diag
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
Console.Write(grid.At(i, i)); //!!! i and j is swithed
}
Then print inverse diag:
for(int i = 0; i < IntAsMatrixExtensions.MatrixColumsCount; i++)
{
Console.Write(grid.At(i, IntAsMatrixExtensions.MatrixColumsCount - i - 1)); //!!! i and j is swithed
}
Here is example on fiddle https://dotnetfiddle.net/pyX31r

I was trying to build a merge sort but ending up with an error

The method mergeSort(int[]) is undefined for the type merge. This is the error that I am facing. Can anyone please point out my fault? Might be I'm making a syntax error, please point out the mistake im making.
public class merge {
public static mergeSort(int[] a) {
int n = a.length;
if (n < 2) {
return(a[]);
}
int mid = n / 2;
int left[] = new int[mid];
int left[] = new int[n - mid];
for (int i = 0; i < mid; i++) {
left[i] = a[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = a[i];
}
mergeSort(left[]);
mergeSort(right[]);
mmerge(left[], right[], A);
}
public static void mmerge(int[] l, int[] r, int[] array) {
int len1 = l.length();
int len2 = r.length();
int i = 0;
int j = 0;
int k = 0;
while (i < len1 && j < len2) {
if (l[i] <= r[j]) {
array[k]=l[i];
k++;
i++;
} else {
array[k] = r[j];
k++;
j++;
}
}
while (i < len1) {
array[k] = l[i];
k++;
i++;
}
while (j < len2) {
array[k] = r[j];
k++;
j++;
}
}
public static void main(String args[]) {
int[] arr = { 4, 6, 2, 9, 1, 7, 3 };
mergeSort(arr);
for (int p = 0; p < arr.length; p++) {
System.out.print(arr[p]+" ");
}
}
}
Try this
public class merge {
public static void mergeSort(int[] a) {
int n=a.length;
if(n<2) {
return;
}
int mid=n/2;
int left[]=new int[mid];
int right[]=new int[n-mid];
for (int i=0;i<mid;i++) {
left[i]=a[i];
}
for (int i=mid;i<n;i++) {
right[i-mid]=a[i];
}
mergeSort(left);
mergeSort(right);
mmerge(left,right,a);
}
public static void mmerge(int[] l,int[] r,int[] array) {
int len1=l.length;
int len2=r.length;
int i=0;
int j=0;
int k=0;
while(i<len1 && j<len2) {
if(l[i]<=r[j]) {
array[k]=l[i];
k++;
i++;
}else {
array[k]=r[j];
k++;
j++;
}
}
while(i<len1) {
array[k]=l[i];
k++;
i++;
}
while(j<len2) {
array[k]=r[j];
k++;
j++;
}
}
public static void main(String args[]) {
int[] arr={4,6,2,9,1,7,3};
mergeSort(arr);
for(int p=0;p<arr.length;p++) {
System.out.print(arr[p]+" ");
}
}
}

print shapes in java

I want to print this shape big X from collection of small x using recursion
this is my code
private static void shape(PrintWriter output, int times, int k, int times2) {
if(times < 0){
return;
} else{
for (int i =0; i<times; i++){
if (i==times)
output.print("X");
else if(i==k)
output.print("X");
else
output.print(" ");
}
output.println();
shape(output,times-1,k+1,times2);
}
}
but I couldn't print the shape requested
Try this.
static void shape(PrintWriter output, int size, int index) {
if (index >= size)
return;
char[] buffer = new char[size];
Arrays.fill(buffer, ' ');
buffer[index] = buffer[size - index - 1] = 'X';
output.println(new String(buffer));
shape(output, size, index + 1);
}
and
try (PrintWriter output = new PrintWriter(new OutputStreamWriter(System.out))) {
shape(output, 11, 0);
}
Just change
int arr[] = new int[times]
to
int arr[] = new int[times2]
where times2 is the width of a single row.
However a more cleaner way would be:
public class InputTest {
private static void FCITshape(int times, int k,int times2) {
if (times < 0) {
return;
} else {
for (int i = 0; i <= times2; i++) {
if (i == times)
System.out.print("X");
else if (i == k)
System.out.print("X");
else
System.out.print(" ");
}
System.out.println();
FCITshape(times - 1, k + 1, times2);
}
}
public static void main(String[] args) {
FCITshape(10, 0, 10);
}
}
Regards.
With recursion
Now just call printX(0, 10);
public static void printX(int x, int l) {
if (x <= l) {
if (x < l / 2) {
for (int i = 0; i < x ; i++) {
System.out.print(" ");
}
} else {
for (int i = 0; i < l - x; i++) {
System.out.print(" ");
}
}
System.out.print("x");
if (x < l / 2) {
for (int j = 0; j < l - x * 2 - 1; j++) {
System.out.print(" ");
}
} else {
for (int j = 0; j < (x * 2 - l) - 1; j++) {
System.out.print(" ");
}
}
if (x != l / 2) {
System.out.print("x");
}
System.out.println();
printX(x + 1, l);
}
}

Uva Judge 10149, Yahtzee

UPDATE: I have found the problem that my DP solution didn't handle bonus correctly. I added one more dimension to the state array to represent the sum of the first 6 categories. However, the solution got timed out. It's not badly timeout since each test case can be solved less than 1 sec on my machine.
The problem description is here: http://uva.onlinejudge.org/external/101/10149.html
I searched online and found that it should be solved by DP and bitmask. I implemented the code and passed all test cases I tested, but the Uva Judge returns wrong answer.
My idea is to have state[i][j] to be matching round i to category bitmasked by j. Please point out my mistakes or link some code that can solve this problem correctly. Here is my code:
public class P10149 {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileInputStream("input.txt"));
// Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
int[][] round = new int[13][5];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 5; j++) {
round[i][j] = in.nextInt();
}
}
in.nextLine();
int[][] point = new int[13][13];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13; j++) {
point[i][j] = getPoint(round[i], j);
}
}
int[][] state = new int[14][1 << 13];
for (int i = 1; i <= 13; i++) {
Arrays.fill(state[i], -1);
}
int[][] bonusSum = new int[14][1 << 13];
int[][] choice = new int[14][1 << 13];
for (int i = 1; i <= 13; i++) {
for (int j = 0; j < (1 << 13); j++) {
int usedSlot = 0;
for (int b = 0; b < 13; b++) {
if (((1 << b) & j) != 0) {
usedSlot++;
}
}
if (usedSlot != i) {
continue;
}
for (int b = 0; b < 13; b++) {
if (((1 << b) & j) != 0) {
int j2 = (~(1 << b) & j);
int bonus;
if (b < 6) {
bonus = bonusSum[i - 1][j2] + point[i - 1][b];
} else {
bonus = bonusSum[i - 1][j2];
}
int newPoint;
if (bonus >= 63 && bonusSum[i - 1][j2] < 63) {
newPoint = 35 + state[i - 1][j2] + point[i - 1][b];
} else {
newPoint = state[i - 1][j2] + point[i - 1][b];
}
if (newPoint > state[i][j]) {
choice[i][j] = b;
state[i][j] = newPoint;
bonusSum[i][j] = bonus;
}
}
}
}
}
int index = (1 << 13) - 1;
int maxPoint = state[13][index];
boolean bonus = (bonusSum[13][index] >= 63);
int[] mapping = new int[13];
for (int i = 13; i >= 1; i--) {
mapping[choice[i][index]] = i;
index = (~(1 << choice[i][index]) & index);
}
for (int i = 0; i < 13; i++) {
System.out.print(point[mapping[i] - 1][i] + " ");
}
if (bonus) {
System.out.print("35 ");
} else {
System.out.print("0 ");
}
System.out.println(maxPoint);
}
}
static int getPoint(int[] round, int category) {
if (category < 6) {
int sum = 0;
for (int i = 0; i < round.length; i++) {
if (round[i] == category + 1) {
sum += category + 1;
}
}
return sum;
}
int sum = 0;
int[] count = new int[7];
for (int i = 0; i < round.length; i++) {
sum += round[i];
count[round[i]]++;
}
if (category == 6) {
return sum;
} else if (category == 7) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 3) {
return sum;
}
}
} else if (category == 8) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 4) {
return sum;
}
}
} else if (category == 9) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 5) {
return 50;
}
}
} else if (category == 10) {
for (int i = 1; i <= 3; i++) {
if (isStraight(count, i, 4)) {
return 25;
}
}
} else if (category == 11) {
for (int i = 1; i <= 2; i++) {
if (isStraight(count, i, 5)) {
return 35;
}
}
} else if (category == 12) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
if (i != j && count[i] == 3 && count[j] == 2) {
return 40;
}
}
}
}
return 0;
}
static boolean isStraight(int[] count, int start, int num) {
for (int i = start; i < start + num; i++) {
if (count[i] == 0) {
return false;
}
}
return true;
}
}
Here is the working solution.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class P10149 {
static final int MAX_BONUS_SUM = 115;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileInputStream("input.txt"));
// Scanner in = new Scanner(System.in);
long t1 = System.currentTimeMillis();
while (in.hasNextLine()) {
int[][] round = new int[13][5];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 5; j++) {
round[i][j] = in.nextInt();
}
}
in.nextLine();
int[][] point = new int[13][13];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13; j++) {
point[i][j] = getPoint(round[i], j);
}
}
int[][] state = new int[1 << 13][MAX_BONUS_SUM + 1];
int[][] newState = new int[1 << 13][MAX_BONUS_SUM + 1];
for (int j = 0; j < (1 << 13); j++) {
Arrays.fill(state[j], -1);
Arrays.fill(newState[j], -1);
}
state[0][0] = 0;
int[][][] choice = new int[13][1 << 13][MAX_BONUS_SUM + 1];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < (1 << 13); j++) {
int usedSlot = 0;
for (int b = 0; b < 13; b++) {
if (((1 << b) & j) != 0) {
usedSlot++;
}
}
if (usedSlot != i + 1) {
continue;
}
for (int b = 0; b < 13; b++) {
if (((1 << b) & j) != 0) {
int j2 = (~(1 << b) & j);
for (int s = 0; s <= MAX_BONUS_SUM; s++) {
int oldSum;
if (b < 6) {
if (s < point[i][b]) {
s = point[i][b] - 1;
continue;
}
oldSum = s - point[i][b];
} else {
oldSum = s;
}
if (state[j2][oldSum] < 0) {
continue;
}
int newPoint;
if (s >= 63 && oldSum < 63) {
newPoint = 35 + state[j2][oldSum] + point[i][b];
} else {
newPoint = state[j2][oldSum] + point[i][b];
}
if (newPoint > newState[j][s]) {
choice[i][j][s] = b;
newState[j][s] = newPoint;
}
}
}
}
}
for (int j = 0; j < (1 << 13); j++) {
for (int s = 0; s <= MAX_BONUS_SUM; s++) {
state[j][s] = newState[j][s];
}
Arrays.fill(newState[j], -1);
}
}
int index = (1 << 13) - 1;
int maxPoint = -1;
int sum = 0;
for (int s = 0; s <= MAX_BONUS_SUM; s++) {
if (state[index][s] > maxPoint) {
maxPoint = state[index][s];
sum = s;
}
}
boolean bonus = (sum >= 63);
int[] mapping = new int[13];
for (int i = 12; i >= 0; i--) {
mapping[choice[i][index][sum]] = i;
int p = 0;
if (choice[i][index][sum] < 6) {
p = point[i][choice[i][index][sum]];
}
index = (~(1 << choice[i][index][sum]) & index);
sum -= p;
}
for (int i = 0; i < 13; i++) {
System.out.print(point[mapping[i]][i] + " ");
}
if (bonus) {
System.out.print("35 ");
} else {
System.out.print("0 ");
}
System.out.println(maxPoint);
}
long t2 = System.currentTimeMillis();
// System.out.println(t2 - t1);
}
static int getPoint(int[] round, int category) {
if (category < 6) {
int sum = 0;
for (int i = 0; i < round.length; i++) {
if (round[i] == category + 1) {
sum += category + 1;
}
}
return sum;
}
int sum = 0;
int[] count = new int[7];
for (int i = 0; i < round.length; i++) {
sum += round[i];
count[round[i]]++;
}
if (category == 6) {
return sum;
} else if (category == 7) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 3) {
return sum;
}
}
} else if (category == 8) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 4) {
return sum;
}
}
} else if (category == 9) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 5) {
return 50;
}
}
} else if (category == 10) {
for (int i = 1; i <= 3; i++) {
if (isStraight(count, i, 4)) {
return 25;
}
}
} else if (category == 11) {
for (int i = 1; i <= 2; i++) {
if (isStraight(count, i, 5)) {
return 35;
}
}
} else if (category == 12) {
for (int i = 1; i <= 6; i++) {
if (count[i] >= 5) {
return 40;
}
}
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
if (i != j && count[i] == 3 && count[j] == 2) {
return 40;
}
}
}
}
return 0;
}
static boolean isStraight(int[] count, int start, int num) {
for (int i = start; i < start + num; i++) {
if (count[i] == 0) {
return false;
}
}
return true;
}
}
Use Munker's algorithm to solve this problem

Resources