Advertisements
Advertisements
प्रश्न
Write a program for the following.
Create a String array SB[ ] to assign the following subject names. Arrange the names in descending order using Selection Sort:
Hindi, English, Mathematics, History, Civics, Geography, Science, Music, Art, Fitness.
कोड लेखन
Advertisements
उत्तर
import java.util.*;
class clTask
{
void main()
{
String SB[ ] = {"Hindi", "English", "Mathematics", "History" "Civics", "Geography", "Science", "Music", "Art", "Fitness"};
for(int j = 0; j <= SB.length-2; j++)
{
String max = SB[j];
int p =j;
for(int k = j + 1; k <= SB.length –1; k++)
{
if(max.compareTo(SB[k]) <0)
{
max = SB[k];
p = k;
}
}
String tmp = SB[j];
SB[j] = SB[p];
SB[p] = tmp;
}
System.out.println ("In Descending order: ");
for(int j = 0; j <= SB.length -1; j++)
{
System.out.print(" /t " + SB[j]);
}
}
}
Output:
In descending order:
Science Music Mathematics History Hindi Geography Fitness English Civics Art
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 8: Arrays - Sort & Search - Exercises [पृष्ठ २००]
