排序算法之冒泡排序,选择排序

    xiaoxiao2026-04-20  4

    public class Sort { public static void main(String[] args) { int[] arr = { 24, 69, 80, 57, 13 }; bubbleSort(arr); selectSort(arr); for (int s : arr) { System.out.print(s + "/"); } } //冒泡排序 public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int a = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = a; } } } } //选择排序 public static void selectSort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { for (int j = i+1; j < arr.length; j++) { if (arr[i] > arr[j]) { int a = arr[i]; arr[i] = arr[j]; arr[j] = a; } } } } }

     

    转载请注明原文地址: https://ju.6miu.com/read-1309027.html
    最新回复(0)