मराठी

Write a program for the following. Create a string array ST[ ] to assign the following color names. Arrange the names in ascending order using Bubble Sort -Violet, Indigo, Blue, Green, Yellow,

Advertisements
Advertisements

प्रश्न

Write a program for the following.

Create a string array ST[ ] to assign the following color names. Arrange the names in ascending order using Bubble Sort -Violet, Indigo, Blue, Green, Yellow, Orange, Red.

[Hint: (StringArray[p].compareTo(StringArray[p + 1]) > 0)indicates a swap is needed]

कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clTask
{
   void main()
   {
       String ST[] = {"Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"};
       for(int j= 1; j <=ST.length -1; j++)
       {
          for(int k = 0; k <= ST.length - 2; k++)
          {
             if(ST[k].compareTo(ST[k + 1]) > 0)
             {
                 String tmp = ST[k];
                 ST[k] = ST[k + 1];
                 ST[k+1] = tmp;
             }
          }
       }
       System.out.println("In Ascending Order : ");
       for(int p = 0; p <= ST.length - 1; p++)
       {
          System.out.print(" " + ST[p]);
       }
   }
}

Output:

In Ascending order: 

Blue Green Indigo Orange Red Violet Yellow

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 8: Arrays - Sort & Search - Exercises [पृष्ठ २००]

APPEARS IN

रूपा पंडित Computer Applications [English] Class 10 ICSE
पाठ 8 Arrays - Sort & Search
Exercises | Q 4. (i) | पृष्ठ २००
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×