Java Simple sorting

Source: Internet
Author: User

Package binary;

/**
* Simple sorting of basic data types
* @author TFQ
*
*/
public class Easysortarray {

Private long[] arr;
private int nitems;

Public Easysortarray (int maxLength) {
This.arr=new Long[maxlength];
this.nitems=0;
}

/*
* put element into array
*/
public void Insert (int element) {
Insert it
Arr[nitems]=element;
Increment size
nitems++;
}

/**
* Sort array element from small to big
*
*/
public void Easysort () {
int out,in;
for (out=0;out<nitems;out++) {
for (in=out;in<nitems;in++) {
if (Arr[out]>arr[in]) {
Long temp=arr[out];
Arr[out]=arr[in];
Arr[in]=temp;
}
}
}
}

/**
* Change Array[one] ' s value into array[two] ' s value
* If add the code to Easysort contributing to save sort time
* @param One
* @param two
*/
public void swap (int one,int two) {
Long Temp=arr[one];
Arr[one]=arr[two];
Arr[two]=temp;
}

public void display () {
for (int i=0;i<nitems;i++) {
System.out.print (arr[i]+ "");
}
}


public static void Main (string[] args) {
int maxlength=10;
Easysortarray esarray=new Easysortarray (maxLength);
Esarray.insert (1);
Esarray.insert (3);
Esarray.insert (5);
Esarray.insert (7);
Esarray.insert (2);
Esarray.insert (4);
Esarray.insert (6);
Esarray.insert (8);
Esarray.insert (10);

Esarray.display ();
System.out.println ("-------");
Esarray.easysort ();

Esarray.display ();
}

}

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.