Because this book I found more mistakes, the feeling of quality is not very good, so we must pay attention to when looking. OK, let's move on to this series, which is basically the simplest bubble sort in the sorting algorithm today. The so-called bubble sort is that in each order of the process there is always a maximum value is moved to the back, the value of small like blisters float to the surface. Here we will look at the code, important places have comments, you can read comments.
1 class Sortingalgorithms
2 {
3 private int[] arr;
4 private int Upper;
5 private int numelement;
6
7//initialization array
8 public sortingalgorithms (int size)
9 {
arr = new Int[size];
One upper = size-1;
numelement = 0;
13}
14
15//Insert element to array
public void Insert (int item)
17 {
Arr[numelement] = Item;
numelement++;
20}
21st
22//Print array elements
public void Displayelement ()
24 {
for (int i = 0; i <= Upper; i++)
26 {
Console.WriteLine (Arr[i] + "");
28}
Console.ReadLine ();
30}
31
32
33//Bubble sort
The public void Bubblesort ()
35 {
int Temp;
37//Outer loop is to limit the number of elements of a bubble sort comparison
for (int Outer = upper; Outer >= 1; outer--)
39 {
40//One bubble sort compares the size of 0~ourter-1 elements
A for (int Inner = 0; Inner <= Outer-1; inner++)
42 {
43//Sort Process
if (Arr[inner] > Arr[inner + 1])
45 {
Temp = Arr[inner];
Arr[inner] = Arr[inner + 1];
Arr[inner + 1] = Temp;
49}
50}
51}
//this. Displayelement ();
53}
54
55}
56
571 is the process of invoking the algorithm:
args static void Main (string[])
59 {
Sortingalgorithms myarray = new Sortingalgorithms (10);
Random rnd = new Random (100);
for (int i = 0; i < i++)
63 {
Myarray.insert (int) (RND. Nextdouble () *100));
65}
Console.WriteLine ("Before Sorting:");
Myarray.displayelement ();
68//Sort
Myarray.bubblesort ();
Console.WriteLine ("After sorting");
71//Print the sorted element
Myarray.displayelement ();
73}
Note : For more wonderful tutorials, please pay attention to the Triple design Tutorials section,