• java不用任何已有方法完全自写的去重法


    package aa;
    class InsertSort{
    	private long[] a;
    	private int nElems;
    	//构造方法
    	public InsertSort(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.println(a[j] + " ");
    		}
    		System.out.println("");
    	}
    	//排序方法
    	public long[] insertionSort(){
    		int out,in;
    		for(out = 1; out < nElems; out++)
    		{
    			long temp = a[out];
    			in = out;
    			while(in > 0 && a[in-1] >= temp)
    			{
    				a[in] = a[in - 1];
    				in --;
    			}
    			a[in] = temp;
    		}
    		return a;
    	}
    	//去重方法
    	public void noDups(long[] arr){
    		
    		for(int i = 1; i < nElems - 1; i++)//遍历数组元素,从第二项开始
    		{
    			for(int j = 0; j < i ; j++)//每一轮比较的次数
    			{
    				if(a[j] == a[i]) //如果遍历项和之前某一项相等
    				{
    					int k = i;//取出遍历项的索引
    					while(k+1 < nElems){//遍历 遍历项之后的元素项
    						a[k] = a[k+1];//将遍历项之后的元素项前移一位
    						k++;						
    					}
    					nElems --;//数组长度减一
    					i--;//因为遍历项后一项前移到遍历项,如果i不减一则少比较一项
    				}
    				
    			}
    		}
    	}
    }
    public class InsertSortApp {
    	public static void main(String[] args){
    		int maxSize = 100;
    		InsertSort arr = new InsertSort(maxSize);
    		
    		arr.insert(77);
    		arr.insert(99);
    		arr.insert(44);
    		arr.insert(55);
    		arr.insert(22);		
    		arr.insert(88);
    		arr.insert(11);
    		arr.insert(00);
    		arr.insert(77);
    		arr.insert(77);
    		
    		arr.display();
    		long[] sortarr = arr.insertionSort();
    		arr.display();
    		arr.noDups(sortarr);
    		arr.display();
    	}
    	
    }
    

      

  • 相关阅读:
    类函数指针
    resource for machine learning
    蒲丰投针与蒙特卡洛模拟
    IIS5、IIS6、IIS7的ASP.net 请求处理过程比较
    CMD 命令速查手册
    Process, Thread, STA, MTA, COM object
    在托管代码中设置断点(Windbg)
    SidebySide Execution
    .NET Framework 3.5 Architecture
    Overview of the .NET Framework
  • 原文地址:https://www.cnblogs.com/iwebkit/p/7533206.html
Copyright © 2020-2023  润新知