java怎么生成顺序序列_随机生成指定顺序序列与二分查找

本文探讨了如何使用Java生成不同条件下的整数序列,包括随机、不重复和有序,以及如何进行二分查找。通过示例代码展示了如何生成降序排列的整数序列,并进行了二分查找操作,输出了查找过程和次数。
摘要由CSDN通过智能技术生成

闲来无事就找事,请看!随机生成 K 个整数;☆

随机生成 K 个不重复的整数;☆☆

随机生成 K 个不重复且有序的整数;☆☆

查找 3 中是否存在某个数,若存在给出索引位置;☆☆☆

随机生成 K 个不重复且降序排列的整数;★

随机生成 K 个不重复且降序排列的在一定范围[M-N]内的整数;★☆

随机生成 K 个不重复且降序和升序排列的在一定范围[M-N]内的整数,并查找某个数是否存在其中,存在给出位置,不存在给出提示;★★☆

根据 7 所述,输出示例如下:[10, 14, 17, 46, 48, 59, 60, 79, 85, 86]

存在 元素 10 索引为: 0

查找次数:3

顺序: 10-->14-->46

[17, 16, 15, 13, 11, 10, 9, 7, 2, 0]

存在 元素 7 索引为: 7

查找次数:2

顺序: 17-->15

这里给出 7 的源码/**

* Project Interview

* Package   com.java.interview.algorithm.sort

* FileName  QuickBinarySearch.java

* Description TODO

* CompanyITSer LTD.

* Copyright 2014

* All rights Reserved, Designed By ITSer

*

* @author Dev-Wangxin

* @version V1.0

* Createdate 2014年8月24日 下午2:09:59

*

* Modification  History

* Date          Author              Version        Description

* -----------------------------------------------------------------------------------

* 2014年8月24日       Wangxin          1.0             1.0

* Why & What is modified

*/

package com.java.interview.algorithm.sort;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Comparator;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

/**

*

* ClassName QuickBinarySearch

* Description TODO

*

* @author Dev-Wangxin

* @date 2014年8月24日 下午3:53:45

*

*/

public class QuickBinarySearch {

// Recording the process

private static ArrayList log;

/**

*

* MethodName: main

* Description: TODO

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午3:53:45

* Modification with annotation or not

*

* @author Wangxin

* @param args

* @return void

*/

public static void main(String[] args) {

// create_Array(1, 9, 4, "desc");

// create_Array(-3, 6, 4, "asc");

// create_Array(-3, 6, 4, "");

// create_Array(-3, 6, 4, null);

Integer k1 = 10;

Integer k2 = 8;

List l1 = create_Array(0, 100, k1, null);

List l2 = create_Array(0, 20, k1, "desc");

position(l1, 10, 0, k1);

position(l2, 7, null, null);

}

/**

* MethodName: position

* Description: 输出key所在位置和查找顺序

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午4:15:16

* Modification with annotation or not

*

* @param create_Array

* @param i

* @return void

*/

private static void position(List list, int key, Integer start,

Integer end) {

if (start == null) {

start = 0;

}

if (end == null) {

end = list.size() - 1;

}

if (start > end || end > list.size() || start 

System.out.println("Error Paramerers!");

System.exit(0);

}

int pos = find_By_BinarySearch(list, key, start, end);

if (pos != -1) {

System.out.println("\n" + list + " \n 存在 元素 " + key + " 索引为: "

+ pos);

System.out.print("查找次数:" + log.size() + "\n顺序: ");

for (int i = 0; i 

System.out.print(list.get(i) + "-->");

}

System.out.println(list.get(log.size()));

} else {

System.out.println("\n" + list + " \n 不存在 元素 " + key);

System.out.print("查找次数:" + log.size() + "\n顺序: ");

for (int i = 0; i 

System.out.print(list.get(i) + "-->");

}

System.out.println(list.get(log.size()));

}

}

/**

*

* MethodName: create_Array

* Description: 创建有序数组或list,逆序或正序

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午4:20:24

* Modification with annotation or not

*

* @param low

*            下区间

* @param high

*            上区间

* @param size

*            生成数的个数

* @param sort_detatile

*            升降序描述

* @return List

*/

private static List create_Array(int low, int high, int size,

String sort_detatile) {

// size 为 0 ,或区间不满足要求,则终止运行

if (size <= 0 || (high - low) 

System.out.println("error input!");

return null;

}

Set hashSet = new HashSet(size);

// 用 set 去重

while (hashSet.size() 

// (int) Math.random() * (high - low + 1) == 0

// hashSet.add((int) (Math.random() * (high - low) + low));

// lose low value if cotains negative

hashSet.add((int) (Math.random() * (high - low)) + low);

}

// 转化为数组

Object[] temp = hashSet.toArray();

// 用集合的比较器Comparator实现定制排序

if ("desc".equalsIgnoreCase(sort_detatile)) {

Arrays.sort(temp, new Comparator() {

@Override

public int compare(Object o1, Object o2) {

// TODO Auto-generated method stub

return (Integer) o2 - (Integer) o1;

}

});

} else if ("asc".equalsIgnoreCase(sort_detatile)

|| "".equals(sort_detatile) || sort_detatile == null) {

Arrays.sort(temp);// default asc

} else {

System.out.println("error sort details");

return null;

}

// 将排序后的数组转化为list输出

List list = new ArrayList(size);

for (Object object : temp) {

list.add((Integer) object);

}

// 销毁temp的引用

temp = null;

return list;

}

/**

* MethodName: find_By_BinarySearch

* Description: 調用查找方法

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午4:00:06

* Modification with annotation or not

*

* @param list

* @return int

*/

private static int find_By_BinarySearch(List list, int key,

Integer low, Integer high) {

log = new ArrayList(0);

// if (low == null) {

// low = 0;

// }

// if (high == null) {

// high = list.size();

// }

if (list.size() >= 2 && list.get(0) > list.get(1)) {

return binarySearch_DESC(list, key, low, high);

} else {

return binarySearch(list, key, low, high);

}

}

/**

*

* MethodName: binarySearch

* Description: 二分法之正序list查找

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午4:37:22

* Modification with annotation or not

*

* @param list

* @param key

* @param low

* @param high

* @return int

*/

private static int binarySearch(List list, int key, int low,

int high) {

if (low > high)

return -1;

int mid = (low + high) / 2;

log.add(list.get(mid));

if (list.get(mid) == key) {

return mid;

} else if (list.get(mid) 

return binarySearch(list, key, mid + 1, high);

} else {

return binarySearch(list, key, low, mid - 1);

}

}

/**

*

* MethodName: binarySearch_DESC

* Description: 二分法之逆序list查找

* Create_By: Wangxin

* Create_Date: 2014年8月24日 下午6:18:16

* Modification with annotation or not

*

* @param list

* @param key

* @param low

* @param high

* @return int

*/

private static int binarySearch_DESC(List list, int key, int low,

int high) {

if (low > high)

return -1;

int mid = (low + high) / 2;

log.add(list.get(mid));

if (list.get(mid) == key) {

return mid;

} else if (list.get(mid) 

return binarySearch_DESC(list, key, low, mid - 1);

} else {

return binarySearch_DESC(list, key, mid + 1, high);

}

}

}output:

[0, 4, 7, 8, 17, 22, 23, 27, 50, 62]

不存在 元素 10

查找次数:4

顺序: 0-->4-->7-->17

[19, 18, 17, 16, 15, 14, 10, 9, 4, 2]

不存在 元素 7

查找次数:3

顺序: 19-->18-->16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值