小铭的学习日记2022.8.3

十进制转二进制

for(int i = 31;i >=0; i--){
	System.out.print((num & (1 << 31)) == 0 ? "0" : "1");
}

求1!+2!+3!+…+N!
方法1

public static long functionA(int n){
	long sum = 0;
	for(int i = 1; i <= n; i++){
		sum += functionA1(i);
	}
	return sum;
}
public static long functionA1(int n){
	long ans = 1;
	for(int i = 1;i <= n;i++){
		ans *= i
	}
	return ans;
}

方法2

public static long functionB(int n){
	long sum = 0;
	long ans = 1;
	for(int i = 1; i <= n; i++){
		ans *= i;
		sum += ans;
	}
}

选择排序

public static void selectionSort(int[] arr){
	if(arr == null || arr.length < 2){
		return;
	}
	int n = arr.length;
	for(int i = 0; i < n-1; i++){
		int minValueIndex = i;
		for(j = i+1; j < n; j++){
			minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
		}
		int temp = arr[j];
		arr[j] = arr[minValueIndex];
		arr[minValueIndex] = temp;
	}
}

冒泡排序

public static void bubbleSort(int[] arr){
	if(arr = null || arr.length < 2){
		return;
	}
	int n = arr.length;
	for(int end = n-1; end >= 0; end--){
		for(int second = 1; second <= end; second++){
			if(arr[second] < arr[second - 1]){
				int temp = arr[second];
				arr[second] = arr[second - 1];
				arr[second - 1] = temp;
			}
		}
	}
}

插入排序

public static void insertSort(int[] arr){
	if(arr == null || arr.length < 2){
	return;
	}
	int n = arr.length;
	for(int end = 1; end < n; end++){
		int newValueIndex = end;
		while(newValueIndex - 1 >= 0 && arr[newVlueIndex - 1] > arr[newVlueIndex]){
			int temp = arr[newVlueIndex];
			arr[newVlueIndex] = arr[newVlueIndex - 1]
			arr[newVlueIndex - 1] = temp;
			newValueIndex--;
		}
	}
}

插入排序优化

public static void insertSort(int[] arr){
	if(arr == null || arr.length < 2){
	return;
	}
	int n = arr.length;
	for(int end = 1; end < n; end++){
		for(int pre = end -1; pre - 1 >= 0 && arr[pre -1] > arr[pre]; pre++)
			int temp = arr[pre];
			arr[pre] = arr[pre - 1]
			arr[pre - 1] = temp;
	}
}

思路看懂了但是写不出来代码是什么原因???

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值