The ascending and descending order of bubble sorting and selection sorting and the ascending and descending order of the specified position in the Java array

1. Code and renderings

1. Ascending order of bubble sort

Case: Use bubble sort to ascend the following array {12,36,5,1,98,55,78}

The code is as follows (example):

public class Work1 {
    
    
private static void main(String[] args) {
    
    
    int arr[]={
    
    12,36,5,1,98,55,78};
        for (int i = 1; i <=arr.length-1 ; i++) {
    
    
            for (int j = 0; j <=arr.length-1-i ; j++) {
    
    
                if (arr[j]>arr[j+1]){
    
    
                    int t=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=t;
                }
            }
        }
            System.out.println(Arrays.toString(arr));
    }
}

2. Descending order of bubble sort

Case: Using bubble sort to descend the following array {12,36,5,1,98,55,78} The
code is as follows (example):

public class Work1 {
    
    
private static void main(String[] args) {
    
    
     int arr[]={
    
    12,36,5,1,98,55,78};
        for (int i = 1; i <=arr.length-1 ; i++) {
    
    
            for (int j = 0; j <=arr.length-1-i ; j++) {
    
    
                if (arr[j]<arr[j+1]){
    
    
                    int t=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=t;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

3. Select the ascending order of sorting

Case: Use selection sort to ascend the following array {12,36,5,1,98,55,78}

The code is as follows (example):

public class Work1 {
    
    
private static void main(String[] args) {
    
    
       int arr[]={
    
    12,36,5,1,98,55,78};
        for (int i = 0; i <= arr.length-1; i++) {
    
    
            int minindex=i;
            for (int j = i; j <=arr.length-1; j++) {
    
    
                if (arr[j]<arr[minindex]){
    
    
                    minindex=j;
                }
            }
            int t=arr[i];
            arr[i]=arr[minindex];
            arr[minindex]=t;
        }
        System.out.println(Arrays.toString(arr));
    }
}

4. Select the descending order of sorting

Case: Use selection sort to descend the following array {12,36,5,1,98,55,78}

The code is as follows (example):

public class Work1 {
    
    
private static void main(String[] args) {
    
    
    int arr[]={
    
    12,36,5,1,98,55,78};
        for (int i = 0; i <arr.length-1 ; i++) {
    
    
            int maxIndex=i;
            for (int j = i; j <=arr.length-1 ; j++) {
    
    
                if (arr[j]>arr[maxIndex]){
    
    
                    maxIndex=j;
                }
            }
            int t=arr[i];
            arr[i]=arr[maxIndex];
            arr[maxIndex]=t;
        }
        System.out.println(Arrays.toString(arr));
    }
}

5. The ascending and descending order of the specified position of the bubble sort

The code is as follows (example):

package work;

import java.util.Arrays;
import java.util.Scanner;
public class Work02 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数你指定位置的前后升序和降序(例如输入5:1-5升序,6-10降序):");
        int number=sc.nextInt();
        int arr[]={
    
    7,9,4,3,7,50,40,95,97,35};
        for (int i = 1; i <=arr.length-1 ; i++) {
    
    
            for (int j = 0; j <=arr.length-1-i ; j++) {
    
    
                if (j<=number-2) {
    
    
                    if (arr[j]>arr[j+1]){
    
    
                        int t=arr[j];
                        arr[j]=arr[j+1];
                        arr[j+1]=t;
                    }
                }else if(j>=number){
    
    
                    if (arr[j]<arr[j+1]){
    
    
                        int t=arr[j];
                        arr[j]=arr[j+1];
                        arr[j+1]=t;
                    }
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}

Insert picture description here

to sum up

The above is the whole content of ascending and descending order of bubble sorting and selection sorting, mainly using related knowledge such as loops, arrays, and data exchange.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/110678202