一、冒泡排序:
public static void main(String[] args) { int[] li ={1,4,5,2,33,22,33}; System.out.println(li); for (int i = 0; i < li.length-1; i++) { for (int j = 0; j < li.length-i-1; j++) { if (li[j]>li[j+1]){ int swap = li[j]; li[j] = li[j+1]; li[j] = swap; } } } for (int i:li) { System.out.println(i); } }
二、快速排序:
public class SortedTest2 { public static void Sort(int[] a,int low,int height){ int i=low; int j=height; if (i>j){ return; } int k=a[i]; while (i<j){ while (i<j && a[j]>k){ j--; } while (i<j&&a[i]<=k){ i++; } if(i<j){ int swap = a[i]; a[i]=a[j]; a[j]=swap; } } k=a[i]; a[i]=a[low]; a[low]=k; Sort(a,low,i-1); Sort(a,i+1,height); } public static void main(String[] args) { int[] arr = {4,5,3,55,22,44,77}; Sort(arr,0,arr.length-1); for (int i:arr) { System.out.println(i); } } }
三、判断字符串是否对称
public static Boolean test(String s){ for(int i=0;i<=s.length()/2;i++){ if(s.charAt(i) != s.charAt(s.length()-i-1){ return false; } } return true; }