In the experience of "select Sort" and "insert sort", this article experiences "bubbling sort", iterating through the elements in the array, in ascending order, if the previous position element is larger than the latter, the two exchange positions.
Customize a class that handles an array of integers, including adding, displaying, purging, and bubbling methods, and getting the properties of the array length.
class Myintarray
{
Private int [] arr;
Private int //MAX index
Private int //Current index
Public Myintarray (int size)
{
New int [Size];
upper = size-1;
index = 0;
}
Public int Length
{
Get return Upper + 1; }
}
Public void Insert (int ele)
{
Arr[index] = ele;
index++;
}
Public void Display ()
{
for (int i = 0; i <= Upper; i++)
{
Console.Write (Arr[i] + "");
}
}
Public void Clear ()
{
for (int i = 0; i <= Upper; i++)
{
Arr[i] = 0;
}
index = 0;
}
Bubble sort: Countdown from the last index
Public void Bubblesort ()
{
int temp;
Traverse forward from the largest index until the index is 1
for (int i = upper; I >= 1; i--)
{
Iterate backward from the smallest index until the current element
for (int j = 0; J <= I-1; j + +)
{
if (Arr[j] > Arr[j+1])
{
temp = Arr[j];
ARR[J] = arr[j + 1];
Arr[j + 1] = temp;
}
}
}
}
}
Above, in the bubbling method, the last index begins to traverse forward, until the index is 1, and then iterates from the position of index 0, to the previous position of the currently traversed element, as long as the left element is found to be greater than the right element, the two swap positions.
Client calls, which are displayed after sorting.
Static void Main (string[] args)
{
New Myintarray (10);
New Random (100);
for (int i = 0; i < Nums. Length; i++)
{
Nums. Insert (R.next (100));
}
Nums. Bubblesort ();
Nums. Display ();
}
The Find and Sort series includes:
Find and Sort 01, linear find, time complexity, algorithm find and sort 02, binary find and sort 03, select Sort Find with sort 04, insert sort find with sort 05, bubble sort
Find and sort 05, bubble sort