Delegated Bubble Sorting Algorithm Based on Generics

Source: Internet
Author: User
1 Class Program
2 {
3 Static   Void Main ( String [] ARGs)
4 {
5 Employee [] employees =
6 {
7 New Employee ( " Bugs Bunny " , 20000 ),
8 New Employee ( " Elmer Fudd " , 10000 ),
9 New Employee ( " Daffy Duck " , 25000 ),
10 New Employee ( " Wile Coyote " , 1000000.38 m ),
11 New Employee ( " Foghorn Leghorn " , 23000 ),
12 New Employee ( " Roadrunner " , 50000 )
13 };
14 Bubblesorter. Sort (employees, employee. comparesalary );
15 Foreach (VAR employee In Employees)
16 {
17 Console. writeline (employee );
18 }
19 Console. Readline ();
20 }
21 }
22
23 Class Bubblesorter
24 {
25 Static   Public   Void Sort < T > (Ilist < T > Sortarray, func < T, T, Bool > Comparison)
26 {
27 Bool Swapped =   True ;
28 Do
29 {
30 Swapped =   False ;
31 For ( Int I =   0 ; I < Sortarray. Count -   1 ; I ++ )
32 {
33 If (Comparison (sortarray [I +   1 ], Sortarray [I])
34 {
35 T temp = Sortarray [I];
36 Sortarray [I] = Sortarray [I +   1 ];
37 Sortarray [I +   1 ] = Temp;
38 Swapped =   True ;
39 }
40 }
41 } While (Swapped );
42 }
43 }
44
45 Class Employee
46 {
47 Public Employee ( String Name, Decimal Salary)
48 {
49 This . Name = Name;
50 This . Salary = Salary;
51 }
52 Public   String Name { Get ; Private   Set ;}
53 Public   Decimal Salary { Get ; Private   Set ;}
54 Public   Override   String Tostring ()
55 {
56 Return   String . Format ( " {0}, {1: c} " , Name, salary );
57 }
58 Public   Static   Bool Comparesalary (employee E1, employee E2)
59 {
60 Return E1.salary < E2.salary;
61 }
62 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.