SlideShare a Scribd company logo
1 of 16
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 (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &n); printf (&quot; Enter the elements : &quot;); for( i = 0; i < n; i++ )  { printf (“arr [%d ]  : “ , i ); scanf( &quot;%d&quot;, &arr[ i ] ); } printf(“Enter 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 : %d”, num , pos);  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.
Binary_Search (A [ ], U_bound, VAL) Step 1 : set BEG = 0 , END = U_bound , POS = -1 Step 2 : Repeat while (BEG <= END ) Step 3 :  set MID = ( BEG + END ) / 2 Step 4 :  if  A [ MID ] == VAL then  POS = MID  print  VAL “ is available at “, POS GoTo Step 6 End if if A [ MID ] > VAL then set END = MID – 1 Else set BEG = MID + 1 End if End while Step 5 :  if POS = -1 then print  VAL “ is not present “ End if Step 6 : EXIT  void binary_serch ( int a [], int n, int val ) { int end = n - 1, beg = 0, pos = -1; while( beg <= end )  { mid = ( beg + end ) / 2; if ( val == a [ mid ] ) { pos = mid; printf(“%d is available at %d”,val, pos ); break; } if ( a [ mid ] > val ) end = mid – 1; else  beg = mid + 1; } if ( pos = - 1)  printf( “%d does not exist “, val ); }  Binary Searching ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Sorting is a technique to rearrange the elements of a list in ascending or descending 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 in the Mathematics subject. iii) If marks in Mathematics subject are  also same, then the date of births will be compared.  Sorting ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Internal Sorting : If all the data that is to be sorted can be accommodated at a time in memory is called internal sorting. External Sorting :  It is applied to Huge amount of data that cannot be accommodated in memory all at a time. So data in disk or file is loaded into memory part by part. Each part that is loaded is sorted separately, and stored in an intermediate file and all parts are merged into one single sorted list.
Bubble Sort  Unsorted   Sorted   Bubbles up the highest   After Pass 1 After  Pass 2 After Pass 3 After Pass 4 After Pass 5 Bubble_Sort ( A [ ] , N ) Step 1 : Repeat  For  P  = 1  to N – 1 Begin Step 2 :  Repeat  For J = 1 to  N – P Begin Step 3 :  If ( A [ J ]  < A [ J – 1 ] ) Swap ( A [ J ] , A [ J – 1 ] ) End  For End For Step 4 : Exit   Complexity of Bubble_Sort The complexity of sorting algorithm is depends upon the number of comparisons that are made. Total comparisons in Bubble sort is n ( n – 1) / 2  ≈  n  2  – n  Complexity = O ( n  2  ) Original List 10 47 12 54 19 23 54 10 47 12 23 19 54 47 10 23 12 19 54 47 23 10 19 12 54 47 23 19 10 12 54 47 23 19 12 10
void print_array (int a[ ], int n) { int i; for (i=0;I < n ; i++) printf(&quot;%5d&quot;,a[ i ]); } void bubble_sort ( int arr [ ], int n) { int pass, current, temp; for ( pass=1;(pass < n) ;pass++)  { for ( current=1;current <= n – pass ; current++)  { if ( arr[ current - 1 ] > arr[ current ] )  {   temp = arr[ current - 1 ];   arr[ current - 1 ] = arr[ current ];   arr[ current ] = temp; }  } } } int main()  { int count,num[50],i ; printf (&quot;How many elements to be sorted : &quot;); scanf (&quot;%d&quot;, &count); printf(&quot; Enter the elements : &quot;); for ( i = 0; i < count; i++) { printf (&quot;num [%d]  : &quot;, i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf(&quot; Array Before Sorting : &quot;); print_array ( num, count ); bubble_sort ( num, count); printf(&quot; Array  After Sorting : &quot;); print_array ( num, count ); } Bubble Sort For pass = 1 to N - 1 For J = 1 to N - pass A [ J – 1 ] > A [ J ] Temp = A [ J – 1 ] A [ J – 1 ] = A [ J ] A [ J ] = Temp T F Return
Insertion Sort  TEMP Insertion_Sort ( A [ ] , N ) Step 1 : Repeat  For  K  = 1  to N – 1 Begin Step 2 :  Set Temp  =  A [ K ] Step 3 :  Set  J  =  K – 1  Step 4 :  Repeat while Temp < A [ J ] AND J >= 0 Begin Set  A [ J + 1 ]  =  A [ J ] Set  J  =  J  - 1  End  While Step 5 :  Set  A [ J + 1 ]  = Temp End For Step 4 : Exit   insertion_sort ( int A[ ] , int n ) { int k , j , temp ; for ( k = 1 ; k < n ; k++ )  { temp = A [ k ] ; j  =  k  -  1; while ( ( temp < A [ j ] ) && ( j >= 0 ) ) { A [ j + 1 ]  = A [ j ] ; j - - ; } A [ j + 1 ]  =  temp ; } } Complexity of Insertion Sort Best Case : O ( n ) Average Case : O ( n 2  ) Worst Case : O ( n 2  )  78 23 45 8 32 36 23 78 45 8 32 36 23 23 45 78 8 32 36 8 23 45 78 32 36 8 23 32 45 78 36 45 8 8 23 32 36 45 78 32 36
Selection Sort ( Select the smallest and Exchange ) Smallest Selection_Sort ( A [ ] , N ) Step 1 : Repeat  For  K  =  0  to N –  2 Begin Step 2 :  Set  POS = K  Step 3 :  Repeat for J = K + 1 to N –  1  Begin If A[ J ] < A [ POS ]  Set  POS  =  J  End  For Step 5 :  Swap  A [ K ]  with A [ POS ] End For Step 6 : Exit   selection_sort ( int A[ ] , int n ) { int k , j , pos , temp ; for ( k = 0 ; k < n - 1 ; k++ ) { pos =  k ;  for ( j = k + 1 ; j <= n ; j ++ ) { if ( A [ j ] < A [ pos ] ) pos = j ; } temp = A [ k ] ; A [ k ] = A [ pos ] ; A [ pos ] = temp ; } } Complexity of Selection Sort Best Case : O ( n 2  ) Average Case : O ( n 2  ) Worst Case : O ( n 2  )  23 78 45 8 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 8 23 32 45 56
Insertion sort k = 1; k < n ; k++ temp = a [ k ]  j = k - 1 temp < a [ j ] && j >= 0 a [ j + 1 ] = a [ j ]  j = j - 1 a [ j + 1 ] = temp return Selection sort k = 0; k < n - 1 ; k++ pos = k j = k + 1 ; j < n ; j++ temp = a[ k ] a [ k ] = a [ pos ] a [ pos ] = temp return a[ j ] < a[ pos ] pos = j
Bubble sort – Insertion sort – Selection sort Bubble 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 receives new 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.
Quick Sort – A recursive process of sorting Algorithm for Quick_Sort : -- set the element A [ start_index ]  as pivot. -- rearrange the array so that :  -- all elements which are less than the pivot  come left ( before ) to the pivot. -- all elements which are greater than the pivot  come right ( after ) to the pivot. -- recursively apply quick-sort on the sub-list of  lesser elements. -- recursively apply quick-sort   on the sub-list of  greater elements. -- the base case of the recursion is lists of size  zero or one, which are always sorted. Original-list of 11 elements :  Set list [ 0 ] as pivot :  pivot   pivot   Rearrange ( partition ) the elements  into two sub lists : Sub-list of  lesser elements Sub-list of  greater elements Apply Quick-sort recursively on sub-list Apply Quick-sort recursively on sub-list Complexity of Quick Sort Best Case : O ( n log n ) Average Case : O ( n log n ) Worst Case : O ( n 2  )  8 3 2 11 5 14 0 2 9 4 20 8 3 2 11 5 14 0 2 9 4 20 4 3 2 2 5 0 8 11 9 14 20
9 12 8 16 1 25 10 3 9 12 8 16 1 25 10 3 3 12 8 16 1 25 10 3 8 16 1 25 10 12 3 1 8 16 25 10 12 3 1 8 16 25 10 12 Pivot Partitioning for ‘ One Step of Quick Sort ’
Quick Sort – Program  void quick_sort(int a[ ] , int beg , int end ) { int loc; if ( beg < end ) { loc = partition( a , beg , end ); quick_sort ( a , beg , loc – 1 ); quick_sort ( a , loc + 1 , end ); } } void print_array (int a [ ],int n)  { int i; for ( i = 0 ; I < n ; i++ ) printf( &quot;%5d“ ,a [ i ] ) ; } int main () { int count , num[ 50 ] , i ; printf (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &count ); printf (&quot; Enter the elements : &quot;); for( i = 0; i < count; i++ )  { printf (&quot;num [%d ]  : “ , i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf (“  Array Before Sorting : “ ); print_array ( num , count ) ; quick_sort ( num ,0 , count-1) ; printf ( &quot; Array  After Sorting : “ ); print_array ( num , count ); } int partition ( int a [ ], int beg, int end ) { int left , right , loc , flag = 0, pivot ; loc = left = beg;  right = end;  pivot = a [ loc ] ;  while ( flag == 0 )  { while( (pivot <= a [ right ] )&&( loc != right ) ) right - - ; if( loc == right )  flag = 1; else {  a  [ loc ] = a [ right ] ; left = loc + 1 ;  loc = right;  }  while ( (pivot  >= a [ left ] ) && ( loc != left ) ) left++; if( loc == left ) flag = 1; else {  a [ loc ] = a  [ left ] ; right = loc - 1;  loc = left;  }  } a [ loc ] = pivot;  return loc;  }
partition ( int a [ ], int beg, int end ) loc = left = beg  flag = 0, right = end  pivot = a [ loc ] Flag == 0 pivot <= a [ right ]  && loc != right right = right  - 1  loc == right a  [ loc ] = a [ right ]  left = loc + 1 ;  loc = right; flag = 1 F T B A B A pivot  >= a [ left ] &&loc != left   left = left  + 1  loc == left a  [ loc ] = a [ left ]  right = loc - 1 ;  loc = left; flag = 1 F T a[ loc ] = pivot  return loc quick_sort ( int a [ ], int beg, int end ) loc == left T loc = partition( a , beg , end ) quick_sort ( a , beg , end ) quick_sort ( a , beg , end ) F return
Merge Sort ( Divide and conquer ) Divide the array Merge the elements to sorted array --  Merge sort technique sorts a given set  of values by combining two sorted  arrays into one larger sorted arrays. --  A small list will take fewer steps to sort  than a large list. --  Fewer steps are required to construct  a sorted list from two sorted lists than  two unsorted lists.  --  You only have to traverse each list  once if they're already sorted . ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Time complexity Worst case - O ( n  log  n )  Best case  - O ( n log n ) typical, O ( n ) natural variant  Average case - O (   n  log  n  ) 39 9 81 45 90 27 72 18 39 9 81 45 90 27 72 18 39 9 81 45 90 27 72 18 39 9 81 45 90 27 72 18 39 9 81 45 90 27 72 18 9 39 45 81 27 90 18 72 9 39 45 81 18 27 72 90 9 18 27 39 45 72 81 90
void merge(int a[ ],int low,int high,int mid){   int i, j, k, c[50];   i=low; j=mid+1; k=low;   while( ( i<=mid )&&( j <= high ) ) {   if( a[ i ]<a[ j ] ){   c[ k ]=a[ i ];  k++;  i++;   }else {   c[ k ]=a[ j ]; k++; j++;   } }   while( i<=mid ) { c[k]=a[ i ]; k++; i++; }  while(j<=high)  { c[k]=a[ j ]; k++; j++; } for(i=low;i<k;i++) a[ i ]=c[ i ]; } void merge_sort(int a[ ], int low, int high){   int mid;   if( low < high) {   mid=(low+high)/2;   merge_sort (a, low, mid);   merge_sort (a, mid+1 ,high);   merge (a, low, high, mid);   } } Merge Sort -  Program void print_array (int a [ ],int n)  { int i; for ( i = 0 ; I < n ; i++ ) printf( &quot;%5d“ ,a [ i ] ) ; } int main () { int count , num[ 50 ] , i ; printf (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &count ); printf (&quot; Enter the elements : &quot;); for( i = 0; i < count; i++ )  { printf (&quot;num [%d ]  : “ , i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf (“  Array Before Sorting : “ ); print_array ( num , count ) ; merge_sort ( num ,0 , count-1) ; printf ( &quot; Array  After Sorting : “ ); print_array ( num , count ); }
Merge_Sort low < high mid = ( low + high ) / 2  merge_sort (a, low, mid) merge_sort (a, mid, high ) Merge (a, low,high , mid) Return T F merge i =low ; j = mid+1;k = low i <= mid && j <= high a[ i ] < a[ j ] c[ k ] =a [ i ] ; k++ ; i++ c[ k ] =a [ j ] ; k++ ; j++ i <= mid c[ k ] =a [ i ] ; k++ ; i++ j <= high c[ k ] =a [ j ] ; k++ ; j++ i = low ; i < k ; i ++ a[ i ] = c [ i ] return F T

More Related Content

What's hot (20)

Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Bubble sort
Bubble sortBubble sort
Bubble sort
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Merge sort
Merge sortMerge sort
Merge sort
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
 
Quick sort
Quick sortQuick sort
Quick sort
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Circular queue
Circular queueCircular queue
Circular queue
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Quick sort
Quick sortQuick sort
Quick sort
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Expression trees
Expression treesExpression trees
Expression trees
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Selection sort
Selection sortSelection sort
Selection sort
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Trees
TreesTrees
Trees
 
Shell sort
Shell sortShell sort
Shell sort
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 

Similar to Unit6 C

Similar to Unit6 C (20)

searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
C Language Unit-6
C Language Unit-6C Language Unit-6
C Language Unit-6
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Lecture 1 sorting insertion &amp; shell sort
Lecture 1 sorting insertion &amp; shell sortLecture 1 sorting insertion &amp; shell sort
Lecture 1 sorting insertion &amp; shell sort
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting
SortingSorting
Sorting
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
 
Insersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in AlgoritmInsersion & Bubble Sort in Algoritm
Insersion & Bubble Sort in Algoritm
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Sorting
SortingSorting
Sorting
 
Sorting
SortingSorting
Sorting
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 

More from arnold 7490 (20)

Les14
Les14Les14
Les14
 
Les13
Les13Les13
Les13
 
Les11
Les11Les11
Les11
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
Les07
Les07Les07
Les07
 
Les06
Les06Les06
Les06
 
Les05
Les05Les05
Les05
 
Les04
Les04Les04
Les04
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 

Recently uploaded

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 

Unit6 C

  • 1. 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 (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &n); printf (&quot; Enter the elements : &quot;); for( i = 0; i < n; i++ ) { printf (“arr [%d ] : “ , i ); scanf( &quot;%d&quot;, &arr[ i ] ); } printf(“Enter 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 : %d”, num , pos); 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.
  • 2.
  • 3.
  • 4. Bubble Sort Unsorted Sorted Bubbles up the highest After Pass 1 After Pass 2 After Pass 3 After Pass 4 After Pass 5 Bubble_Sort ( A [ ] , N ) Step 1 : Repeat For P = 1 to N – 1 Begin Step 2 : Repeat For J = 1 to N – P Begin Step 3 : If ( A [ J ] < A [ J – 1 ] ) Swap ( A [ J ] , A [ J – 1 ] ) End For End For Step 4 : Exit Complexity of Bubble_Sort The complexity of sorting algorithm is depends upon the number of comparisons that are made. Total comparisons in Bubble sort is n ( n – 1) / 2 ≈ n 2 – n Complexity = O ( n 2 ) Original List 10 47 12 54 19 23 54 10 47 12 23 19 54 47 10 23 12 19 54 47 23 10 19 12 54 47 23 19 10 12 54 47 23 19 12 10
  • 5. void print_array (int a[ ], int n) { int i; for (i=0;I < n ; i++) printf(&quot;%5d&quot;,a[ i ]); } void bubble_sort ( int arr [ ], int n) { int pass, current, temp; for ( pass=1;(pass < n) ;pass++) { for ( current=1;current <= n – pass ; current++) { if ( arr[ current - 1 ] > arr[ current ] ) { temp = arr[ current - 1 ]; arr[ current - 1 ] = arr[ current ]; arr[ current ] = temp; } } } } int main() { int count,num[50],i ; printf (&quot;How many elements to be sorted : &quot;); scanf (&quot;%d&quot;, &count); printf(&quot; Enter the elements : &quot;); for ( i = 0; i < count; i++) { printf (&quot;num [%d] : &quot;, i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf(&quot; Array Before Sorting : &quot;); print_array ( num, count ); bubble_sort ( num, count); printf(&quot; Array After Sorting : &quot;); print_array ( num, count ); } Bubble Sort For pass = 1 to N - 1 For J = 1 to N - pass A [ J – 1 ] > A [ J ] Temp = A [ J – 1 ] A [ J – 1 ] = A [ J ] A [ J ] = Temp T F Return
  • 6. Insertion Sort TEMP Insertion_Sort ( A [ ] , N ) Step 1 : Repeat For K = 1 to N – 1 Begin Step 2 : Set Temp = A [ K ] Step 3 : Set J = K – 1 Step 4 : Repeat while Temp < A [ J ] AND J >= 0 Begin Set A [ J + 1 ] = A [ J ] Set J = J - 1 End While Step 5 : Set A [ J + 1 ] = Temp End For Step 4 : Exit insertion_sort ( int A[ ] , int n ) { int k , j , temp ; for ( k = 1 ; k < n ; k++ ) { temp = A [ k ] ; j = k - 1; while ( ( temp < A [ j ] ) && ( j >= 0 ) ) { A [ j + 1 ] = A [ j ] ; j - - ; } A [ j + 1 ] = temp ; } } Complexity of Insertion Sort Best Case : O ( n ) Average Case : O ( n 2 ) Worst Case : O ( n 2 ) 78 23 45 8 32 36 23 78 45 8 32 36 23 23 45 78 8 32 36 8 23 45 78 32 36 8 23 32 45 78 36 45 8 8 23 32 36 45 78 32 36
  • 7. Selection Sort ( Select the smallest and Exchange ) Smallest Selection_Sort ( A [ ] , N ) Step 1 : Repeat For K = 0 to N – 2 Begin Step 2 : Set POS = K Step 3 : Repeat for J = K + 1 to N – 1 Begin If A[ J ] < A [ POS ] Set POS = J End For Step 5 : Swap A [ K ] with A [ POS ] End For Step 6 : Exit selection_sort ( int A[ ] , int n ) { int k , j , pos , temp ; for ( k = 0 ; k < n - 1 ; k++ ) { pos = k ; for ( j = k + 1 ; j <= n ; j ++ ) { if ( A [ j ] < A [ pos ] ) pos = j ; } temp = A [ k ] ; A [ k ] = A [ pos ] ; A [ pos ] = temp ; } } Complexity of Selection Sort Best Case : O ( n 2 ) Average Case : O ( n 2 ) Worst Case : O ( n 2 ) 23 78 45 8 32 56 8 78 45 23 32 56 8 23 45 78 32 56 8 23 32 78 45 56 8 23 32 45 78 56 8 23 32 45 56 78 8 23 32 45 56
  • 8. Insertion sort k = 1; k < n ; k++ temp = a [ k ] j = k - 1 temp < a [ j ] && j >= 0 a [ j + 1 ] = a [ j ] j = j - 1 a [ j + 1 ] = temp return Selection sort k = 0; k < n - 1 ; k++ pos = k j = k + 1 ; j < n ; j++ temp = a[ k ] a [ k ] = a [ pos ] a [ pos ] = temp return a[ j ] < a[ pos ] pos = j
  • 9. Bubble sort – Insertion sort – Selection sort Bubble 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 receives new 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 sorting Algorithm for Quick_Sort : -- set the element A [ start_index ] as pivot. -- rearrange the array so that : -- all elements which are less than the pivot come left ( before ) to the pivot. -- all elements which are greater than the pivot come right ( after ) to the pivot. -- recursively apply quick-sort on the sub-list of lesser elements. -- recursively apply quick-sort on the sub-list of greater elements. -- the base case of the recursion is lists of size zero or one, which are always sorted. Original-list of 11 elements : Set list [ 0 ] as pivot : pivot pivot Rearrange ( partition ) the elements into two sub lists : Sub-list of lesser elements Sub-list of greater elements Apply Quick-sort recursively on sub-list Apply Quick-sort recursively on sub-list Complexity of Quick Sort Best Case : O ( n log n ) Average Case : O ( n log n ) Worst Case : O ( n 2 ) 8 3 2 11 5 14 0 2 9 4 20 8 3 2 11 5 14 0 2 9 4 20 4 3 2 2 5 0 8 11 9 14 20
  • 11. 9 12 8 16 1 25 10 3 9 12 8 16 1 25 10 3 3 12 8 16 1 25 10 3 8 16 1 25 10 12 3 1 8 16 25 10 12 3 1 8 16 25 10 12 Pivot Partitioning for ‘ One Step of Quick Sort ’
  • 12. Quick Sort – Program void quick_sort(int a[ ] , int beg , int end ) { int loc; if ( beg < end ) { loc = partition( a , beg , end ); quick_sort ( a , beg , loc – 1 ); quick_sort ( a , loc + 1 , end ); } } void print_array (int a [ ],int n) { int i; for ( i = 0 ; I < n ; i++ ) printf( &quot;%5d“ ,a [ i ] ) ; } int main () { int count , num[ 50 ] , i ; printf (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &count ); printf (&quot; Enter the elements : &quot;); for( i = 0; i < count; i++ ) { printf (&quot;num [%d ] : “ , i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf (“ Array Before Sorting : “ ); print_array ( num , count ) ; quick_sort ( num ,0 , count-1) ; printf ( &quot; Array After Sorting : “ ); print_array ( num , count ); } int partition ( int a [ ], int beg, int end ) { int left , right , loc , flag = 0, pivot ; loc = left = beg; right = end; pivot = a [ loc ] ; while ( flag == 0 ) { while( (pivot <= a [ right ] )&&( loc != right ) ) right - - ; if( loc == right ) flag = 1; else { a [ loc ] = a [ right ] ; left = loc + 1 ; loc = right; } while ( (pivot >= a [ left ] ) && ( loc != left ) ) left++; if( loc == left ) flag = 1; else { a [ loc ] = a [ left ] ; right = loc - 1; loc = left; } } a [ loc ] = pivot; return loc; }
  • 13. partition ( int a [ ], int beg, int end ) loc = left = beg flag = 0, right = end pivot = a [ loc ] Flag == 0 pivot <= a [ right ] && loc != right right = right - 1 loc == right a [ loc ] = a [ right ] left = loc + 1 ; loc = right; flag = 1 F T B A B A pivot >= a [ left ] &&loc != left left = left + 1 loc == left a [ loc ] = a [ left ] right = loc - 1 ; loc = left; flag = 1 F T a[ loc ] = pivot return loc quick_sort ( int a [ ], int beg, int end ) loc == left T loc = partition( a , beg , end ) quick_sort ( a , beg , end ) quick_sort ( a , beg , end ) F return
  • 14.
  • 15. void merge(int a[ ],int low,int high,int mid){ int i, j, k, c[50]; i=low; j=mid+1; k=low; while( ( i<=mid )&&( j <= high ) ) { if( a[ i ]<a[ j ] ){ c[ k ]=a[ i ]; k++; i++; }else { c[ k ]=a[ j ]; k++; j++; } } while( i<=mid ) { c[k]=a[ i ]; k++; i++; } while(j<=high) { c[k]=a[ j ]; k++; j++; } for(i=low;i<k;i++) a[ i ]=c[ i ]; } void merge_sort(int a[ ], int low, int high){ int mid; if( low < high) { mid=(low+high)/2; merge_sort (a, low, mid); merge_sort (a, mid+1 ,high); merge (a, low, high, mid); } } Merge Sort - Program void print_array (int a [ ],int n) { int i; for ( i = 0 ; I < n ; i++ ) printf( &quot;%5d“ ,a [ i ] ) ; } int main () { int count , num[ 50 ] , i ; printf (&quot;How many elements to sort : &quot;); scanf (&quot;%d&quot;, &count ); printf (&quot; Enter the elements : &quot;); for( i = 0; i < count; i++ ) { printf (&quot;num [%d ] : “ , i ); scanf( &quot;%d&quot;, &num[ i ] ); } printf (“ Array Before Sorting : “ ); print_array ( num , count ) ; merge_sort ( num ,0 , count-1) ; printf ( &quot; Array After Sorting : “ ); print_array ( num , count ); }
  • 16. Merge_Sort low < high mid = ( low + high ) / 2 merge_sort (a, low, mid) merge_sort (a, mid, high ) Merge (a, low,high , mid) Return T F merge i =low ; j = mid+1;k = low i <= mid && j <= high a[ i ] < a[ j ] c[ k ] =a [ i ] ; k++ ; i++ c[ k ] =a [ j ] ; k++ ; j++ i <= mid c[ k ] =a [ i ] ; k++ ; i++ j <= high c[ k ] =a [ j ] ; k++ ; j++ i = low ; i < k ; i ++ a[ i ] = c [ i ] return F T

Editor's Notes

  1. ADITYA ENGINEERING COLLEGES