Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8881521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:21:51+00:00 2026-06-14T20:21:51+00:00

I am doing a project for a CS 140 class at my university and

  • 0

I am doing a project for a CS 140 class at my university and I am trying to create a program that uses selection sort with an array of the 50 states of the US with fields for name, population, capital and region. Here is the array given:

private static void loadStates() {
    // Set the search key to be the state name.
    State.setKey("name");

    us = new Country("United States", 311591917, "Washington DC",
            new State[]{
                new State("Alabama", 4802740, "Montgomery", "Southeast"),
                new State("Alaska", 722718, "Juneau", "West"),
                new State("Arizona", 6482505, "Phoenix", "Southwest"),
                new State("Arkansas", 2937979, "Little Rock", "Southeast"),
                new State("California", 37691912, "Sacramento", "West"),
                new State("Colorado", 5116769, "Denver", "West"),
                new State("Connecticut", 3580709, "Hartford", "Northeast"),
                new State("Delaware", 907135, "Dover", "Northeast"),
                new State("Florida", 19057542, "Tallahassee", "Southeast"),
                new State("Georgia", 9815210, "Atlanta", "Southeast"),
                new State("Hawaii", 1374810, "Honolulu", "West"),
                new State("Idaho", 1584985, "Boise", "West"),
                new State("Illinois", 12869257, "Springfield", "Midwest"),
                new State("Indiana", 6516922, "Indianapolis", "Midwest"),
                new State("Iowa", 3062309, "Des Moines", "Midwest"),
                new State("Kansas", 2871238, "Topeka", "Midwest"),
                new State("Kentucky", 4369356, "Frankfurt", "Southeast"),
                new State("Louisiana", 4574836, "Baton Rouge", "Southeast"),
                new State("Maine", 1328188, "Augusta", "Northeast"),
                new State("Maryland", 5828289, "Annapolis", "Northeast"),
                new State("Massachusetts", 6587536, "Boston", "Northeast"),
                new State("Michigan", 9876187, "Lansing", "Midwest"),
                new State("Minnesota", 5344861, "St. Paul", "Midwest"),
                new State("Mississippi", 2978512, "Jackson", "Southeast"),
                new State("Missouri", 6010688, "Jefferson City", "Midwest"),
                new State("Montana", 998199, "Helena", "West"),
                new State("Nebraska", 1842641, "Lincoln", "Midwest"),
                new State("Nevada", 2723322, "Carson City", "West"),
                new State("New Hampshire", 1318194, "Concord", "Northeast"),
                new State("New Jersey", 8821155, "Trenton", "Northeast"),
                new State("New Mexico", 2082224, "Santa Fe", "Southwest"),
                new State("New York", 19465197, "Albany", "Northeast"),
                new State("North Carolina", 9656401, "Raleigh", "Southeast"),
                new State("North Dakota", 683932, "Bismarck", "Midwest"),
                new State("Ohio", 11544951, "Columbus", "Midwest"),
                new State("Oklahoma", 3791508, "Oklahoma City", "Southwest"),
                new State("Oregon", 3871859, "Salem", "West"),
                new State("Pennsylvania", 12742886, "Harrisburg", "Northeast"),
                new State("Rhode Island", 1051302, "Providence", "Northeast"),
                new State("South Carolina", 4679230, "Columbia", "Southeast"),
                new State("South Dakota", 824082, "Pierre", "Midwest"),
                new State("Tennessee", 6403353, "Nashville", "Southeast"),
                new State("Texas", 25674681, "Austin", "Southwest"),
                new State("Utah", 2817222, "Salt Lake City", "West"),
                new State("Vermont", 4802740, "Montpelier", "Northeast"),
                new State("Virginia", 8096604, "Richmond", "Southeast"),
                new State("Washington", 6830038, "Olympia", "West"),
                new State("West Virginia", 1855364, "Charleston", "Southeast"),
                new State("Wisconsin", 5711767, "Madison", "Midwest"),
                new State("Wyoming", 568158, "Cheyenne", "West")
            });
} // end loadStates()

And so on for every state. Here’s what I have for my selection sort. It’s split into three methods as what is required by the professor.

private static void selectionSort(State[] states, String onField) {
    for (int top = 0; top < states.length - 1; top++) {
        swap(states, top, indexOfMinValueInArray(states, top, onField));
    }
} // end selectionSort()

private static int indexOfMinValueInArray(State[] states, int startAt,
        String onField) {
    int minIndex = startAt;
    if ("name".equals(onField)) {
        for (int index = startAt; index < states.length; index++) {
            if (states[index].getName().compareTo(states[minIndex].getName()) < 0) {
                minIndex = index;
            }
        }
    } else if ("population".equals(onField)) {
        for (int index = startAt; index < states.length; index++) {
            if (states[index].getPopulation() < states[minIndex].getPopulation()) {
                minIndex = index;
            }
        }
    }
    return minIndex;
}// end indexOfMinValueInArray()

private static void swap(State[] states, int index1, int index2) {
    State temp = states[index1];
    states[index1] = states[index2];
    states[index2] = temp;
}

Basically what this is supposed to do is take a “key” or, as stated in the methods, the “onField” which is supposed to tell the selection sort what to sort the array by, either by name, population, capital, or region. That is what I’m lost at. Could someone provide some direction on how to correct my selection sort? Also, here is the beginning of the code so you see the direction I need to take this.

public static void main(String[] args) {
    loadStates();

    State[] sortedStates = new State[50];
    for (int i = 0; i <= 49; i++) {
        sortedStates[i] = us.getStateAtIndex(i);
    }

    // TODO: Sort the states by population.
    selectionSort(sortedStates, "population");

    // TODO: List the states by population.
    listStates(sortedStates);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:21:52+00:00Added an answer on June 14, 2026 at 8:21 pm

    I’d suggest defining four Comparator objects, one for each field as the key. For instance, for the name field:

    public class NameComparator implements Comparator<State> {
        public int compare(State s1, State s2) {
            return s1.name.compareTo(s2.name);
        }
    }
    

    The other comparators can be defined similarly. Then you can define your selection sort to use a Comparator<State> to do the item comparisons and you can pick which comparator to use based on the key designation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a project that aims to create a PDF using the exiftool library
I'm doing project on object detection using javacv in that I went through couple
I am doing project on java. In one of the class, I am working
I am doing a project that recognize human face from camera. Here is the
I'm currently doing a project for a Beginner C programming class where i'm supposed
Currently am doing project on Family Business. In that I need some video calling
I am doing project in powerpoint 2007 automation. In that i am using macro
I am doing a project that has dependencies on some classes from the mahout
I am doing Project by asp.net. Some states of changing themes of page (setting
I am doing project at home that involves 4 buttons in the left and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.