English

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,

Advertisements
Advertisements

Question

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.

Code Writing
Advertisements

Solution

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
  Is there an error in this question or solution?
Chapter 8: Arrays - Sort & Search - Exercises [Page 200]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 8 Arrays - Sort & Search
Exercises | Q 4. (ii) | Page 200
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×