0

Need help with an assignment for Data Structures and Algorithms. It's a two-part assignment where we have to implement two sorting algorithms: DistributionCountingSort and InsertionSort. I have the algorithm parts completed, but I'm a little stuck on how to implement the second part of the assignment.

For the moment, I have an array initialized in the main of both programs for testing purposes, and I pass that array through both sorting methods. I already have part of the program that finds the min and max value of the array but I'm not sure how that would work when passing a .txt as an argument in the terminal. They both appear to work but I'll post them here as a reference.

Part of the assignment requires four command line parameters:

  1. The first parameter is an integer that specifies how many integers are to be sorted by the algorithm.
  2. The second parameter specifies the path of the text file
  3. The third parameter specifies the minimum value integer from the text file
  4. The fourth parameter specifies the maximum value integer the text file.

I'll admit that my command-line skills are rusty, but I want to learn to do this the right way for future reference as a way of building my testing skills.

Thanks in advance.

distributionCountingSort:

import java.util.Arrays;

public class distributionCountingSort {
  private static void sort(int[] arr) {
    int arrayLength = arr.length;
    if (arrayLength == 0) return;
    int max = arr[0], min = arr[0];
    for (int i = 1; i < arrayLength; i++) {
      if (arr[i] > max) {
        max = arr[i];
      }
      if (arr[i] < min) {
        min = arr[i];
      }
    }
    int range = max - min + 1;

    int[] count = new int[range];
    for (int value : arr) count[value - min]++;
    for (int i = 1; i < range; i++) count[i] += count[i - 1];
    int j = 0;
    for (int i = 0; i < range; i++) while (j < count[i]) arr[j++] = i + min;
    System.out.println("Length of the array: " + arrayLength);
    System.out.println("Max value: " + max);
    System.out.println("Min value: " + min);
  }

  public static void main(String[] args) {
    int[] testArray = {10, 13, 9, 15, 7, 13};
    System.out.println("Elements before applying countingSort: " + Arrays.toString(testArray));
    sort(testArray);
    System.out.println("Elements after apply countingSort: " + Arrays.toString(testArray));
  }
}

insertionSort:

import java.util.Arrays;

public class insertionSort2 {
  public static void main(String[] args) {
    int[] unsorted = {
      5, 6, 4, 2, 3, 1,
    };
    System.out.println("Number of elements in unsorted array: " + unsorted.length);
    sort(unsorted);
    System.out.println(Arrays.toString(unsorted));
  }

  private static void sort(int[] array) {
    int n = array.length;
    for (int i = 1; i < n; i++) {
      int j = array[i];
      int k = i - 1;
      while ((k > -1) && (array[k] > j)) {
        array[k + 1] = array[k];
        k--;
      }
      array[k + 1] = j;
    }
  }
}
Soup
  • 23
  • 4
  • you don't pass the text file as argument, only the full path to the text file – jhamon Feb 23 '20 at 00:48
  • And at least from there you can take whatever's inside the text file and store it in an array that can be passed through both sorting methods? That's what I thought but the professor wasn't very clear. – Soup Feb 23 '20 at 03:17

0 Answers0