of 17 /17
Searching techniques Searching : It is a process to find whether a particular value with specified properties is present or not among a collection of items. If the value is present in the collection, then searching is said to be successful, and it returns the location of the value in the array. Otherwise, if the value is not present in the array, the searching process displays the appropriate message and in this case searching is said to be unsuccessful. 1) Linear or Sequential Searching 2) Binary Searching Linear_Search (A[ ], N, val , pos ) Step 1 : Set pos = -1 and k = 0 Step 2 : Repeat while k < N Begin Step 3 : if A[ k ] = val Set pos = k print pos Goto step 5 End while Step 4 : print “Value is not present” Step 5 : Exit int main( ) { int arr [ 50 ] , num , i , n , pos = -1; printf ("How many elements to sort : "); scanf ("%d", &n); printf ("\n Enter the elements : \n\n"); for( i = 0; i < n; i++ ) { printf (“arr [%d ] : “ , i ); scanf( "%d", &arr[ i ] ); } printf(“\nEnter the number to be searched : “); scanf(“%d”,&num); for(i=0;i<n;i++) if( arr [ i ] == num ) { pos = i ; break; } if ( pos == -1 ) printf(“ %d does not exist ”,num); else printf(“ %d is found at location : Searches -- for each item one by one in the list from the first, until the match is found. Efficiency of Linear search : -- Executes in O ( n ) times where n is the number of elements in the list.
• Author

mrecedu
• Category

## Technology

• view

129

2

TAGS:

Embed Size (px)

description

### Transcript of Unit6 jwfiles

• 1. Searching techniquesSearching : It is a process to find whether a particular value with specified properties is present or notamong a collection of items. If the value is present in the collection, then searching is said to be successful, and itreturns the location of the value in the array.Otherwise, if the value is not present in the array, the searching process displays theappropriate message and in this case searching is said to be unsuccessful.1) Linear or Sequential Searching2) Binary Searchingint main( ) { Linear_Search (A[ ], N, val , pos ) int arr [ 50 ] , num , i , n , pos = -1; Step 1 : Set pos = -1 and k = 0 printf ("How many elements to sort : "); Step 2 : Repeat while k < N scanf ("%d", &n); Begin printf ("n Enter the elements : nn"); Step 3 : if A[ k ] = val for( i = 0; i < n; i++ ) { Set pos = kprintf (arr [%d ] : , i ); print posscanf( "%d", &arr[ i ] ); Goto step 5 } End while printf(nEnter the number to be searched : );Step 4 : print Value is not present scanf(%d,&num);Step 5 : Exit for(i=0;i

2. Binary SearchingAlgorithm: Before searching, the list of items should be sorted in ascending order. We first compare the key value with the item in the position of the array. If there is a match, wecan return immediately the position. if the value is less than the element in middle location of the array, the required value is lie inthe lower half of the array. if the value is greater than the element in middle location of the array, the required value is liein the upper half of the array. We repeat the above procedure on the lower half or upper half of the array.Binary_Search (A [ ], U_bound, VAL)Step 1 : set BEG = 0 , END = U_bound , POS = -1Step 2 : Repeat while (BEG val ) end = mid 1; set BEG = MID + 1else beg = mid + 1; End if}End whileif ( pos = - 1)Step 5 : if POS = -1 thenprintf( %d does not exist , val );print VAL is not present }End ifStep 6 : EXIT 3. Sorting Sorting is a technique to rearrange the elements of a list in ascending ordescending order, which can be numerical, lexicographical, or any user-defined order.Ranking of students is the process of sorting in descending order.EMCET Ranking is an example for sorting with user-defined order.EMCET Ranking is done with the following priorities.i) First priority is marks obtained in EMCET. ii) If marks are same, the ranking will be done with comparing marks obtained inthe Mathematics subject. iii) If marks in Mathematics subject are also same, then the date of births will becompared.Internal Sorting : Types of Internal SortingsIf all the data that is to be sorted can be accommodatedat a time in memory is called internal sorting.Bubble SortExternal Sorting : Insertion Sort It is applied to Huge amount of data that cannot be Selection Sortaccommodated in memory all at a time. So data in diskor file is loaded into memory part by part. Each part that Quick Sortis loaded is sorted separately, and stored in anintermediate file and all parts are merged into one single Merge Sortsorted list. 4. Bubble Sort Bubbles up the highest Unsorted Sorted Bubble_Sort ( A [ ] , N ) Step 1 : Repeat For P = 1 to N 11054 54 5454 54 Begin Step 2 :Repeat For J = 1 to N P4710 47 4747 47Begin Step 3 :If ( A [ J ] < A [ J 1 ] )1247 10 2323 23Swap ( A [ J ] , A [ J 1 ] ) End For5412 23 1019 19 End For Step 4 : Exit1923 12 1910 12Complexity of Bubble_Sort2319 19 1212 10The complexity of sorting algorithm is depends upon the number of comparisonsOriginal AfterAfter AfterAfter After that are made.List Pass 1 Pass 2 Pass 3 Pass 4Pass 5 Total comparisons in Bubble sort is n ( n 1) / 2 n 2 n Complexity = O ( n 2 ) 5. void print_array (int a[ ], int n) {int i;for (i=0;I < n ; i++) printf("%5d",a[ i ]);Bubble Sort}void bubble_sort ( int arr [ ], int n) { int pass, current, temp;For pass = 1 to N - 1 for ( pass=1;(pass < n) ;pass++) {for ( current=1;current arr[ current ] ) {temp = arr[ current - 1 ];For J = 1 to N - passarr[ current - 1 ] = arr[ current ];arr[ current ] = temp; } TA[J1]>A[J]} }}Fint main() {Temp = A [ J 1 ] int count,num,i ;A[J1]=A[J] printf ("How many elements to be sorted : "); A [ J ] = Temp scanf ("%d", &count); printf("n Enter the elements : nn"); for ( i = 0; i < count; i++) {printf ("num [%d] : ", i ); scanf( "%d", &num[ i ] ); } printf("n Array Before Sorting : nnn"); print_array ( num, count ); Return bubble_sort ( num, count); printf("nnn Array After Sorting : nnn"); print_array ( num, count );} 6. TEMPInsertion Sort Insertion_Sort ( A [ ] , N )7823 45 832 36 Step 1 : Repeat For K = 1 to N 123Begin Step 2 :Set Temp = A [ K ] Step 3 :Set J = K 1 23 7845832 36 Step 4 :Repeat while Temp < A [ J ] AND J >= 045 Begin Set A [ J + 1 ] = A [ J ] Set J = J - 1234578832 36 End While Step 5 :Set A [ J + 1 ] = Temp 8End For Step 4 : Exit8 234578 32 3632 insertion_sort ( int A[ ] , int n ) { int k , j , temp ;8 233245 78 36 for ( k = 1 ; k < n ; k++ ) { temp = A [ k ] ;36 j = k - 1; while ( ( temp < A [ j ] ) && ( j >= 0 ) ) {8 233236 45 78A[j+1] =A[j];j--;Complexity of Insertion Sort }Best Case : O ( n )A [ j + 1 ] = temp ;Average Case : O ( n2 )}Worst Case : O ( n2 )} 7. SmallestSelection Sort ( Select the smallest and Exchange )Selection_Sort ( A [ ] , N )823 78 45 8 32 56 Step 1 : Repeat For K = 0 to N 2 BeginStep 2 : Set POS = KStep 3 : Repeat for J = K + 1 to N 1 23878452332 56Begin If A[ J ] < A [ POS ] Set POS = J 32823457832 56 End ForStep 5 :Swap A [ K ] with A [ POS ] End ForStep 6 : Exit 45823327845 56selection_sort ( int A[ ] , int n ) { int k , j , pos , temp ; for ( k = 0 ; k < n - 1 ; k++ ) { 56823324578 56 pos = k ;for ( j = k + 1 ; j = 0a[ j ] < a[ pos ]a[j+1]=a[j]pos = j j=j-1a [ j + 1 ] = temp temp = a[ k ] a [ k ] = a [ pos ] a [ pos ] = temp returnreturn 9. Bubble sort Insertion sort Selection sortBubble Sort :-- very primitive algorithm like linear search, and least efficient .-- No of swappings are more compare with other sorting techniques.-- It is not capable of minimizing the travel through the array like insertion sort.Insertion Sort :-- sorted by considering one item at a time.-- efficient to use on small sets of data.-- twice as fast as the bubble sort.-- 40% faster than the selection sort.-- no swapping is required.-- It is said to be online sorting because it continues the sorting a list as and when it receivesnew elements.-- it does not change the relative order of elements with equal keys.-- reduces unnecessary travel through the array.-- requires low and constant amount of extra memory space.-- less efficient for larger lists.Selection sort :-- No of swappings will be minimized. i.e., one swap on one pass.-- generally used for sorting files with large objects and small keys.-- It is 60% more efficient than bubble sort and 40% less efficient than insertion sort.-- It is preferred over bubble sort for jumbled array as it requires less items to be exchanged.-- uses internal sorting that requires more memory space.-- It cannot recognize sorted list and carryout the sorting from the beginning, when new elements are added to the list. 10. Quick Sort A recursive process of sortingOriginal-list of 11 elements :8 3 2 11 5 14 0 2 9 4 20 Algorithm for Quick_Sort :Set list [ 0 ] as pivot :-- set the element A [ start_index ] as pivot. -- rearrange the array so that : pivot-- all elements which are less than the pivot come left ( before ) to the pivot.8 3 2 11 5 14 0 2 9 4 20-- all elements which are greater than the pivot come right ( after ) to the pivot.Rearrange ( partition ) the elements -- recursively apply quick-sort on the sub-list ofinto two sub lists : lesser elements.pivot-- recursively apply quick-sort on the sub-list of greater elements.4 3 2 25 0 811 914 20-- the base case of the recursion is lists of size zero or one, which are always sorted. Sub-list of Sub-list oflesser elements greater elements Complexity of Quick SortBest Case : O ( n log n )Apply Quick-sortApply Quick-sortAverage Case : O ( n log n )recursively recursively Worst Case : O ( n2 )on sub-list on sub-list 11. Partitioning for One Step of Quick Sort Pivot 9 12 8 16 1 25 103 9 12 8 16 1 25 103 3 12 8 16 1 25 10 38 16 1 25 10 12 31 8 16 25 10 12 31 8 16 25 10 12 12. Quick Sort Programint partition ( int a [ ], int beg, int end ) {void quick_sort(int a[ ] , int beg , int end ) {int left , right , loc , flag = 0, pivot ;int loc;loc = left = beg; if ( beg < end ) {right = end;loc = partition( a , beg , end );pivot = a [ loc ] ; quick_sort ( a , beg , loc 1 );while ( flag == 0 ) quick_sort ( a , loc + 1 , end );{ } while( (pivot