1: static void Main(string[] args)
2: { 3: int[] array = { 9,8,2,6,5,4,3,7,1}; 4: BubleSort(array);
5: for (int i = 0; i < array.Length; i++)
6: { 7: Console.WriteLine(array[i]);
8: }
9: }
10: static void BubleSort(int[] array)
11: { 12: for (int i = 0; i < array.Length; i++)
13: { 14: for (int j = 0; j < array.Length-i-1; j++)
15: { 16: if (array[j] > array[j + 1])
17: { 18: int temp = array[j];
19: array[j] = array[j + 1];
20: array[j + 1] = temp;
21: }
22: }
23: }
24: }
25:
26: // Interface Implement 1
27:
28: public interface IArray
29: { 30: bool Compare(IArray IA);
31: }
32: public class Employee:IArray
33: { 34: public Employee(int age)
35: { 36: this.Age = age;
37: }
38: public int Age { get; set; } 39: public bool Compare(IArray IA)
40: { 41: return this.Age>((Employee)IA).Age;
42: }
43: }
44: class Program
45: { 46: static void Main(string[] args)
47: { 48: Employee[] array = { new Employee(23),new Employee(20),new Employee(19),new Employee(30)}; 49: BubleSort(array);
50: for (int i = 0; i < array.Length; i++)
51: { 52: Console.WriteLine(array[i].Age);
53: }
54: }
55: static void BubleSort(IArray[] array)
56: { 57: for (int i = 0; i < array.Length; i++)
58: { 59: for (int j = 0; j < array.Length-i-1; j++)
60: { 61: if (array[j].Compare(array[j + 1]))
62: { 63: IArray temp = array[j];
64: array[j] = array[j + 1];
65: array[j + 1] = temp;
66: }
67: }
68: }
69: }
70: }
71:
72: //Interface implement2
73:
74: class Program
75: { 76: static void Main(string[] args)
77: { 78: //Employee[] array = { new Employee(23),new Employee(20),new Employee(19),new Employee(30)}; 79: IComparable[] array = { 9, 8, 2, 6, 5, 4, 3, 7, 1 }; 80: BubleSort(array);
81: for (int i = 0; i < array.Length; i++)
82: { 83: Console.WriteLine(array[i]);
84: }
85: }
86: static void BubleSort(IComparable[] array)
87: { 88: for (int i = 0; i < array.Length; i++)
89: { 90: for (int j = 0; j < array.Length-i-1; j++)
91: { 92: if (array[j].CompareTo(array[j + 1])>0)
93: { 94: IComparable temp = array[j];
95: array[j] = array[j + 1];
96: array[j + 1] = temp;
97: }
98: }
99: }
100: }
101: }
102:
103:
104: //Interface Implement 3 --- Inversion of control
105: class IntCompare:IComparer<int>
106: { 107: public int Compare(int x, int y)
108: { 109: return x.CompareTo(y);
110: }
111:
112: }
113: class Program
114: { 115: static void Main(string[] args)
116: { 117: //Employee[] array = { new Employee(23),new Employee(20),new Employee(19),new Employee(30)}; 118: int[] array = { 9, 8, 2, 6, 5, 4, 3, 7, 1 }; 119: BubleSort<int>(array,new IntCompare());
120: for (int i = 0; i < array.Length; i++)
121: { 122: Console.WriteLine(array[i]);
123: }
124: }
125:
126: static void BubleSort<T>(T[] array,IComparer<T> compare)
127: { 128: for (int i = 0; i < array.Length; i++)
129: { 130: for (int j = 0; j < array.Length-i-1; j++)
131: { 132: if (compare.Compare(array[j], array[j + 1]) > 0)
133: { 134: T temp = array[j];
135: array[j] = array[j + 1];
136: array[j + 1] = temp;
137: }
138: }
139: }
140: }
141: }
142:
143: //Delegate implementation
144: class Program
145: { 146: static void Main(string[] args)
147: { 148: //Employee[] array = { new Employee(23),new Employee(20),new Employee(19),new Employee(30)}; 149: int[] array = { 9, 8, 2, 6, 5, 4, 3, 7, 1 }; 150: BubleSort<int>(array, CompareMethod);
151: for (int i = 0; i < array.Length; i++)
152: { 153: Console.WriteLine(array[i]);
154: }
155: }
156: static bool CompareMethod(int t1, int t2)
157: { 158: return t1 > t2;
159: }
160: delegate bool Compare<T>(T t1, T t2);
161: static void BubleSort<T>(T[] array, Compare<T> compare)
162: { 163: for (int i = 0; i < array.Length; i++)
164: { 165: for (int j = 0; j < array.Length-i-1; j++)
166: { 167: if (compare(array[j], array[j + 1]))
168: { 169: T temp = array[j];
170: array[j] = array[j + 1];
171: array[j + 1] = temp;
172: }
173: }
174: }
175: }
176: }
177:
178: //Lamda function implementation
179: class Program
180: { 181: static void Main(string[] args)
182: { 183: //Employee[] array = { new Employee(23),new Employee(20),new Employee(19),new Employee(30)}; 184: int[] array = { 9, 8, 2, 6, 5, 4, 3, 7, 1 }; 185: BubleSort<int>(array, (a,b)=>a>b);
186: for (int i = 0; i < array.Length; i++)
187: { 188: Console.WriteLine(array[i]);
189: }
190: }
191: static void BubleSort<T>(T[] array, Func<T,T,bool> compare)
192: { 193: for (int i = 0; i < array.Length; i++)
194: { 195: for (int j = 0; j < array.Length-i-1; j++)
196: { 197: if (compare(array[j], array[j + 1]))
198: { 199: T temp = array[j];
200: array[j] = array[j + 1];
201: array[j + 1] = temp;
202: }
203: }
204: }
205: }
206: }