简体   繁体   中英

How to use recursion to sort an array of strings from smallest to largest?

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++;
        }
    }
}

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

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