0
import java.io.File;
import java.util.Scanner;

public class TestDriver {

public static void main(String[] args) {
    Scanner x = null;
    try {
        x = new Scanner(new File("pokemon"));
    } catch (Exception e) {
        System.out.println("could not find file");
    }

    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    System.out.println("Type in the number of Pokemons (1-15)!");
    int userNumber = 0;
    boolean userFalse = false;
    while (!userFalse) { // Validates user inputs for years
        if (input.hasNextInt()) {
            int temp = input.nextInt();
            if (temp < 1 || temp > 15) { // Years cannot be below 0
                System.out.println("Invalid input.");
                userFalse = false;
            } else {
                userFalse = true;
                userNumber = temp;
            }
        } else {
            System.out.println("Try Again!");
            input.next();
        }
    }
    String[] a = new String[userNumber];
    for (int i = 0; i < userNumber; i++) {
        a[i] = x.next();
        System.out.print(a[i] + " ");
    }
    sort(a, userNumber);
}

In the pokemon.txt, it reads

Gyarados  
Lapras 
Eevee  
Vaporeon 
Snorlax
Abra
Slowbro
Rhyson
Kyogre
Blastoise
Jigglypuff
Miltank
Lugia
Steelix
Arbok

I am trying to sort the Pokemon NAMES from smallest to largest. I don't know the best way to do this. My teacher wants me to do it using recursion. Is that the same thing as quicksort or mergesort? Thanks in advance. edit: Here's my attempt to sort using mergesort:

public static void sort(String[] pokemon, int userNumber) {

    String[] a = new String[pokemon.length / 2]; // Split array into two
    String[] b = new String[pokemon.length - a.length]; // halves, a and b
    for (int i = 0; i < pokemon.length; i++) {
        if (i < a.length)
            a[i] = a[i];
        else
            b[i - a.length] = pokemon[i];
    }

    sort(a, userNumber); // Recursively sort first
    sort(b, userNumber); // and second half.

    int ai = 0; // Merge halves: ai, bi
    int bi = 0; // track position in
    while (ai + bi < pokemon.length) { // in each half.
        if (bi >= b.length || (ai < a.length && a[ai].length() < b[bi].length())) {
            pokemon[ai + bi] = a[ai]; // (copy element of first array over)
            ai++;
        } else {
            pokemon[ai + bi] = b[bi]; // (copy element of second array over)
            bi++;
        }
    }
}
7
  • here you can find a similar question link Commented Oct 31, 2016 at 4:16
  • Its unclear from your code... are you trying to sort the file or the users input? You show nothing related to any sort method. I would say google is your friend here... look up recursive quicksort or such. Commented Oct 31, 2016 at 4:18
  • 1
    That is a lot of code that has nothing to do with your question. Please remove any unnecessary code. Commented Oct 31, 2016 at 4:46
  • @JohnG The user input is suppose to enter how many Pokemon they want to sort, but that doesn't really matter. I'm just not sure how to create a sorting method or just sort in general an array of STRINGS based upon how many letters it has. What is the best way to approach this? I think the sorting is suppose to include the swapping from one location to another location. Is this related to mergesort or quicksort? Commented Oct 31, 2016 at 4:57
  • I am not absolutely sure... but at the line...sort(a, userNumber); you will never go beyond there. I do not see a stopping condition above it. So this will cause an infinite loop and fail. Also the userNumber variable is unnecessary. Commented Oct 31, 2016 at 5:17

1 Answer 1

0

Quicksort and Mergesort can both be implemented recursively. Since this is homework I will not be providing actual code but I will link you to a few places you can look:

  1. Merge Sort
  2. Quick Sort
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.