Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2539616
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: Java

2016-08-18 15:53:25



点击(此处)折叠或打开

  1. // orderedArray.java
  2. // demonstrates ordered array class
  3. // to run this program: C>java OrderedApp
  4. ////////////////////////////////////////////////////////////////
  5. class OrdArray
  6.    {
  7.    private long[] a; // ref to array a
  8.    private int nElems; // number of data items
  9.    //-----------------------------------------------------------
  10.    public OrdArray(int max) // constructor
  11.       {
  12.       a = new long[max]; // create array
  13.       nElems = 0;
  14.       }
  15.    //-----------------------------------------------------------
  16.    public int size()
  17.       { return nElems; }
  18.    //-----------------------------------------------------------
  19.    public int find(long searchKey)
  20.       {
  21.       int lowerBound = 0;
  22.       int upperBound = nElems-1;
  23.       int curIn;

  24.       while(true)
  25.          {
  26.          curIn = (lowerBound + upperBound ) / 2;
  27.          if(a[curIn]==searchKey)
  28.             return curIn; // found it
  29.          else if(lowerBound > upperBound)
  30.             return nElems; // can't find it
  31.          else // divide range
  32.             {
  33.             if(a[curIn] < searchKey)
  34.                lowerBound = curIn + 1; // it's in upper half
  35.             else
  36.                upperBound = curIn - 1; // it's in lower half
  37.             } // end else divide range
  38.          } // end while
  39.       } // end find()
  40.    //-----------------------------------------------------------
  41.    public void insert(long value) // put element into array
  42.       {
  43.       int j;
  44.       for(j=0; j<nElems; j++) // find where it goes
  45.          if(a[j] > value) // (linear search)
  46.             break;
  47.       for(int k=nElems; k>j; k--) // move bigger ones up
  48.          a[k] = a[k-1];
  49.       a[j] = value; // insert it
  50.       nElems++; // increment size
  51.       } // end insert()

  52.    //采用二分查找法,进行数据插入操作 递增排序 程晓鹏 2016.08.18 add 编程作业2.4
  53.    public void insert2(long value){
  54.       int j; //要插入数据的位置
  55.       int bIdx = 0; // 二分查找开始索引号码
  56.       int eIdx = nElems - 1; //二分查找终止编码
  57.       int curIdx = 0; //中间索引编码
  58.       while(true){
  59.      curIdx = (bIdx + eIdx) / 2;
  60.          if(bIdx > eIdx){ //当不含任何元素时
  61.              j = nElems;
  62.      break;
  63.      }else if(bIdx == curIdx && nElems > 0){
  64.      if(value >= a[bIdx] && value >= a[eIdx]){
  65.         j = eIdx+1;
  66.      }else if (value >= a[bIdx]){
  67.         j = eIdx;
  68.      }
  69.      else{
  70.         j =bIdx;
  71.      }
  72.      break;
  73.      }else if (a[curIdx] < value){
  74.      bIdx = curIdx + 1;
  75.      }else{
  76.              eIdx = curIdx - 1;
  77.      }
  78.       }
  79.       System.out.println("value = " + value + ",insert idx = " + j + ", bIdx=" + bIdx + ", eIdx=" + eIdx + ", mIdx = " +curIdx);
  80.        for(int k=nElems; k>j; k--) // move bigger ones up
  81.          a[k] = a[k-1];
  82.       a[j] = value; // insert it
  83.       nElems++; // increment size
  84.      
  85.    }
  86.    //-----------------------------------------------------------
  87.    public boolean delete(long value)
  88.       {
  89.       int j = find(value);
  90.       if(j==nElems) // can't find it
  91.          return false;
  92.       else // found it
  93.          {
  94.          for(int k=j; k<nElems; k++) // move bigger ones down
  95.             a[k] = a[k+1];
  96.          nElems--; // decrement size
  97.          return true;
  98.          }
  99.       } // end delete()
  100.    //-----------------------------------------------------------
  101.    public void display() // displays array contents
  102.       {
  103.       for(int j=0; j<nElems; j++) // for each element,
  104.          System.out.print(a[j] + " "); // display it
  105.       System.out.println("");
  106.       }
  107.    //-----------------------------------------------------------
  108.    } // end class OrdArray
  109. ////////////////////////////////////////////////////////////////
  110. class OrderedApp
  111.    {
  112.    public static void main(String[] args)
  113.       {
  114.       int maxSize = 100; // array size
  115.       OrdArray arr; // reference to array
  116.       arr = new OrdArray(maxSize); // create the array

  117.       arr.insert2(77); //编程作业 2.4
  118.       arr.insert2(99);
  119.       arr.insert2(44);
  120.       arr.insert2(11);
  121.       arr.insert2(55);
  122.       arr.insert2(22);
  123.       arr.insert2(88);
  124.       arr.insert2(00);
  125.       arr.insert2(66);
  126.       arr.insert2(33);

  127.       System.out.println("insert data result is");
  128.       arr.display();
  129.       int searchKey = 55; // search for item
  130.       if( arr.find(searchKey) != arr.size() )
  131.          System.out.println("Found " + searchKey);
  132.       else
  133.          System.out.println("Can't find " + searchKey);

  134.       arr.display(); // display items

  135.       arr.delete(00); // delete 3 items
  136.       arr.delete(55);
  137.       arr.delete(99);

  138.       arr.display(); // display items again
  139.       } // end main()
  140.    }
执行结果



点击(此处)折叠或打开

  1. // highArray.java
  2. // demonstrates array class with high-level interface
  3. // to run this program: C>java HighArrayApp
  4. ////////////////////////////////////////////////////////////////
  5. class HighArray
  6.    {
  7.    private long[] a; // ref to array a
  8.    private int nElems; // number of data items
  9.    //-----------------------------------------------------------
  10.    public HighArray(int max) // constructor
  11.       {
  12.       a = new long[max]; // create the array
  13.       nElems = 0; // no items yet
  14.       }
  15.    //-----------------------------------------------------------
  16.    public boolean find(long searchKey)
  17.       { // find specified value
  18.       int j;
  19.       for(j=0; j<nElems; j++) // for each element,
  20.          if(a[j] == searchKey) // found item?
  21.             break; // exit loop before end
  22.       if(j == nElems) // gone to end?
  23.          return false; // yes, can't find it
  24.       else
  25.          return true; // no, found it
  26.       } // end find()
  27.    //-----------------------------------------------------------
  28.    //获取最大值函数 编程作业2.1 程晓鹏 2016.08.17 add
  29.    public long getMax(){
  30.       long result = -1;
  31.       for(int i=0; i<nElems; i++){
  32.      if(a[i] > result){
  33.              result = a[i];        
  34.      }
  35.       }
  36.       return result;
  37.    }
  38.   
  39.    //删除最大值 编程作业2.2 程晓鹏 2016.08.17 add
  40.    public long removeMax(){
  41.       long result = -1;
  42.       int i=-1; // 最大值索引值
  43.       for(int ii=0; ii<nElems; ii++){
  44.      if(a[ii] > result){
  45.              result = a[ii];
  46.              i = ii;    
  47.      }
  48.       }

  49.       if(i !=nElems && i != -1){
  50.      for(int j=i; j<nElems; j++){
  51.          a[j] = a[j+1];
  52.      }    
  53.      nElems--;
  54.       }
  55.       return result;
  56.    }

  57.    //选择排序 编程作业2.3 程晓鹏 2016.08.17 add
  58.    public void xuanzeSort(){
  59.      for (int i=0; i<nElems; i++){
  60.      long max=a[i];
  61.      int index = i;
  62.      for(int j=i+1; j<nElems; j++){
  63.             if(a[j]>max){
  64.          max=a[j];
  65.          index = j;
  66.      }
  67.      }

  68.      if(index != i){
  69.      long temp=a[i];
  70.      a[i]=a[index];
  71.      a[index]=temp;
  72.      }

  73.      }
  74.    }

  75.    //删除重复项 编程作业2.6 程晓鹏 2016.08.18 add
  76.    public void noDup(){
  77.       for(int i=0; i<nElems; i++){
  78.          long tmp = a[i];
  79.      for(int j=i+1; j<nElems; j++){
  80.      if(tmp == a[j]){ //当出现重复数据时,进行
  81.          for(int k=j; k<nElems; k++){
  82.          a[k] = a[k+1];
  83.          }
  84.         
  85.          nElems--;
  86.          j--; //减一,防止两个连续的重复的出现
  87.      }
  88.      }
  89.       }
  90.    }

  91.    //-----------------------------------------------------------
  92.    public void insert(long value) // put element into array
  93.       {
  94.       a[nElems] = value; // insert it
  95.       nElems++; // increment size
  96.       }
  97.    //-----------------------------------------------------------
  98.    public boolean delete(long value)
  99.       {
  100.       int j;
  101.       for(j=0; j<nElems; j++) // look for it
  102.          if( value == a[j] )
  103.             break;
  104.       if(j==nElems) // can't find it
  105.          return false;
  106.       else // found it
  107.          {
  108.          for(int k=j; k<nElems; k++) // move higher ones down
  109.             a[k] = a[k+1];
  110.          nElems--; // decrement size
  111.          return true;
  112.          }
  113.       } // end delete()
  114.    //-----------------------------------------------------------
  115.    public void display() // displays array contents
  116.       {
  117.       for(int j=0; j<nElems; j++) // for each element,
  118.          System.out.print(a[j] + " "); // display it
  119.       System.out.println("");
  120.       }
  121.    //-----------------------------------------------------------
  122.    } // end class HighArray
  123. ////////////////////////////////////////////////////////////////
  124. class HighArrayApp
  125.    {
  126.    public static void main(String[] args)
  127.       {
  128.       int maxSize = 100; // array size
  129.       HighArray arr; // reference to array
  130.       arr = new HighArray(maxSize); // create the array

  131.       arr.insert(77); // insert 10 items
  132.       arr.insert(99);
  133.       arr.insert(44);
  134.       arr.insert(55);
  135.       arr.insert(22);
  136.       arr.insert(88);
  137.       arr.insert(11);
  138.       arr.insert(00);
  139.       arr.insert(66);
  140.       arr.insert(22);
  141.       arr.insert(33);
  142.       arr.insert(15);
  143.       arr.insert(22);
  144.       arr.insert(14);

  145.       arr.display(); // display items

  146.       int searchKey = 35; // search for item
  147.       if( arr.find(searchKey) )
  148.          System.out.println("Found " + searchKey);
  149.       else
  150.          System.out.println("Can't find " + searchKey);

  151.       arr.delete(00); // delete 3 items
  152.       arr.delete(55);
  153.       arr.delete(33);

  154.       arr.display(); // display items again
  155.       arr.noDup(); //编程作业2.6
  156.       System.out.println("delete dup data result");
  157.       arr.display();
  158.       long iMax = arr.getMax(); //编程作业 2.1
  159.       System.out.println("the max is " + iMax);
  160.       long reMax = arr.removeMax(); //编程作业2.2
  161.       System.out.println("remove max is " + reMax);
  162.       arr.display();
  163.       System.out.println("xuanze sort result");
  164.       arr.xuanzeSort(); //编程作业2.3
  165.       arr.display();
  166.       } // end main()
  167.    }

执行结果:

Array.rar

阅读(2168) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~