SHARE
TWEET

Untitled

a guest Apr 18th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************************
  2. Author:         Dr. Daniel Spiegel, Updated by: Trisha Badlu
  3. Creation Date:  21 March 2017                            
  4. Due Date:       31 March 2017
  5. Assignment:     #3
  6. Filename:       SortSearch.h                                  
  7. Course:     CSC136 - 020                                    
  8. Professor Name: Dr. Spiegel    
  9. Purpose:        Creates a template class that helps to compute
  10.             results on any element type, as opposed to a
  11.             specific type.
  12. ***************************************************************/
  13. // FILE: SortSearch.h
  14. // A couple of things that are nice to have available
  15.  
  16. // SORTS AN ARRAY (ASCENDING ORDER) USING SELECTION SORT ALGORITHM
  17. //    USES exchange AND find_index_of_min
  18. /***************************************************************
  19. Function name: exchange (mutator)                                          
  20. Description: Swaps two elements          
  21. Parameters: eltType& - x (import/export)
  22.                y (import/export)
  23. Return Value: none      
  24. ***************************************************************/
  25.    // EXCHANGES TWO INTEGER VALUES
  26.    template <class eltType> void exchange(eltType &x,eltType &y)          
  27.    // Arguments:
  28.    // Both: INOUT:
  29.    {    eltType temp;
  30.         temp=y; y=x; x=temp;
  31.    }
  32. /***************************************************************
  33. Function name: selSort (facilitator)                                          
  34. Description: Sorts the elements in a list using selection sort          
  35. Parameters: eltType - *list (import/export)
  36.               items (import)
  37. Return Value: none      
  38. ***************************************************************/
  39. template <class eltType> void selSort(eltType *list,int items)
  40. // Arguments:
  41. // list: INOUT - array to be sorted;
  42. // items IN: number of items to be sorted (items >= 0)
  43.  
  44. // Sorts the data in array items (list[0] through list[items-1]).
  45. // Pre:  list is defined and items <= declared size of actual argument array.
  46. // Post: The values in list[0] through list[items-1] are in increasing order.
  47. {
  48.    // Local data ...
  49.    int idxMax;  // subscript of each smallest item located by find_index_of_min
  50.  
  51.    for (int spot = items-1; spot > 0; spot--)
  52.    {
  53.       // Invariant: The elements in list[spot+1] through list[items-1] are in their
  54.       //    proper place and spot > 0.
  55.  
  56.       // Find index of largest unsorted element
  57.       idxMax = spot;
  58.       for (int idx = 0 ; idx < spot ; idx++)
  59.         if (list[idx] > list[idxMax])
  60.             idxMax = idx;
  61.  
  62.       // Exchange items at position idxMax and spot if different
  63.       if (spot != idxMax)
  64.         exchange (list[idxMax], list[spot]);
  65.    }  // end for
  66.  
  67. }  // end sel_sort
  68.  
  69. /***************************************************************
  70. Function name: orderedSearch (facilitator)      
  71. Description: Searches for an item in the array          
  72. Parameters: eltType - *list (import/export)
  73.               items (import)
  74.               key (import)
  75. Return Value: int - index      
  76. ***************************************************************/
  77. // Templated Search function
  78. template <class eltType>
  79.         int orderedSearch(eltType *list,int items,eltType key)
  80. {
  81.     int index;
  82.     for (index=0;index<items && list[index]<key;index++);
  83.  if (index==items || list[index]>key) return(-1);
  84.  return(index);
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top