parent
836de8f633
commit
38a51bdcfa
@ -1,4 +1,4 @@
|
||||
package com.never;
|
||||
package com.never.basic;
|
||||
|
||||
public class BitOperation {
|
||||
public static void main(String[] args) {
|
@ -0,0 +1,21 @@
|
||||
package com.never.basic;
|
||||
|
||||
/**
|
||||
* @Description: 冒泡排序
|
||||
* @author: Bo Li
|
||||
* @date: 2022年08月24日 10:19
|
||||
*/
|
||||
public class BubbleSort {
|
||||
public void bubbleSort(int[] arr){
|
||||
if(arr == null || arr.length <2){
|
||||
return;
|
||||
}
|
||||
for(int i = 0 ; i<arr.length ; i++){
|
||||
for(int j = arr.length-1 ;j > i;j--){
|
||||
if(arr[j]<arr[j-1]){
|
||||
CommonUtil.swapIndex(arr,j,j-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.never.basic;
|
||||
|
||||
/**
|
||||
* @Description: 工具类
|
||||
* @author: Bo Li
|
||||
* @date: 2022年08月24日 10:14
|
||||
*/
|
||||
public class CommonUtil {
|
||||
public static void swapIndex(int[] arr , int i, int j){
|
||||
int tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.never.basic;
|
||||
|
||||
/**
|
||||
* @Description: 递归实现N阶乘相加
|
||||
* @author: Bo Li
|
||||
* @date: 2022年08月24日 10:32
|
||||
*/
|
||||
public class NRecursion {
|
||||
public static void main(String[] args) {
|
||||
int num = nRecursion(3);
|
||||
System.out.println(num);
|
||||
}
|
||||
private static int nRecursion(int n ){
|
||||
int sum = 0;
|
||||
int step = 1;
|
||||
for(int i = 1 ;i <= n; i++){
|
||||
step *= i;
|
||||
sum += step;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.never.basic;
|
||||
|
||||
/**
|
||||
* @Description: 选择排序
|
||||
* @author: Bo Li
|
||||
* @date: 2022年08月24日 10:13
|
||||
*/
|
||||
public class SelectSort {
|
||||
public void selectSort(int[] arr){
|
||||
if(arr == null || arr.length<2){
|
||||
return;
|
||||
}
|
||||
for(int i = 0;i < arr.length; i++){
|
||||
int minIndex = i;
|
||||
for(int j = i+1; j<arr.length ; j++){
|
||||
minIndex = arr[j]<arr[minIndex]?j:minIndex;
|
||||
}
|
||||
CommonUtil.swapIndex(arr,minIndex,i);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.never.basic;
|
||||
|
||||
/**
|
||||
* @Description: 排序验证入口
|
||||
* @author: Bo Li
|
||||
* @date: 2022年08月24日 10:01
|
||||
*/
|
||||
public class SortDemo {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {11,2,5,3,6,13,7};
|
||||
printArr(arr);
|
||||
// new SelectSort().selectSort(arr);
|
||||
new BubbleSort().bubbleSort(arr);
|
||||
printArr(arr);
|
||||
}
|
||||
|
||||
private static void printArr(int[] arr){
|
||||
for(int i = 0; i<arr.length;i++){
|
||||
System.out.print(arr[i]+" ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue