Data structure C # example Tutorial: Bubble Sorting Algorithm

Source: Internet
Author: User

I have found many errors in this book and feel that the quality is not very good. Therefore, you must pay attention when reading this book. Now, let's continue with this series. Today we mainly sort the simplest Bubble Sorting Algorithm. The so-called Bubble Sorting means that in each sorting process, there is always a maximum value that is moved to the back, and the smaller value is like a bubble that floats to the water. Next, let's take a look at the Code. There are comments in important parts. You can see comments.
1 class SortingAlgorithms
2 {
3 private int [] arr;
4 private int upper;
5 private int numElement;
6
7 // initialize the Array
8 public SortingAlgorithms (int size)
9 {
10 arr = new int [size];
11 upper = size-1;
12 numElement = 0;
13}
14
15 // Insert elements to the array
16 public void Insert (int item)
17 {
18 arr [numElement] = item;
19 numElement ++;
20}
21
22 // print the array element
23 public void DisplayElement ()
24 {
25 for (int I = 0; I <= upper; I ++)
26 {
27 Console. WriteLine (arr [I] + "");
28}
29 Console. ReadLine ();
30}
31
32
33 // bubble sort
34 public void BubbleSort ()
35 {
36 int Temp;
37 // The External Loop is to limit the number of elements for one bubble sort comparison.
38 for (int Outer = upper; Outer> = 1; Outer --)
39 {
40 // a bubble sequence is compared to 0 ~ Size of ourter-1 Elements
41 for (int Inner = 0; Inner <= Outer-1; Inner ++)
42 {
43 // sorting process
44 if (arr [Inner]> arr [Inner + 1])
45 {
46 Temp = arr [Inner];
47 arr [Inner] = arr [Inner + 1];
48 arr [Inner + 1] = Temp;
49}
50}
51}
52 // this. DisplayElement ();
53}
54
55}
56
57. The process of calling an algorithm is as follows:
58 static void Main (string [] args)
59 {
60 SortingAlgorithms MyArray = new SortingAlgorithms (10 );
61 Random rnd = new Random (100 );
62 for (int I = 0; I <10; I ++)
63 {
64 MyArray. Insert (int) (rnd. NextDouble () * 100 ));
65}
66 Console. WriteLine ("Before Sorting :");
67 MyArray. DisplayElement ();
68 // sort
69 MyArray. BubbleSort ();
70 Console. WriteLine ("After sorting ");
71 // print the sorted Elements
72 MyArray. DisplayElement ();
73}

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.