0% found this document useful (0 votes)
6 views1 page

Array Programs - Sorting String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Array Programs - Sorting String

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q. Write a program to input 10 names in a SDA.

Using the selection sort technique,


arrange the names of the array in ascending order and display the sorted array.

import java.util.Scanner;
public class sel_sort2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,j,min=0;
String t;
String [] n = new String[10];
System.out.println("Enter 10 names:");
for (i = 0; i < 10; i++)
{
n[i] = sc.nextLine();
}
for (i = 0; i <9; i++)
{
min = i;
for (j = i + 1; j < 10; j++)
{
if (n[j].compareTo( n[min])<0)
{
min = j;
}
}

t = n[min];
n[min] = n[i];
n[i] = t;
}

System.out.println("Sorted array:");
for (i = 0; i<10; i++)
{
System.out.print(n[i]);
}
}
}

You might also like