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

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]+" ");
}
}
}

Related

error void cannot be converted to java.lang.Integer[]

How do I implement the InsertionSort for an array? Could you please tell me what the correct code would've been in this case:
import javax.swing.JOptionPane;
public class InsertionSort {
public static void insertionSort(Integer [] a ){
for(Integer i = 1; i>= a.length ; i++){
Integer toInsert = a[i];
Integer j = i;
while (j>0 && a[j-1]> toInsert){
a [j] = a [j-1]; // a[j-1] wird eine Stelle nach rechts geschoben
j--;
}
a [j] = toInsert;
}
}
public static void main (String []args){
Integer a1[] = new Integer[8];
for(Integer i=0; i< a1.length;i++){
a1[i] = Integer.parseInt(JOptionPane.showInputDialog("Zahl:"));
}
Integer a2[] = insertionSort(a1);
for(Integer i=0; i<a2.length; i++){
System.out.println(a2[i]+", ");
}
}
}
Here is the correct one:
public class InsertionSort
{
public static void insertionSort(Integer[] a)
{
for (int i = 1; i < a.length; i++)
{
Integer toInsert = a[i];
int j = i;
while (j > 0 && a[j - 1] > toInsert)
{
a[j] = a[j - 1]; // a[j-1] wird eine Stelle nach rechts geschoben
j--;
}
a[j] = toInsert;
}
}
public static void main(String[] args)
{
Integer a1[] = new Integer[8];
for (int i = 0; i < 8; i++)
{
a1[i] = Integer.parseInt(JOptionPane.showInputDialog("Zahl:"));
}
insertionSort(a1);
for (int i = 0; i < a1.length; i++)
{
System.out.println(a1[i] + ", ");
}
}
}

Trouble with recursive merge sort bug

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++;
}
}

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);
}
}

multiplyByConstant method and matrices

public class Matrix{
public double myArray[][];
public Matrix(double a[][]){
this.myArray=a;
}
public Matrix(int b,Vector...vectors) {
double myArray[][] = new double[vectors.length][];
int row = vectors.length;
int column = vectors.length;
for (int i = 0; i < row; i++) {
myArray[i] = new double[column];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if(b==0)
{
myArray[i][j] = vectors[i].getYourArray()[j];
}
else
{
myArray[j][i] = vectors[i].getYourArray()[j];
}
}
}
}
public Matrix(int a){
double [][] t=new double[a][a];
Matrix z=new Matrix(t);
for(int i=0;i<a;i++){
for(int j=0;j<a;j++){
if(i==j) z.myArray[i][j]=1;
else z.myArray[i][j]=0;
}
}
this.myArray=z.myArray;
}
public Matrix multiplyByConstant(double m){ // here
}
}
multiplyByConstatnt: Multiplication by a constant: taking a double as a multiplication factor and multiply every element of the matrix with that factor and return a new matrix.
I have also vector and test class,but i don't know how to use this method with matrix

MergeSort gives StackOverflow error

this is the code for the mergeSort,this gives an stackoverflow error in line 53 and 54(mergeSort(l,m); and mergeSort(m,h);)
Any help will be regarded so valuable,please help me out,i am clueless,Thank you.
package codejam;
public class vector {
static int[] a;
static int[] b;
public static void main(String[] args) {
int[] a1 = {12,33,2,1};
int[] b1 = {12,333,11,1};
mergeSort(0,a1.length);
a1=b1;
mergeSort(0,b1.length);
for (int i = 0; i < a1.length; i++) {
System.out.println(a[i]);
}
}
public static void merge(int l,int m,int h) {
int n1=m-l+1;
int n2 = h-m+1;
int[] left = new int[n1];
int[] right = new int[n2];
int k=l;
for (int i = 0; i < n1 ; i++) {
left[i] = a[k];
k++;
}
for (int i = 0; i < n2; i++) {
right[i] = a[k];
k++;
}
left[n1] = 100000000;
right[n1] = 10000000;
int i=0,j=0;
for ( k =l ; k < h; k++) {
if(left[i]>=right[j])
{
a[k] = right[j];
j++;
}
else
{
a[k] = left[i];
i++;
}
}
}
public static void mergeSort(int l,int h) {
int m =(l+h)/2;
if(l<h)
{
mergeSort(l,m);
mergeSort(m,h);
merge(l,m,h);;
}
}
}
Following is the recursive iterations table of the mergeSort function with argument l=0 and h=4
when the value of l is 0 and value of h is 1 , expression calculate m value which turn out to be 0 but we are checking condition with h which is still 1 so 0<1 become true , recursive calls of this mergeSort function forms a pattern , this pattern doesn't let the function to terminate , stack runs out of memory , cause stackoverflow error.
import java.lang.*;
import java.util.Random;
public class MergeSort {
public static int[] merge_sort(int[] arr, int low, int high ) {
if (low < high) {
int middle = low + (high-low)/2;
merge_sort(arr,low, middle);
merge_sort(arr,middle+1, high);
arr = merge (arr,low,middle, high);
}
return arr;
}
public static int[] merge(int[] arr, int low, int middle, int high) {
int[] helper = new int[arr.length];
for (int i = 0; i <=high; i++){
helper[i] = arr[i];
}
int i = low;
int j = middle+1;
int k = low;
while ( i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
arr[k++] = helper[i++];
} else {
arr[k++] = helper[j++];
}
}
while ( i <= middle){
arr[k++] = helper[i++];
}
while ( j <= high){
arr[k++] = helper[j++];
}
return arr;
}
public static void printArray(int[] B) {
for (int i = 0; i < B.length ; i++) {
System.out.print(B[i] + " ");
}
System.out.println("");
}
public static int[] populateA(int[] B) {
for (int i = 0; i < B.length; i++) {
Random rand = new Random();
B[i] = rand.nextInt(20);
}
return B;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int A[] = new int[10];
A = populateA(A);
System.out.println("Before sorting");
printArray(A);
A = merge_sort(A,0, A.length -1);
System.out.println("Sorted Array");
printArray(A);
}
}

Resources