arraysort--bubble Sort, select sort, insert Sort Tool class Demo

Source: Internet
Author: User

public class ArraySort {private long[] a;private int nelems;public arraysort (int max) {a=new long[max];nelems=0;} public void Insert (Long value) {a[nelems]=value;nelems++;} public void display () {for (int j=0;j<nelems;j++) {System.out.print (a[j]+ ",");} System.out.println ("");} public void swap (int one,int) {long temp=a[one];a[one]=a[two];a[two]=temp;} /** * @Title: Bubblesort * @Description: Bubbling sort * @return: void */public void Bubblesort () {int out,in;for (out=nelems-1;out& gt;1;out--)//Outer loop (out pointer moves to the left of the array) {for (in=0;in<out;in++)///inner loop (in pointer to right) {if (a[in]>a[in+1])//is not the correct order of the left small right size? {swap (in,in+1);//Exchange Them}}}} /** * * @Title: Selectsort * @Description: Select sort * @return: void */public void Selectsort () {int out,in,min;for (OUT=0;OUT&L t;nelems-1;out++)//Outer Loop {min=out;//Specifies the minimum value as the position value of the Out pointer for (in=out+1;in<nelems;in++)//inner Loop {if (A[in]<a[min])/ /is the value of the current in pointer smaller than the minimum that min refers to?  min=in;//specifies the minimum value as the data}swap (out, min) of the current in pointer, or//exchange out and Min values}}/** * @Title: Insertsort * @Description: Insert sort * @return: void */public void INSErtsort () {int In,out;long temp;for (out=1;out<nelems;out++)//out is the dividing line between the ordered part on the left and the unordered part on the right {temp=a[out];// Save the cutoff data to a temporary variable in=out;//move data from out to the right while (in>0 && a[in-1]>=temp)//until you move to a smaller data than out {a[in]=a[in-1] ;//move data one position to the right--in;//in pointer left one position}a[in]=temp;}} public static void Main (string[] args) {int maxsize=10; ArraySort arr;arr=new ArraySort (maxSize);        Arr.insert (77);        Arr.insert (99);        Arr.insert (45);        Arr.insert (55);        Arr.insert (23);        Arr.insert (86);        Arr.insert (12);        Arr.insert (00);        Arr.insert (65);        Arr.insert (34);        Arr.display ();        Arr.insertsort ();        Arr.display ();        Arr.bubblesort ();        Arr.display ();        Arr.selectsort (); Arr.display ();} /** * Sorting algorithm idea description: public void Bubblesort (), the outer for loop counter is out from the end of the array, that is, out equals nElems-1, every time a loop is out minus one. Data items with subscript greater than out are already well-sequenced.       The variable out moves left one bit after each completion of the internal loop (the counter is in), so the algorithm no longer processes the already sequenced data. The inner for Loop counter in is calculated from the beginning of the array, that is, in=0, each time the inner loop body is completed plus one, and the loop is closed when it equals out. In the inner for loop body, the array is labeled in and in+1 two data itemsThe comparison is that if the data item with the subscript in is greater than the data item with the subscript in+1, two data items are exchanged. public void Selectsort (), the outer loop uses the loop variable out, starting at the beginning of the array (array subscript 0) to the high position.       The inner loop uses the loop variable in, starting at the point where it is out, and also shifting to the right. In the new location of each in, data items a[in] and a[min] are compared. If A[in] is smaller, Min is assigned a value of in. At the end of the inner loop, min points to the smallest data item, then swaps the data pointing to the Out and Min. public void Insertsort (), in the outer layer for loop, the out variable starts at 1 and moves to the right. It marks the leftmost data of the unsorted part. In the inner while loop, the in variable starts at the out variable and moves to the left (copy) until the TEMP variable is less than the array data item in which it refers, or it can no longer move to the left. Each trip to the while loop moves a sorted data item to the right. Comparison of several sorts: bubble sort: Very easy to write, but generally not, the amount of data is very small or some application value. Select sort: Although the number of exchanges is minimized, the number of comparisons is still large, and it can be applied when the amount of data is small and the exchange is more time-consuming than the comparison. Insert sort: When the data volume is small or basic order, the insertion algorithm is the best choice among the three. Compared to random data, it is generally one-fold faster than bubbling, and slightly faster than the selection. Because it is less expensive to move data than to exchange data. */}

  

arraysort--bubble Sort, select sort, insert Sort Tool class Demo

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.