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 ();}}}