选择、冒泡排序练习

master
libospinach 2 years ago
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: 20220824 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: 20220824 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: 20220824 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: 20220824 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: 20220824 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();
}
}
Loading…
Cancel
Save