Data structure C # instance tutorial: Bubble sort algorithm

Source: Internet
Author: User
Tags comments int size sort

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,

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.