19.1.16 Java数组中的选择法排序和冒泡法排序整理

一、选择法排序(当前位和其他位依次比较)

例:

public class Paixu{
	public static void main (String[] args){
		int a[] = {18,62,68,82,65,9};
		arraySort1(a);
	}
public static void arraySort1(int a[]) {
	for (int i=0; i<a.length-1; i++){
		for (int j<i+1; j<a.length; j++) {
			if (a[j]<a[i]) {
			//大于号小于号分别表示从大到小和从小到大排序
				int temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			} 
		}
	}
	for (int i:a) {
		System.out.print(i+" ");
	}
	System.out.println();
}

}
二、冒泡法排序(俩俩比较)
public class Paixu{
	public static void main (String[] args){
		int a[] = {18,62,68,82,65,9};
		arraySort2(a);
	}
public static void arraySort2(int a[]) {
	for (int i=0; i<a.length; i++){
		for (int j=0; j<a.length-i-1; j++) {
			if (a[j]<a[i]) {
			//大于号小于号分别表示从小到大和从大到小排序
				int temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			} 
		}
	}
	for (int i:a) {
		System.out.print(i+" ");
	}
	System.out.println();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下正则表达式来匹配以$d开头,disk结尾的字符串: ```python import re string = "# snmpwalk -v 2c -c zte_public 190.254.0.3:1611.3.6.1.4.1.3902.6053.19.1.3.2.7\ ...\n$da 8:15 0 477G 0 disk\n\ ... ~ # snmpwalk -v 2c -c zte_public 190.254.0.3:161 1.3.6.1.4.1.3902.6053.19.1.3.2.7\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.7.1.0 = Gauge32: 0\ ...\n$db 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.0.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.14.1.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.15.1.0 = Gauge32: 0\ ...\n$dc 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.20.1.0 = Gauge32: 0\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.0.0 = Gauge32: 0\ ...\n$dd 8:15 0 477G 0 disk\n\ ...\nSNMPv2-SMI::enterprises.3902.6053.19.1.3.2.7.1.1.26.1.0 = Gauge32: 0" pattern = r'\$d.*?disk\\n\\s*\\\.\\.\\..*?' result = re.findall(pattern, string, re.DOTALL) print(result) # 输出结果:['$da\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$db\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$dc\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...', '$dd\t\t\t\t\t8:15\t0\t477G\t0\tdisk\n\\ ...'] ``` 解析一下正则表达式: - `\` :转义字符,表示后面的字符是特殊字符,需要转义处理; - `\$d` :匹配以$d开头的字符串; - `.*?` :非贪婪匹配任意字符,直到下一个字符能够匹配成功; - `disk\\n\\s*` :匹配disk和换行符,并且后面可能有多个空白字符; - `\\\.\\\.\\.` :匹配三个连续的点(因为点是特殊字符,需要转义); - `.*?` :非贪婪匹配任意字符,直到下一个字符能够匹配成功; - `.*?` 和 `\\\.\\\.\\.` 重复出现多次,因为要匹配多个符合条件的字符串; - `re.DOTALL` :表示`.`可以匹配任意字符,包括换行符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值