简体   繁体   中英

Count swaps in mergesort

I'm trying to count minimum swaps(only consecutive swaps) in an array sorted by mergesort. It works for some cases but this case doesn't work for example: http://puu.sh/kC9mg/65e055807f.png . The first number is how many numbers it should sort, then you enter the numbers. In that case it should print 1 because the number of minimum consecutive swaps is 1 in this case which will swap 4 and 3.

This is the code I have:

public class MergeSort {

private int[] numbers;
private int[] helper; 
private int number;
private long swapCounter = 0;

public MergeSort(int[] inputNumbers)
{
    numbers = inputNumbers;
    number = inputNumbers.length;
    helper = new int[number];
    mergesort(0, number-1);

}

private void mergesort(int low, int high) { 
     // check if low is smaller then high, if not then the array is sorted
    if (low < high) {
      // Get the index of the element which is in the middle
      int middle = low + (high - low) / 2;
      // Sort the left side of the array
      mergesort(low, middle);
      // Sort the right side of the array
      mergesort(middle + 1, high);
      // Combine them both
      merge(low, middle, high);
    }
  }

private void merge(int low, int middle, int high) { // Merge it

    long internCounter = 0;

    for (int i = low; i <= high; i++) { // Copy both parts into the helper array
      helper[i] = numbers[i];
    }

    int i = low;
    int j = middle + 1;
    int k = low;

    while (i <= middle && j <= high) { // Copy the smallest values from either the left or the right side back to the original array

      if (helper[i] < helper[j]) {
        numbers[k] = helper[i];
        i++;
        internCounter++;
      } else {
        numbers[k] = helper[j];
        j++;
        swapCounter += internCounter;
      } 
      k++;
    }

    while (i <= middle) { // Copy the rest of the left side of the array into the target array
      numbers[k] = helper[i];
      k++;
      i++;
      swapCounter += internCounter;
    }

  }

public long getCounter() // Get the counter
{
    return this.swapCounter;
}

}

And this is my main class: import java.util.Scanner;

public class Fr {   

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int numberOfStudents;
    int[] inputNumbers2;

    numberOfStudents = input.nextInt();
    inputNumbers2 = new int[numberOfStudents];

    for(int i = 0; i < numberOfStudents; i++)
    {               
        inputNumbers2[i] = input.nextInt();
    }

    MergeSort ms = new MergeSort(inputNumbers2);    

    System.out.println(ms.getCounter());

    input.close();
}


}

Do anyone have any thoughts what could be wrong?

I don't think you need the intern counter at all. Just count how many positions you swap an element when taking an element from the high half:

public class MergeSort {

private int[] numbers;
private int[] helper; 
private int number;
private long swapCounter = 0;

public MergeSort(int[] inputNumbers)
{
    numbers = inputNumbers;
    number = inputNumbers.length;
    helper = new int[number];
    mergesort(0, number-1);

}

private void mergesort(int low, int high) { 
     // check if low is smaller then high, if not then the array is sorted
    if (low < high) {
      // Get the index of the element which is in the middle
      int middle = low + (high - low) / 2;
      // Sort the left side of the array
      mergesort(low, middle);
      // Sort the right side of the array
      mergesort(middle + 1, high);
      // Combine them both
      merge(low, middle, high);
    }
  }

private void merge(int low, int middle, int high) { // Merge it

    for (int i = low; i <= high; i++) { // Copy both parts into the helper array
      helper[i] = numbers[i];
    }

    int i = low;
    int j = middle + 1;
    int k = low;

    while (i <= middle && j <= high) { // Copy the smallest values from either the left or the right side back to the original array

      if (helper[i] < helper[j]) {
        numbers[k] = helper[i];
        i++;
      } else {
        numbers[k] = helper[j];
        swapCounter += (j-k);
        j++;
      } 
      k++;
    }

    while (i <= middle) { // Copy the rest of the left side of the array into the target array
      numbers[k] = helper[i];
      k++;
      i++;
    }

  }

    public long getCounter() // Get the counter
    {
        return this.swapCounter;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM