Search code examples
javaarrayssortingselection-sort

Java: Selection sort of names entered by the user in alphabetical order


I already have a code for Selection sort using an array:

public class SelectionSortTest
{ 

 public static void main(String[] args)
 {
      String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"};

      System.out.println("Unsorted array:");
      for(String element: stringArray)
           System.out.print(element + " ");
      System.out.println();

      selectionSort(stringArray);

      System.out.println("Sorted array:");
      for(String element: stringArray)
            System.out.print(element + " ");
      System.out.println();
 }


 static void selectionSort(Comparable[] array)
 {
      int smallindex;
      for(int i = 0; i < array.length; i++)
      {
           smallindex = i; // set first element as smallest
           for(int j = i + 1; j < array.length; j++) // find smallest
                if(array[j].compareTo(array[smallindex]) < 0)
                     smallindex = j;
           if(smallindex != i)
                swap(array, smallindex, i);
      }
 }


 static void swap(Object[] array, int index1, int index2)
 {
      Object temp = array[index1];
      array[index1] = array[index2];
      array[index2] = temp;
 } 
}

But i want to change this so that the user has to input those names instead. Such that the outprint keeps asks "Enter the names" and you put whatever amount of names you want (separated by commas) then does the above task.

i Have tried replacing

String[] stringArray = {"Flash", "Green Arrow", "superman", "spiderman", "green lantern"}; 

with:

Scanner input = new Scanner(System.in);

for (int i = 0; < array.length; i++)
{
System.out.print("Enter names " + (i + 1) + ": ");
array[i] = input.next():    

But it doesn't work. Any suggestions?


Solution

  • Never mind guys, i figured out how to do it:

    import java.util.Scanner;
    
    public class selectionsort
    { 
    public static void main (String args[])
    {
        Scanner in = new Scanner (System.in);
        System.out.print ("Enter the number of names you wish to enter: ");
        int n = in.nextInt();
        String array[] = new String [n]; //Array to store the names in.
    
    in.nextLine(); // < --- an extra next Line
    
    for (int i = 0; i < array.length; i++)
    {
        System.out.println("Please enter the name: ");
        array[i]= in.nextLine();
    
    }
       System.out.println("Unsorted array:");
          for(String element: array)
               System.out.print(element + " ");
          System.out.println();
    
          selectionSort(array);
          System.out.println("SelectionSorted array:");
          for(String element: array)
                System.out.print(element + " ");
          System.out.println();
    
    
    }
    static void selectionSort(Comparable[] array)
     {
          int smallindex;
          for(int i = 0; i < array.length; i++)
          {
               smallindex = i; // set first element as smallest
               for(int j = i + 1; j < array.length; j++) // find smallest
                    if(array[j].compareTo(array[smallindex]) < 0)
                         smallindex = j;
               if(smallindex != i)
                    swap(array, smallindex, i);
          }
     }
    static void swap(Object[] array, int index1, int index2)
     {
          Object temp = array[index1];
          array[index1] = array[index2];
          array[index2] = temp;
     } 
    
    
    }