zoukankan      html  css  js  c++  java
  • 一、数组

    1、数组的增删查 

    // to run this program: C>java ArrayAppclass Array
       {
       private long[] a;          
       private int nElems;          
       public Array(int max)         // constructor
          {
          a = new long[max];                 // create the array
          nElems = 0;                        // no items yet
          }
       //-----------------------------------------------------------
       public boolean find(long searchKey)
          {                              // find specified value
          int j;
          for(j=0; j<nElems; j++)            // for each element,
             if(a[j] == searchKey)           // found item?
                break;                       // exit loop before end
          if(j == nElems)                    // gone to end?
             return false;                   // yes, can't find it
          else
             return true;                    // no, found it
          }  // end find()
       //-----------------------------------------------------------
       public void insert(long value)    // put element into array
          {
          a[nElems] = value;             // insert it
          nElems++;                      // increment size
          }
       //-----------------------------------------------------------
       public boolean delete(long value)
          {
          int j;
          for(j=0; j<nElems; j++)        // look for it
             if( value == a[j] )
                break;
          if(j==nElems)                  // can't find it
             return false;
          else                           // found it
             {
             for(int k=j; k<nElems; k++) // move higher ones down
                a[k] = a[k+1];
             nElems--;                   // decrement size
             return true;
             }
          }  // end delete()
       //-----------------------------------------------------------
       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("");
          }
       //-----------------------------------------------------------
       }  // end class HighArray
    ////////////////////////////////////////////////////////////////
    class ArrayApp
       {
       public static void main(String[] args)
          {
          int maxSize = 100;            // array size
          HighArray arr;                // reference to array
          arr = new HighArray(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
    
          int searchKey = 35;           // search for item
          if( arr.find(searchKey) )
             System.out.println("Found " + searchKey);
          else
             System.out.println("Can't find " + searchKey);
    
          arr.delete(00);               // delete 3 items
          arr.delete(55);
          arr.delete(99);
    
          arr.display();                // display items again
          }  // end main()
       }  // end class HighArrayApp

     

      

    2、有序数组的二分查找

    // orderedArray.java
    // demonstrates ordered array class
    // to run this program: C>java OrderedApp
    ////////////////////////////////////////////////////////////////
    class OrdArray
       {
       private long[] a;                 // ref to array a
       private int nElems;               // number of data items
       //-----------------------------------------------------------
       public OrdArray(int max)          // constructor
          {
          a = new long[max];             // create array
          nElems = 0;
          }
       //-----------------------------------------------------------
       public int size()
          { return nElems; }
       //-----------------------------------------------------------
       public int find(long searchKey)
          {
          int lowerBound = 0;
          int upperBound = nElems-1;
          int curIn;
    
          while(true)
             {
             curIn = (lowerBound + upperBound ) / 2;
             if(a[curIn]==searchKey)
                return curIn;              // found it
             else if(lowerBound > upperBound)
                return nElems;             // can't find it
             else                          // divide range
                {
                if(searchKey > a[curIn])
                   lowerBound = curIn + 1; // it's in upper half
                else
                   upperBound = curIn - 1; // it's in lower half
                }  // end else divide range
             }  // end while
          }  // end find()
       //-----------------------------------------------------------
       public void insert(long value)    // put element into array
          {
          int j;
          for(j=0; j<nElems; j++)        // find where it goes
             if(value < a[j])            // (linear search)
                break;
          for(int k=nElems; k>j; k--)    // move bigger ones up
             a[k] = a[k-1];
          a[j] = value;                  // insert it
          nElems++;                      // increment size
          }  // end insert()
       //-----------------------------------------------------------
       public boolean delete(long value)
          {
          int j = find(value);
          if(j==nElems)                  // can't find it
             return false;
          else                           // found it
             {
             for(int k=j; k<nElems; k++) // move bigger ones down
                a[k] = a[k+1];
             nElems--;                   // decrement size
             return true;
             }
          }  // end delete()
       //-----------------------------------------------------------
       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("");
          }
       //-----------------------------------------------------------
       }  // end class OrdArray

    //////////////////////////////////////////////////////////////// class OrderedApp { public static void main(String[] args) { int maxSize = 100; // array size OrdArray arr; // reference to array arr = new OrdArray(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); int searchKey = 55; // search for item if( arr.find(searchKey) != arr.size() ) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.display(); // display items arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again } // end main() } // end class OrderedApp

    线性查找         O(N)

    二分查找         O(logN)

    无序数组的插入      O(1)

    有序数组的插入        O(N)

    无序数组的删除      O(N)

    有序数组的删除      O(N)

  • 相关阅读:
    mysql 往表中insert的时候如何让主键id按当前表的最大值自动增长?
    visual studio 2013 win7安装笔记
    mysql奇葩之旅
    java JVM常见的四大异常及处理方案
    DDR3_旧版(2):初始化
    DDR3_旧版(1):IP核调取
    【转】AXI_Lite 总线详解
    ZYNQ笔记(7):AXI从口自定义IP封装
    ZYNQ笔记(6):普通自定义IP封装实现PL精准定时中断
    ZYNQ笔记(5):软中断实现核间通信
  • 原文地址:https://www.cnblogs.com/xxlong/p/4975962.html
Copyright © 2011-2022 走看看