Public class SelectionSort The method for sorting the numbe

Public class SelectionSort {/** The method for sorting the numbers */public static void selectionSort(double[] list) {for (int I = 0; I

Solution

SortStrings.java


import java.util.Arrays;
import java.util.Scanner;


public class SortStrings {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       while(true){
       System.out.println(\"Enter the list size: \");
       int n = scan.nextInt();
       scan.nextLine();
       String names[] = new String[n];
       for(int i=0; i<names.length; i++){
           System.out.print(\"Enter Name \"+(i+1)+\": \");
           names[i] = scan.nextLine();
       }
       System.out.println(\"List Before sorting: \");
       System.out.println(Arrays.toString(names));
       selectionSort(names);

System.out.println(\"List After sorting: \");
       System.out.println(Arrays.toString(names));
       System.out.println(\"Do you want to continue(y or n): \");
       char ch = scan.next().charAt(0);
       if(ch == \'n\' || ch ==\'N\'){
           break;
       }
       }
   }

   public static void selectionSort(String arr[]){
       for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j].compareTo(arr[index]) < 0)
index = j;
  
String str = arr[index];
arr[index] = arr[i];
arr[i] = str;
}
   }

}

Output:

Enter the list size:
5
Enter Name 1: Suresh
Enter Name 2: Skhar
Enter Name 3: Murapaka
Enter Name 4: Anshu
Enter Name 5: Revathi
List Before sorting:
[Suresh, Skhar, Murapaka, Anshu, Revathi]
List After sorting:
[Anshu, Murapaka, Revathi, Skhar, Suresh]
Do you want to continue(y or n):
n

 Public class SelectionSort {/** The method for sorting the numbers */public static void selectionSort(double[] list) {for (int I = 0; I SolutionSortStrings.jav
 Public class SelectionSort {/** The method for sorting the numbers */public static void selectionSort(double[] list) {for (int I = 0; I SolutionSortStrings.jav

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site