Java data structure and Algorithm for Bubble Sorting

Source: Internet
Author: User

 

Class arraybub {

Private long [] A; // ref to array
Private int nelems; // Number of date items
Public arraybub (INT max) {// Constructor
A = new long [Max]; // create the Array
Nelems = 0; // No items yet
}

/**
* Insert
* @ Param Value
*/
Public void insert (long value) // put element into Array
{
A [nelems] = value; // insert it
Nelems ++; // increment size
}

/*
* Display data
*/
Public void display () // displays array Contents
{
For (Int J = 0; j <nelems; j ++)
// For each element,
System. Out. Print (A [J] + ""); // display it
System. Out. println ("");
}

/**
* Bubble Sorting
*/
Public void bubblesort (){
Int out, in;
For (out = nelems-1; out> 1; out --)
For (in = 0; in <out; in ++)
If (A [in]> A [in + 1])
Swap (in, in + 1 );
} // End bubblesort ()
//--------------------------------------------

Private void swap (INT one, int two ){
Long temp = A [one];
A [one] = a [two];
A [two] = temp;
}
//---------------------------------------------
} // End class arraybub
//////////////////////////////////////// ///////////

Public class bubblesort {
Public static void main (string ARGs []) {
Int maxsize = 100; // array size
Arraybub arr; // reference to array
Arr = new arraybub (maxsize); // create the Array
Arr. insert (77); // Insert 10 items
Arr. insert (99 );
Arr. insert (44 );
Arr. insert (55 );
Arr. insert (22 );
Arr. insert (88 );
Arr. insert (11 );
Arr. insert (00 );
Arr. insert (66 );
Arr. insert (33 );
Arr. Display (); // display items
Arr. bubblesort (); // bubble sort them
Arr. Display ();
} // End main ()
} // End class bubblesortapp

 

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.