Page 361 - ComputerScience_Class_11
P. 361

11.10 LINEAR SEARCH VS. BINARY SEARCH
                 The differences between linear search and binary search are given below:

                                       Linear Search                                   Binary Search
                      1.  Array elements may or may not be in sorted order.  1.  Array elements should be in sorted order.

                      2.  Execution is slow.                          2.  Execution is fast.

                      3.  Searching begins from the first element and proceeds  3.  Search begins from the middle element and proceeds
                        to the last.                                    either  to the left or to the right depending  on  the
                                                                        value of the searched element.


                     11.11 BUBBLE SORT VS. SELECTION SORT
                 The differences between bubble sort and selection sort are given below:

                                        Bubble Sort                                    Selection Sort

                      1.  It compares the number with the next element and  1.  It selects the minimum  element from the unsorted
                        swaps if the condition matches.                 part of the array and places it in the next position in
                                                                        the sorted part of the array.

                      2.  Execution is slow.                          2.  Execution is fast.
                      3.  It is less efficient.                       3.  It is more efficient.


                     11.12 SINGLE-DIMENSIONAL ARRAY VS. DOUBLE-DIMENSIONAL ARRAY
                 The differences between a single-dimensional array and a double-dimensional array are given below:

                                  Single-Dimensional Array                       Double-Dimensional Array

                      1.  It contains a single row or a list of elements.  1.  It  contains  multiple  rows,  each  having multiple
                                                                        columns.
                      2.  The variables are having the same name with a single  2.  The variables are having the same name with  two
                        subscript value.                                subscript values one for row and one for column.
                      3.  To declare the array:                      3.  To declare the array:

                        int ar[ ]=new int[10];                         int ar[ ][ ]=new int[2][4];


                     11.13 FINDING MAXIMUM OR MINIMUM
                 To find the maximum or minimum value in an array, you can iterate through the array, comparing each element to a
                 running current maximum or minimum. The following program shows how to find the maximum and minimum in an
                 array:


                   Program 21    Write a Java program to find the maximum and minimum values in an array.

                   1       public class Main

                   2       {
                   3           public static void main(String[] args)

                   4           {





                                                                                                           Arrays  359
   356   357   358   359   360   361   362   363   364   365   366