Find and sort 05, bubble sort

Source: Internet
Author: User

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

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.