java 簡單排序

來源:互聯網
上載者:User

package binary;

/**
 * 基礎資料型別 (Elementary Data Type)的簡單排序
 * @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();
 }
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.