英文:
MergeSort problems parameters
问题
import java.lang.reflect.Array;
public class Sorting {
public static void mergeSort(CompareInt[] arr) {
for (int i = 0; i <= arr.length - 1; i++) {
System.out.println("Initial Arr element:" + arr[i].val);
}
CompareInt[] arrAux;
arrAux = new CompareInt[arr.length];
mergeS(arr, 0, arr.length - 1, arrAux);
}
public static void mergeS(CompareInt[] arr, int startI, int endI, CompareInt[] arrAux) {
if (endI - startI <= 0)
return;
int mid = (startI + endI) / 2;
mergeS(arr, startI, mid, arrAux);
mergeS(arr, mid + 1, endI, arrAux);
arrAux = merge(arr, mid);
for (int i = 0; i <= arr.length - 1; i++) {
arr[i] = arrAux[i];
System.out.println("Arr element:" + arr[i].val);
}
}
public static CompareInt[] merge(CompareInt[] arr, int midpoint) {
int i = 0, j = 0, k = 0;
int n1 = arr.length - midpoint;
int n2 = arr.length - n1;
CompareInt[] L;
CompareInt[] R;
CompareInt[] resA;
L = new CompareInt[n1];
R = new CompareInt[n2];
resA = new CompareInt[n1 + n2];
for (i = 0; i < n1; i++) {
L[i] = arr[i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[midpoint + j + 1];
}
i = 0;
j = 0;
k = 0;
while (i < n1 && j < n2) {
if (L[i].compareTo(R[j]) <= 0) {
resA[k] = L[i];
i++;
} else {
resA[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
resA[k] = L[i];
i++;
k++;
}
while (j < n2) {
resA[k] = R[j];
j++;
k++;
}
return resA;
}
}
Note: The provided code has been translated into Chinese, but please keep in mind that there might still be issues with the code itself, such as logic errors or issues related to the CompareInt
class that is not defined in the provided context. Make sure to thoroughly review and test the code for correctness.
英文:
import java.lang.reflect.Array;
public class Sorting {
public static void mergeSort(CompareInt[] arr) {
for(int i=0;i<=arr.length-1;i++){
System.out.println("Initial Arr element:"+arr[i].val);
}
CompareInt[] arrAux;
arrAux = new CompareInt[arr.length];
mergeS(arr,0,arr.length-1,arrAux);
}
public static void mergeS(CompareInt[] arr,int startI,int endI,CompareInt[] arrAux){
//System.out.println("StartIndex:"+startI+" EndIndex:"+endI);
if(endI-startI<=0)
return;
int mid = (startI+endI)/2;
// System.out.println("Midpoint value:"+mid);
mergeS(arr,startI,mid);
mergeS(arr,mid+1,endI);
// System.out.println("Inside mergeS");
// System.out.println("StartIndex:"+startI+" EndIndex:"+endI+" Midpoint value:"+mid);
// System.out.println("Arr length:"+arr.length);
arrAux = merge(arr,mid);
for(int i=0;i<=arr.length-1;i++){
arr[i]=arrAux[i];
System.out.println("Arr element:"+arr[i].val);
}
}
public static CompareInt[] merge(CompareInt[] arr,int midpoint){
int i=0,j=0,k=0;
int n1 = arr.length-midpoint;
int n2 = arr.length-n1;
//System.out.println("Midpoint value inside merge:"+midpoint);
//System.out.println("N1:"+n1+" N2:"+n2);
CompareInt[] L;
CompareInt[] R;
CompareInt[] resA;
L = new CompareInt[n1];
R = new CompareInt[n2];
resA = new CompareInt[n1+n2];
for(i=0;i<n1;i++){
L[i]=arr[i];
// System.out.println("Inside Left loop"+i);
// System.out.println(L[i].val);
}
for(j=0;j<n2;j++){
R[j]=arr[midpoint+j+1];
// System.out.println("Inside Right loop "+j);
// System.out.println(R[j].val);
}
i=0;
j=0;
k=0;
while(i<n1 && j<n2){
if(L[i].compareTo(R[j])<=0){
resA[k]=L[i];
// System.out.println(resA[i].val);
i++;
}
else{
resA[k]=R[j];
// System.out.println(resA[j].val);
j++;
}
k++;
}
while(i<n1){
resA[k]=L[i];
//System.out.println(resA[k].val);
i++;
k++;
}
while(j<n2){
resA[k]=R[j];
//System.out.println(resA[k].val);
j++;
k++;
}
return resA;
}
}
I am getting actual and formal argument lists differ in length at line mereS(..)
How do I solve this error and is there any way to run with different number of arguments in actual and formal parameters.
This is how Pseudo code is given ..
Modifications in pseudo code:
if (hi - lo <= 0)", not "if (hi - lo <= 1)".
aux <- merge(A[lo:mid], A[mid+1:hi])
答案1
得分: 0
这段代码将解决您的问题:
public static void main(String[] args) {
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("\nSorted array");
for(int i=0;i<=arr.length-1;i++){
System.out.println("Initial Arr element:"+arr[i]);
}
mergeS(arr, 0, arr.length-1);
System.out.println("\nSorted array");
for(int i=0;i<=arr.length-1;i++){
System.out.println("Sorted Arr element:"+arr[i]);
}
}
public static void mergeS(int[] arr,int startI,int endI){
//System.out.println("StartIndex:"+startI+" EndIndex:"+endI)
if (startI< endI)
{
// Find the middle point
int m = (startI+endI)/2;
// Sort first and second halves
mergeS(arr,startI, m);
mergeS(arr , m+1, endI);
// Merge the sorted halves
merge(arr, startI,m, endI);
}
}
public static void merge(int arr[], int startI, int midpoint, int endI){
// Find sizes of two subarrays to be merged
int n1 = midpoint - startI + 1;
int n2 = endI - midpoint;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[startI + i];
for (int j=0; j<n2; ++j)
R[j] = arr[midpoint + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = startI;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
英文:
This code will sort out your problem :
public static void main(String[] args) {
int arr[] = {12, 11, 13, 5, 6, 7};
System.out.println("\nSorted array");
for(int i=0;i<=arr.length-1;i++){
System.out.println("Initial Arr element:"+arr[i]);
}
mergeS(arr, 0, arr.length-1);
System.out.println("\nSorted array");
for(int i=0;i<=arr.length-1;i++){
System.out.println("Sorted Arr element:"+arr[i]);
}
}
public static void mergeS(int[] arr,int startI,int endI){
//System.out.println("StartIndex:"+startI+" EndIndex:"+endI)
if (startI< endI)
{
// Find the middle point
int m = (startI+endI)/2;
// Sort first and second halves
mergeS(arr,startI, m);
mergeS(arr , m+1, endI);
// Merge the sorted halves
merge(arr, startI,m, endI);
}
}
public static void merge(int arr[], int startI, int midpoint, int endI){
// Find sizes of two subarrays to be merged
int n1 = midpoint - startI + 1;
int n2 = endI - midpoint;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[startI + i];
for (int j=0; j<n2; ++j)
R[j] = arr[midpoint + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = startI;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论