Delegated application --- bubble, delegated application --- bubble
Code directly, the code is relatively simple
1 class Program 2 { 3 private delegate string GetAString(); 4 static void Main(string[] args) 5 { 6 7 int[] a = { 0, 5, 6, 2, 1 }; 8 BubbleSorter.Sort(a, BubbleSorter.Compa); 9 foreach (var item in a)10 {11 Console.Write(item);12 }13 Console.ReadLine();14 }15 16 public static void SortA(int[] sortArray)17 {18 bool swapped = true;19 do20 {21 swapped = false;22 for (int i = 0; i < sortArray.Length - 1; i++)23 {24 if (sortArray[i] < sortArray[i + 1])25 {26 int temp = sortArray[i];27 sortArray[i] = sortArray[i + 1];28 sortArray[i + 1] = temp;29 swapped = true;30 }31 }32 }33 while (swapped);34 }35 }36 37 class BubbleSorter38 {39 static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)40 {41 bool swapped = true;42 do43 {44 swapped = false;45 for (int i = 0; i < sortArray.Count - 1; i++)46 {47 if (comparison(sortArray[i + 1], sortArray[i]))48 {49 T temp = sortArray[i];50 sortArray[i] = sortArray[i + 1];51 sortArray[i + 1] = temp;52 swapped = true;53 }54 }55 } while (swapped);56 }57 58 public static bool Compa(int i, int j)59 {60 return i < j;61 }62 }
Use c # To compile a program for sorting by the Bubble Method
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace GanggangApplication
{
Class Program
{
Static void Main (string [] args)
{
SortedNumbers ();
}
/// <Summary>
/// This method obtains the array to be sorted, and the table calls the sorting method for sorting
/// </Summary>
Public static void SortedNumbers ()
{
Int numberCount;
Int [] numbers;
Console. WriteLine ("How many numbers do you want to sort? ");
NumberCount = Convert. ToInt32 (Console. ReadLine ());
Numbers = new int [numberCount];
Console. WriteLine ("Enter the {0} number you want to sort:", numberCount );
For (int I = 0; I <numberCount; I ++)
{
Numbers [I] = Convert. ToInt32 (Console. ReadLine ());
}
Console. WriteLine ("\ n the {0} numbers you want to sort are:", numberCount );
For (int I = 0; I <numberCount; I ++)
{
Console. Write (numbers [I]. ToString () + "\ t ");
}
Console. WriteLine ("\ n what sort of {0} numbers do you want to sort? (1 indicates Bubble sorting, 2 indicates sorting) ", numberCount );
Int method = Convert. ToInt32 (Console. ReadLine ());
While (method! = 1 & method! = 2)
{
Console. WriteLine ("only 1 or 2 can be entered. Please try again! ");
Method = Convert. ToInt32 (Console. ReadLine ());
}
// Call the sorting method
ExecuteSortedMe ...... remaining full text>
Compile a Bubble sorting method in JAVA.
I used it for school n years ago. Look at it. Hope to help you
Public class Test
{
Public void Sx (int [] t)
{
For (int I = 0; I <t. length; I ++)
{
If (t [I] <10)
{
System. out. print ("0" + t [I] + "");
}
Else
{
System. out. print (t [I] + "");
}
If (I + 1) % 6 = 0)
{
System. out. println ();
}
}
}
Public static void main (String [] args)
{
Test m = new Test ();
Int a [] = new int [36];
Out1: for (int I = 0; I <36; I ++)
{
A [I] = (int) (Math. random () * 36 + 1 );
For (int j = 0; j <36 & j! = I; j ++)
{
If (a [j] = a [I])
{
I --;
Continue out1;
}
}
}
M. Sx ();
System. out. println ();
// Arrays. sort ();
// Bubble Method
Int temp;
For (int I = 0; I <a. length; I ++ ){
For (int j = 0; j + 1 <a. length-I; j ++ ){
If (a [j]> a [j + 1]) {
Temp = a [j];
A [j] = a [j + 1];
A [j + 1] = temp;
}
}
}
M. Sx ();
}
}... Remaining full text>