import java.util.Arrays; // This program demonstrates a few of the search and sort algorithms public class ArrayDemo { // Linear search public static int linearSearch( int[] a, int key ){ for( int i=0; i=0; j--) if( a[j] > a[k] ){ swap(a,j,k); k=j; } } // end of outer for-loop } public static void printArray(int[] a){ System.out.print("The array is: "); for(int n:a) System.out.print(n+" "); System.out.println(); } private static void swap( int[] a, int first, int second ){ int temp = a[first]; a[first] = a[second]; a[second] = temp; } public static void main( String args[] ){ int[] array = {2,3,1,8,5,1,13,11}; int key = 3; int pos; printArray(array); // Searching using linear search pos = linearSearch( array, key ); System.out.println("Linear search finds " + key + " at position " + pos); // Sorting using selection sort or insertion sort //insertionSort(array); selectionSort(array); System.out.println("Sorting the array..."); printArray(array); // Searching using binary search pos = binarySearch( array, key ); System.out.println("Binary search finds " + key + " at position " + pos); } }