Page 289 - Computer science 868 Class 12
P. 289

26          }
                   27

                   28          public static void main(String args[])
                   29          {
                   30              BubbleSort ob = new BubbleSort();

                   31              System.out.println("Before sorting array");

                   32              ob.printArray();
                   33              ob.bubbleSort();
                   34              System.out.println("Sorted array");

                   35              ob.printArray();
                   36          }

                   37      }

                 The output of the preceding program is as follows:
                 Before sorting array
                 64 34 25 12 22 11 90

                 Sorted array
                 11 12 22 25 34 64 90


                 Selection Sort
                 In the selection sort technique, an array is checked repeatedly to find the minimum element if it is to be sorted in
                 ascending order from the unsorted part and then putting it at the beginning. Thus the smallest element is selected
                 from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array.
                 This process continues moving unsorted array boundary by one element to the right.


                   Program 7     Write a program to sort an array that contains the names of the cities using Selection Sort.

                   1       class SelectionSort

                   2       {

                   3           String city[] = { "Mumbai", "Kolkata" , "Delhi" , "Chennai" , "Amritsar" };
                   4           void selection()
                   5           {

                   6               int i,j,n, minpos;
                   7               String min, temp;

                   8               n = city.length;
                   9               for (i = 0; i < n-1 ; i++)

                   10              {
                   11                  min=city[i];




                                                                                                                       287
                                                                                                              Arrays   287
   284   285   286   287   288   289   290   291   292   293   294