Page 90 - CTS - CSA TP - Volume 2
P. 90

COMPUTER SOFTWARE APPLICATION - CITS





                       System.out.print(“Element “ + (i + 1) + “: “);
                       arr[i] = scanner.nextInt();
                   }



                   // Bubble sort in descending order
                   for (int i = 0; i < n - 1; i++) {
                       for (int j = 0; j < n - i - 1; j++) {
                           if (arr[j] < arr[j + 1]) {

                               // Swap arr[j] and arr[j+1]
                               int temp = arr[j];
                               arr[j] = arr[j + 1];
                               arr[j + 1] = temp;
                           }
                       }
                   }


                   // Display the sorted array in descending order
                   System.out.println(“Array elements in descending order:”);
                   for (int i = 0; i < n; i++) {
                  System.out.println(arr[i]);
                   }


                   scanner.close(); // Close the scanner to avoid resource leak
               }
           }

           Output:


































                                                           75
                                CITS : IT & ITES - Computer Software Application - Exercise 93
   85   86   87   88   89   90   91   92   93   94   95