选择排序+冒泡排序
Posted linding
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选择排序+冒泡排序相关的知识,希望对你有一定的参考价值。
import java.util.Arrays; public class Arrsort { public static void main(String[] args) { int[] arr = {5, 8, 3, 4, 9, 2, 7, 6, 1}; System.out.println(Arrays.toString(bubblesort(arr))); System.out.println(Arrays.toString(selectsort(arr))); System.out.println(Arrays.toString(arr)); } private static int[] bubblesort(int[] arr) { int[] newarr = new int[arr.length]; System.arraycopy(arr, 0, newarr, 0, arr.length); for (int i = newarr.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (newarr[j] > newarr[j + 1]) { int temp = newarr[j]; newarr[j] = newarr[j + 1]; newarr[j + 1] = temp; } } } return newarr; } private static int[] selectsort(int[] arr) { int[] newarr = new int[arr.length]; System.arraycopy(arr, 0, newarr, 0, arr.length); for (int i = 0; i < newarr.length; i++) { for (int j = i + 1; j < newarr.length; j++) { if (newarr[i] < newarr[j]) { int temp = newarr[i]; newarr[i] = newarr[j]; newarr[j] = temp; } } } return newarr; } }
以上是关于选择排序+冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章