Advertisement
Guest User

GWArraySearch

a guest
Jul 17th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace GameWork.util {
  5.  
  6.     /**
  7.      * @Author Alex
  8.      */
  9.  
  10.     public class GWArraySearch {
  11.         //vars
  12.  
  13.         //constructor
  14.         public GWArraySearch() {
  15.  
  16.         }
  17.  
  18.         //public
  19.  
  20.         //can be used for anything, but is VERY slow
  21.         public int linearSearch<T>(List<T> list, T search) {
  22.             return linearSearch<T>(list.ToArray(), search);
  23.         }
  24.         public int linearSearch<T>(T[] arr, T search) {
  25.             for (int i = 0; i < arr.Length; i++) {
  26.                 if (arr[i].Equals(search)) {
  27.                     return i;
  28.                 }
  29.             }
  30.  
  31.             return -1;
  32.         }
  33.  
  34.         //ONLY use with sorted arrays. Very fast search
  35.         public int binarySearch<T>(List<T> list, T search) where T : IComparable {
  36.             return binarySearch<T>(list.ToArray(), search);
  37.         }
  38.         public int binarySearch<T>(T[] arr, T search) where T : IComparable {
  39.             int min = 0;
  40.             int N = arr.Length;
  41.             int max = N - 1;
  42.  
  43.             do {
  44.                 int mid = (min + max) / 2;
  45.  
  46.                 if (arr[mid].CompareTo(search) < 0) {
  47.                     min = mid + 1;
  48.                 } else {
  49.                     max = mid - 1;
  50.                 }
  51.  
  52.                 if (arr[mid].Equals(search)) {
  53.                     return mid;
  54.                 }
  55.             } while (min <= max);
  56.  
  57.             return -1;
  58.         }
  59.  
  60.         //ONLY use with sorted arrays. Fastest search, but experimental and only works with items that can be converted to doubles
  61.         public int interpolationSearch<T>(List<T> list, T search) where T : IComparable, IConvertible {
  62.             return interpolationSearch<T>(list.ToArray(), search);
  63.         }
  64.         public int interpolationSearch<T>(T[] arr, T search) where T : IComparable, IConvertible {
  65.             int low = 0;
  66.             int high = arr.Length - 1;
  67.             int mid;
  68.  
  69.             while (arr[low].CompareTo(search) < 0 && arr[high].CompareTo(search) >= 0) {
  70.                 mid = low + (int) (((Convert.ToDouble(search) - Convert.ToDouble(arr[low])) * (high - low)) / (Convert.ToDouble(arr[high]) - Convert.ToDouble(arr[low])));
  71.  
  72.                 if (arr[mid].CompareTo(search) < 0) {
  73.                     low = mid + 1;
  74.                 } else if (arr[mid].CompareTo(search) > 0) {
  75.                     high = mid - 1;
  76.                 } else {
  77.                     return mid;
  78.                 }
  79.             }
  80.  
  81.             if (arr[low].Equals(search)) {
  82.                 return low;
  83.             }
  84.  
  85.             return -1;
  86.         }
  87.  
  88.         //private
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement