2020-12-07

冒泡排序

public static void main(String[] args) {
		int n = 0;
		// 冒泡排序
		// 随机产生10个0~100之间的随机数添加到数组中
		int[] ns = new int[10];
//		int[] ns = {99, 90, 84, 1, 62, 33, 78, 48, 8, 67};

		for (int i = 0; i < ns.length; i++) {
			ns[i] = (int) (Math.random() * 100);
		}

		System.out.println(Arrays.toString(ns));

		// 冒泡排序
		for (int j = 0; j < ns.length - 1; j++) {
			for (int i = 0; i < ns.length - 1 - j; i++) {
				if (ns[i] > ns[i + 1]) {
					ns[i] = ns[i] ^ ns[i + 1];
					ns[i + 1] = ns[i] ^ ns[i + 1];
					ns[i] = ns[i] ^ ns[i + 1];
					n++;
				}
			}
		}

		System.out.println(Arrays.toString(ns) + n);
	}

选择排序

public static void main(String[] args) {
		// 选择排序
		// 随机产生10个0~100之间的随机数添加到数组中
		int[] ns = new int[10];
//		int[] ns = { 99, 90, 84, 1, 62, 33, 78, 48, 8, 67 };

		for (int i = 0; i < ns.length; i++) {
			ns[i] = (int) (Math.random() * 100);
		}
		System.out.println(Arrays.toString(ns));
		for (int j = 0; j < ns.length - 1; j++) {
			// 假设第一个为最小值,记录最小值的下标
			int minIndex = j;
			// 验证最小值
			for (int i = j + 1; i < ns.length; i++) {
				if (ns[minIndex] > ns[i]) {
					minIndex = i;
				}
			}
			if (minIndex != j) {
				ns[minIndex] = ns[minIndex] ^ ns[j];
				ns[j] = ns[minIndex] ^ ns[j];
				ns[minIndex] = ns[minIndex] ^ ns[j];
			}
		}

		System.out.println(Arrays.toString(ns));
	}

快速排序

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ns = new int[10];
		for (int i = 0; i < ns.length; i++) {
			ns[i] = (int) (Math.random() * 100);
		}

		System.out.println(Arrays.toString(ns));
		quickSort(ns);
		System.out.println(Arrays.toString(ns));
	}

	public static void quickSort(int[] values) {
		quickSort(values, 0, values.length - 1);
	}

	private static void quickSort(int[] values, int sta, int end) {
		// 假设第一个是中间值,记录中间值的下标
//		int number = values[0];
		// 定义交换坐标
		int i = sta;
		int j = end;

		while (i < j) {
			while (i < j) {
				if (values[j] < values[i]) {
					values[i] = values[i] ^ values[j];
					values[j] = values[i] ^ values[j];
					values[i] = values[i] ^ values[j];
					break;
				} else {
					j--;
				}
			}

			while (i < j) {
				if (values[i] > values[j]) {
					values[i] = values[i] ^ values[j];
					values[j] = values[i] ^ values[j];
					values[i] = values[i] ^ values[j];
					break;
				} else {
					i++;
				}
			}
		}

		if (i - sta > 1) {
			quickSort(values, sta, i - 1);
		}

		if (end - i > 1) {
			quickSort(values, i + 1, end);
		}
	}

常用类

String, Math, Random

// StringBuffer
		StringBuffer str1 = new StringBuffer();
		str1.append("ads123");
		str1.append("456");
		str1.append("fgh");
		str1.reverse();

		System.out.println(str1);
		str1.delete(2, str1.length());
		System.out.println(str1);
//		String
		String str = new String();
		str = "Hello World";
		String str2 = "hello world";
		String str3 = "  Hello world   Hello world   ";
//		根据下标取得字符串中字符
		System.out.println(str.codePointAt(3));
		System.out.println(str.charAt(3));
		for (int i = 0; i < str.length(); i++) {
			System.out.print(str.charAt(i));
		}
//		equals:比较字符串内容是否相等  equalsIgnoreCase:比较但不区分大小写
		System.out.println(str.equals(str2));
		System.out.println(str.equalsIgnoreCase(str2));
//		 得到字符串的长度
		System.out.println(str.length());
//		去掉前后的空格
		System.out.println(str3);
		System.out.println(str3.trim());
//		判断字符串是否以某一个字符串结尾
		System.out.println(str.endsWith("ld"));
		System.out.println(str.startsWith("he"));
//		判断字符串首次出现的位置
		System.out.println(str3.indexOf("ll"));
		System.out.println(str3.lastIndexOf("ll"));
//		字符串截取
		System.out.println(str3.substring(5));
//		从下标6截取到小标11,包含起始6,不包含结束下标11
		System.out.println(str3.substring(6, 11));
		String str4 = "12,22,33,44,52,63,72,83";
//		切割
		String[] str5 = str4.split(",");
		for (String s : str5) {
			System.out.print(s);
//		字符串替换
			System.out.println(str3.replace("wo", "cc"));
			/*
			 * 比较两个字符串的大小 -如果字符串相等则返回0 -从前向后依次比较字符编码的差值。只到比较到不同的为止。 -如果字符串前面全部相同,就比较字符串的长度差值
			 */
			String str6 = "abcABC";
			String str7 = "abcb2";
			System.out.println(str6.compareTo(str7));
// 把字符串转为小写
			System.out.println(str.toLowerCase());
// 把字符串转为大写
			System.out.println(str.toUpperCase());



System.out.println(Math.E);
		System.out.println(Math.PI);
		
		System.out.println(Math.sin(Math.PI/3));
		// a的b次方
		System.out.println(Math.pow(4, 2));
		// 四舍五入
		System.out.println(Math.round(1.5));
		// 开平凡
		System.out.println(Math.sqrt(2));




Random random = new Random();

		System.out.println(random.nextBoolean());

		System.out.println(random.nextInt());

//		for (int i = 0; i < 100; i++) {
//			System.out.println(random.nextInt(5) + 10);
//		}
		
		System.out.println(random.nextFloat());
		
		// 得到当前的时间
		System.out.println(System.currentTimeMillis());
		
//		// 强制关机。强制结束当前进程(程序)。
//		System.exit(-1);
//		
//		System.err.println("错误");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值