赞
踩
public class maopao { public static void sort(int[] arr){ for (int i = 0; i <arr.length ; i++) { for (int j = 0; j <arr.length-1 ; j++) { if (arr[i] > arr[j]) { int t=arr[i]; arr[i]=arr[j]; arr[j]=t; } } } System.out.println(Arrays.toString(arr)); } public static void main(String[] args) { int arr[]={4,8,'d',1}; sort(arr); }
public class maopao { public static void sort(int[] arr){ public static void main(String[] args) { int[] arr = {26, 5, -96, -9, 89, 78, -6, 1, 3, 2}; for (int i = 0; i < arr.length; i++) { for (int j = arr.length - 1; j >= i + 1; j--) { if (arr[j] < arr[j - 1]) { int t = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = t; } } } for (int a : arr) { System.out.println(a); } } }
``使用异或方式进行排序
(无临时变量)
public static void sort(int[] arr){ for (int i = 0; i <arr.length ; i++) { for (int j = 0; j <arr.length-1 ; j++) { if (arr[i] > arr[j]) { arr[i]^=arr[j]; arr[j]^=arr[i]; arr[i]^=arr[j]; } } } System.out.println(Arrays.toString(arr)); } public static void main(String[] args) { int arr[]={4,8,'-2',1}; sort(arr); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。