Bubble Sorting (generic + delegation) and Bubble Sorting

Source: Internet
Author: User

Bubble Sorting (generic + delegation) and Bubble Sorting

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Namespace ConsoleApp1
{
Class Program
{

Static void Main (string [] args)
{
// Declare a class
People [] arrays = new People [5];
Arrays [0] = new People ("p1", 3 );
Arrays [1] = new People ("p1", 321 );
Arrays [2] = new People ("p1", 12 );
Arrays [3] = new People ("p1", 3231 );
Arrays [4] = new People ("p1", 21 );

// Call the Bubble Method
SortMethod (arrays, People. ComPare );
// After the output is called
Foreach (People P in arrays)
{
Console. WriteLine (P );
}
Console. ReadKey ();
}
// Define the Bubble Method
Static bool isChange = false;
Public static void SortMethod (People [] arrays, Func <int, int, bool> compare)
{
Do
{
IsChange = false;
For (int I = 0; I <arrays. Length-1; I ++)
{
If (compare (arrays [I]. salary, arrays [I + 1]. salary ))
{
People temp = arrays [I];
Arrays [I] = arrays [I + 1];
Arrays [I + 1] = temp;
IsChange = true;
}
}
}
While (isChange );
}

}
}

Below is a PeoPle class

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApp1 {class People {private string name; public int salary; public People (string name, int salary) {this. name = name; this. salary = salary;} public static bool ComPare (int salary1, int salary2) {if (salary1> salary2) return true; return false;} // rewrite public override string ToString () {return name + "," + salary. toString ();}}}

 

Related Article

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.