Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SEARCHINGS

This is the concept of searchings for an item in the list of data stored. The application of search occurs when a particular data is required from a set of stored data. Searchings techniques can be classified into two types. Linear search (or) sequential search Binary search. 1) Linear search: - (sequential search) Suppose "A" is a linear array with "n" elements. Search for a given element "x",in an array A is to compare x with the element of A one by one . That is first we test whether A[0]=x and then test whether A[1]=x and so on. This method which transverses array A sequentially to allocate x is called Linear search. Program: import java.util.*; class linear { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a[]=new int[5]; int i; System.out.println("no of elements"); int n=sc.nextInt(); System.out.println("Enter elements"); for(i=0;i<n;i++) { a[i]=sc.nextInt(); } System.out.println("elements are"); for(i=0;i<n;i++) { System.out.println(a[i]); } System.out.println("Enter search element"); int x=sc.nextInt(); for( i=0;i<n;i++) { if(a[i]==x) { break; } } if(i==n) { System.out.println(x+"not found"); }

else { System.out.println(x+"is elemnt in "+(i+1)); } } }

Linear Search Complexity in Java Linear Search iterate over each element until it found the right element hence it has a variable best case and a fixed average and worst case complexity of 'O(n)'.
2) Binary search:|---------|----------| L M U First given array will be sorted. A binary search algorithm search for an element by trying to isolate smaller partitions in which the element can be found. In this method the search element is compared with the middle element of the table. If they are equal the search end successfully. Otherwise either the upper (or) lower half of the table must be searched in a similar memory. Given in the array a with n elements (Array should be in ascending order).The appropriate middle and key middle of the array is located and of a[mid] is compared with the elements to be search. Case 1:-If a[mid]>key(key=search element) The search interval for next to pass will be first half of the array that is elements a[0] to a[mid-1]. Case 2:-If a[mid]<key The search interval for next to pass will be second half of the array that is elements from a[mid+1] to a [n-1]. Case 3:-A[mid]=key the search is successful process is stopped. The case 1, case 2 process continues and till the desired element is found are search element interval can be empty. Here l lower bound and u upper bound. Program: import java.util.*; class BinarySearch { public static void main(String args[]) { int c, first, last, middle, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt();

array = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) { array[c] = in.nextInt(); } System.out.println("Enter value to find"); search = in.nextInt(); first = 0; last = n - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < search ) { first = middle + 1; } else if ( array[middle] == search ) { System.out.println(search + " found at location " + (middle + 1) + "."); break; } Else { last = middle - 1; } middle = (first + last)/2; } if ( first > last ) System.out.println(search + " is not present in the list.\n"); } }

This is an O(log n)

SORTINGS
"SORT" is the concept of arranging data in ascending (or) descending order with character (or) numeric data. Sorting techniques can be classified into two types namely Internal sorting External sorting In internal sorting method, the entire sorting can be carried out using internal memory only. But in external sorting methods the sorting can be carried out using external memory. Internal Sorting Methods 1. 2. 3. 4. 5. 6. 7. 8. Bubble Sorting or Exchange Sorting Selection Sorting Insertion Sort Quick Sort Merge Sort Radix Sort Shell Sort Heap Sort (Tree Sort)

BUBBLE SORT: In this technique it compares two consecutive elements in the list. If they are not in order the two elements will be interchanged. Otherwise they are in order, the two elements will not be interchanged .This process will continue (n-1) times of an array size of 'n'. Example:3 7 9 4 1 1) 3 7 4 1 9 2) 3 4 1 7 9 Four Sortings 3) 3 1 4 7 9 4) 1 3 4 7 9
import java.util.Scanner; class BubbleSort { public static void main(String []args) { int n, c, d, swap; Scanner in = new Scanner(System.in); System.out.println("Input number of integers to sort"); n = in.nextInt(); int array[] = new int[n]; System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); for (c = 0; c < ( n - 1 ); c++) { for (d = 0; d < n - c - 1; d++) {

if (array[d] { swap array[d] array[d+1] } } }

> array[d+1]) /* For descending order use < */ = array[d]; = array[d+1]; = swap;

System.out.println("Sorted list of numbers"); for (c = 0; c < n; c++) System.out.println(array[c]); } }

Complexity of bubble sort is O(n2) which makes it a less frequent option for arranging in sorted order when quantity of numbers is high.
import java.util.Arrays; class Sort { public static void main(String args[]) { int data[] = { 4, -5, 2, 6, 1 }; Arrays.sort(data); for (int c: data) { System.out.println(c); } } }

SELECTION SORT: In this method first find the smallest element in the list and put in the first position and then find the second smallest in the list and put it in the second position and so on. This process will continue (n-1) times of an array size 'n'. Example: 3 7 9 1 4 1. 1 7 9 3 4 2. 1 3 9 7 4 Four Sortings 3. 1 3 4 7 9 4. 1 3 4 7 9
import java.util.*; class sel

{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); int array[]=new int[100]; int n, c, d, position, swap; System.out.println("enter no of elements"); n=sc.nextInt(); System.out.println("Enter values"); for(c=0;c<n;c++) { array[c]=sc.nextInt(); } System.out.println("before elements are"); for(c=0;c<n;c++) { System.out.println(array[c]); } for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } System.out.println("after elements are"); for(c=0;c<n;c++) { System.out.println(array[c]); } } }

Each

such

instruction

is

executed

(N-1)

(N-2)

...

times

Time complexity: O(N2)


rough guide when picking between sorts of the same big-O efficiency.

Time
Sort Bubble sort Average O(n^2) Best O(n^2) O(n) O(n^2) O(n) Worst O(n^2) O(n^2) O(n^2) O(n^2) Space Stability Remarks Always use a modified bubble sort Stops after reaching a sorted array Even a perfectly sorted input requires scanning the entire array In the best case (already sorted), every insert requires constant time Constant Stable Constant Stable Constant Stable Constant Stable Modified Bubble O(n^2) sort Selection Sort Insertion Sort O(n^2) O(n^2)

Heap Sort Merge Sort Quicksort

O(n*log(n)) O(n*log(n)) O(n*log(n)) Constant Instable By using input array as storage for the heap, it is possible to achieve constant space O(n*log(n)) O(n*log(n)) O(n*log(n)) Depends Stable O(n*log(n)) O(n*log(n)) O(n^2) Constant Stable On arrays, merge sort requires O(n) space; on linked lists, merge sort requires constant space Randomly picking a pivot value (or shuffling the array prior to sorting) can help avoid worst case scenarios such as a perfectly sorted array.

You might also like