java算法每日一练

 描述:有一组数(43,14,6,9,3,27,38,19,5等)
排序:将最大的放在第一位,最小放在第二位,剩下的最大的放在第三位,最小的放在第四位,以此类推
输出排序结果

 

作为一个新手,首先看到题目感觉有点难,他不像一般的大小排序可以调用工具类直接排序。

仔细想想其实也不是那么的复杂:

1.难点1:本身是个无序的组合,如果是个排序好的数组,或许就没有那么难了

2.排完序后,只要按照最后一个放第一位,原来的第一位放在后面,下面的以此类推。

 

 

 

        int[] a = new int[]{43,14,6,9,3,27,38,19,5};
        
        Arrays.sort(a); 

        for (int i = 0,j=a.length-1; i <= j;) 
        { 
            System.out.println(a[j--]);
            if(j==i)
            {
            	System.out.println(a[i]);
            }
            if(j>i)
            { 
                System.out.println(a[i++]); 
            } 
        } 


 

 

按照上的思路很快就能实现功能了。

 

 

由此,复习一下最近用到的排序,最近的一个有个优先级排序的功能,具体的掠过:

功能描述大概是这样的:按照规则对对象进行优先级排序,相同的优先级需要随机返回。

 下面的列子中People中的leavl是对象的优先级的体现。

 

 

public class People
{
	public People(String name,String leavl,String id)
	{
		this.id = id;
		this.name = name;
		this.leavl = leavl;
	}
	
	private String name;
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getLeavl()
	{
		return leavl;
	}

	public void setLeavl(String leavl)
	{
		this.leavl = leavl;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	private String leavl;
	
	private String id;
	
}

 

 

 

 

	public static void main(String[] args)
	{
		People people1 = new People("cxm",1,"1");
		People people2 = new People("zxc",1,"2");
		People people3 = new People("zxz",2,"3");
		People people4 = new People("zxx",4,"4");
		People people5 = new People("zxv",2,"5");
		People people6 = new People("zxb",3,"6");
		People people7 = new People("zxn",4,"7");
		People people8 = new People("zxm",3,"8");
		
		List<People> list = new ArrayList<People>();
		list.add(people1);
		list.add(people2);
		list.add(people3);
		list.add(people4);
		list.add(people5);
		list.add(people6);
		list.add(people7);
		list.add(people8);
		
		Collections.sort(list, new Comparator<People>()
		{
			public int compare(People arg0, People arg1)
			{
				return arg0.getLeavl().compareTo(arg1.getLeavl());
			}
		});
		
					
		Set<Integer> set = new HashSet<Integer>();
		set.add(people1.getLeavl());
		set.add(people2.getLeavl());
		set.add(people3.getLeavl());
		set.add(people4.getLeavl());
		set.add(people5.getLeavl());
		set.add(people6.getLeavl());
		set.add(people7.getLeavl());
		set.add(people8.getLeavl());
		
		Map<Integer ,List<People>> map = new HashMap<Integer, List<People>>();
		List<People> newList = null;
		for(Integer i : set)
		{
			newList = new ArrayList<People>();
			for(People people : list)
			{
				if(i.equals(people.getLeavl()))
				{
					newList.add(people);
				}
			}
			map.put(i, newList);
		}

		List<People> returnList = new ArrayList<People>();
		for(Map.Entry<Integer, List<People>> entry :map.entrySet() )
		{
			returnList.addAll(getNewOrderList(entry.getValue()));
		}
		
		for(People people : returnList)
		{
			System.out.println("name "+people.getName()+"   leavl "+people.getLeavl() +"   id "+people.getId());
		}
		
		
	}

	private static List<People> getNewOrderList(List<People> value)
	{
		Random rand = new Random();
		int listIndex = rand.nextInt(value.size());
		List<People> returnList = new ArrayList<People>();
		returnList.add(value.get(listIndex));
		value.remove(listIndex);
		returnList.addAll(value);
		return returnList;
	}


 


	public static void main(String[] args)
	{
		People people1 = new People("cxm",1,"1");
		People people2 = new People("zxc",1,"2");
		People people3 = new People("zxz",2,"3");
		People people4 = new People("zxx",4,"4");
		People people5 = new People("zxv",2,"5");
		People people6 = new People("zxb",3,"6");
		People people7 = new People("zxn",4,"7");
		People people8 = new People("zxm",3,"8");
		
		List<People> list = new ArrayList<People>();
		list.add(people1);
		list.add(people2);
		list.add(people3);
		list.add(people4);
		list.add(people5);
		list.add(people6);
		list.add(people7);
		list.add(people8);
		
		Collections.sort(list, new Comparator<People>()
		{
			public int compare(People arg0, People arg1)
			{
				return arg0.getLeavl().compareTo(arg1.getLeavl());
			}
		});
		
					
		Set<Integer> set = new HashSet<Integer>();
		set.add(people1.getLeavl());
		set.add(people2.getLeavl());
		set.add(people3.getLeavl());
		set.add(people4.getLeavl());
		set.add(people5.getLeavl());
		set.add(people6.getLeavl());
		set.add(people7.getLeavl());
		set.add(people8.getLeavl());
		
		Map<Integer ,List<People>> map = new HashMap<Integer, List<People>>();
		List<People> newList = null;
		for(Integer i : set)
		{
			newList = new ArrayList<People>();
			for(People people : list)
			{
				if(i.equals(people.getLeavl()))
				{
					newList.add(people);
				}
			}
			map.put(i, newList);
		}

		List<People> returnList = new ArrayList<People>();
		for(Map.Entry<Integer, List<People>> entry :map.entrySet() )
		{
			returnList.addAll(getNewOrderList(entry.getValue()));
		}
		
		for(People people : returnList)
		{
			System.out.println("name "+people.getName()+"   leavl "+people.getLeavl() +"   id "+people.getId());
		}
		
		
	}

	private static List<People> getNewOrderList(List<People> value)
	{
		Random rand = new Random();
		int listIndex = rand.nextInt(value.size());
		List<People> returnList = new ArrayList<People>();
		returnList.add(value.get(listIndex));
		value.remove(listIndex);
		returnList.addAll(value);
		return returnList;
	}


 

上面运用的排序:

1.

Collections.sort(list, new Comparator<People>()

实现了优先级的排序

2.

 

 

 

 

 


		Random rand = new Random();
		int listIndex = rand.nextInt(value.size());
		List<People> returnList = new ArrayList<People>();
		returnList.add(value.get(listIndex));
		value.remove(listIndex);
		returnList.addAll(value);


 

 

 

这里最终使用的那个值可能是People中的Id,实现了同一个优先级的随机返回。

测试的结果如下:

 

name zxc   leavl 1   id 2          name cxm   leavl 1   id 1      name zxc   leavl 1   id 2
name cxm   leavl 1   id 1          name zxc   leavl 1   id 2      name cxm   leavl 1   id 1
name zxv   leavl 2   id 5          name zxz   leavl 2   id 3      name zxz   leavl 2   id 3
name zxz   leavl 2   id 3          name zxv   leavl 2   id 5      name zxv   leavl 2   id 5
name zxm   leavl 3   id 8          name zxb   leavl 3   id 6      name zxm   leavl 3   id 8
name zxb   leavl 3   id 6          name zxm   leavl 3   id 8      name zxb   leavl 3   id 6
name zxn   leavl 4   id 7          name zxn   leavl 4   id 7      name zxn   leavl 4   id 7
name zxx   leavl 4   id 4          name zxx   leavl 4   id 4      name zxx   leavl 4   id 4


 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值