2

Merge sorting, sorting by dividing a random array in half and then putting them in numeric order. Concept is called "Divide and Conquer." The output is out of order and I don't see anything wrong with this code. Main just outputs all the numbers in the array. FYI, other parts of the code isn't the problem. But if you need it I can give it to you.

private void merge(int[] a, int first, int mid, int last)
{
    int size = last - first + 1;
    int [] temp = new int[size];
    int i = first, j = mid + 1;
    for(int s = 0; s < size; s++){ // a.length
        if(i > mid){ // case a
            temp[s] = a[j];
            j++;
        }else if(j > last){ // case b
            temp[s] = a[i];
            i++;
        }else if(a[i] < a[j]){ // case c
            temp[s] = a[i];
            i++;
        }else if(a[j] <= a[i]){ // case d
            temp[s] = a[j];
            j++;
        }
    }

    for(int s = first; s < size; s++){
        a[first] = temp[s - first];
    }
}

public void mergeSort(int[] a, int first, int last)
{
    int size = last - first + 1, mid;
    if(size == 1){
        steps++;
    }else if(size == 2){
        if(a[last] > a[first]){
            int temp = a[last];
            a[last] = a[first];
            a[first] = temp;
            steps += 3;
        }
    }else{
        mid = (last + first) / 2;
        mergeSort(a, first, mid);
        mergeSort(a, mid + 1, last);
        merge(a, first, mid, last);
        steps += 4;
    }
}

This is what the generator looks like:

private void fillArray(int numInts, int largestInt)
{
    myArray = new int[numInts];
    Random randGen = new Random();

    for(int loop = 0; loop < myArray.length; loop++){
        myArray[loop] = randGen.nextInt(largestInt) + 1;
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Stack Over
  • 21
  • 3
  • 2
    Have you tried using a debugger? You won't always be able to rely on SO for such questions. – Tim Biegeleisen Feb 10 '17 at 03:29
  • @TimBiegeleisen Yes I have used a debugger. – Stack Over Feb 10 '17 at 03:31
  • @StackOver - why not change the if's and copies from a[] to temp[] in merge() to sort in the order you want into temp, then change the for loop to use `a[s] = temp[s];` ? The code in mergesort() sorts an sub-array of size 2 in descending order, while it seems the code in merge sorts in ascending order. You need to make them the same (both ascending or both descending). – rcgldr Feb 10 '17 at 13:03
  • @StackOver - `divide and conquer` - no sorting takes place until repeated division produces an sub-array of size 1 or 2, which will be followed by the first instance of merge(). – rcgldr Feb 10 '17 at 13:09

1 Answers1

1

there are two flaws in your code:

first:

for(int s = first; s - first < size; s++){// replace s<size with s-first<size
        a[s] = temp[s - first];//yours a[first] = temp[s-first] 
}

in your coding, first is fixed and it will always update the a[first] which I think is not what you want.

second:

....
}else if(a[i] > a[j]){ // case c  yours a[i]<a[j]
            temp[s] = a[i];
            i++;
}else if(a[i] <= a[j]){ // case d  yours a[j] <= a[i]
            temp[s] = a[j];
            j++;
}
.... 

because about your sort you will get a descend sequence and in merge you want get ascend order, this conflict with each other.

nail fei
  • 2,179
  • 3
  • 16
  • 36
  • Doing this just gives me the same problem. It doesn't put the random array in numeric order. But thanks. – Stack Over Feb 10 '17 at 04:27
  • @Stack Over I have repaired it for you, and it can put the random array in numeric order. The comment is yours code and I have fixed them. – nail fei Feb 10 '17 at 04:32
  • @Stack Over give me your input. – nail fei Feb 10 '17 at 04:33
  • This method generates random numbers, depending how much numbers you want in the array (numInts) and the largest int you want it to go up to (largestInt). private void fillArray(int numInts, int largestInt) { myArray = new int[numInts]; Random randGen = new Random(); for(int loop = 0; loop < myArray.length; loop++){ myArray[loop] = randGen.nextInt(largestInt) + 1; } } – Stack Over Feb 10 '17 at 04:39
  • I have edited my question, so if it's hard to see, it's easier up top. – Stack Over Feb 10 '17 at 04:43