Arrays, Sorting and Searching

Arrays:

  • Arrays are means of storing a list of items.
  • Multiple values of same type can be stored together

  • Ex:
       int marks[];
       marks=new int[25];
       int marks[]=new int[25];
       int[] marks=new int[25];
       int[][] myMarks=new myMarks[5][5];

Sorting

  • This is the operation of arranging the elements of a table into some sequential order according to ordering criteria.
  • The sort is performed according to the key value of each of the elements. Depending on the structure of the key, elements can be sorted numerically, alphabetically or alphanumerically.
  • For example in numerical sorting, the elements are arranged in ascending or descending order according to the numerical value of each of the elements.
  • There different techniques to perform the sorting. We are discussing two of such techniques and they are selection sort and bubble sort.
Selection Sort:
  • In this beginning with the first element of the array we search for the smaller element in the entire array.
  • When this element is found it is interchanged with the first element of the array, during the next pass, the second smallest element is found out.
  • This process is continued until the nth smallest element is found which is the largest element of the array of n elements and hence occupies the last position in the sorted array.
Bubble Sort:
  • Bubble sort differs from selection sort. In this instead of finding smallest element and then performing an interchange two elements are interchanged soon after discovering that they are out of order.
  • When the approach is used during the first pass the first and second element a[1] and a[2] are compared if they are out of order then the two elements are inter changed.
  • This process is repeated for array of elements a[2] and a[3], a[3] and a[4] until a[n-1] and a[n].
  • This process causes elements with smaller values to bubble up the array and hence the name bubble sort. Thus after the 1st pass the largest element will be at the nth position and on each of the successive pass the element with next largest value be placed in position n-1, n-2 ... so on. Where a is an array of n elements.

Searching

  • This is the process by which one searches the table of elements for the desired element.
  • There are different methods of searching but let us deal two popular methods of searching and they are linear search and binary search.
  • Linear Search:This is one of the simplest techniques for searching an unordered table for a particular element.
  • In this each and every entry in the table is checked in a sequential manner until the desired element is found.
  • Binary Search: In binary search the basic requirement is the elements of the array should have been sorted alphabetically or numerically in the ascending order.
  • In this technique the approximate middle entry of the array is located, and its key value is examined. If its value is too high, then the key value of the middle entry of the first half of the table is examined and the procedure is repeated on the first half until the required element is found or the search interval becomes empty. If the value is too low then the key of the middle entry of the second half of the array is tried and the procedure is repeated on the second half. The procedure continues until the desired key is found or the search interval becomes empty.

Strings

Constructed from literals, char arrays
Ex:
String[] nameOfStudents=new String[25];

String nameOfStudnets[]=new String[25];


Control Flow Statements in Java<< Previous

Next>>Java Classes and Methods

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus










Footer1