1、冒泡排序
static void bubble_sort(int[] str) { for (int i = 0; i < str.length; i++) { for (int j = i; j < str.length; j++) { if (str[i] > str[j]) { int temp = str[i]; str[i] = str[j]; str[j] = temp; } } } }
2、交叉排序
public static void quickSort(int[] A, int start, int end) { if (start < end) { int pivotLocation = partition(A, start, end); quickSort(A, start, pivotLocation - 1); quickSort(A, pivotLocation + 1, end); } } private static int partition(int[] A, int start, int end) { int pivot = A[end]; int i = start; for (int j = start; j < end; ++j) { if (A[j] <= pivot) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; ++i; } } A[end] = A[i]; A[i] = pivot; return i; }
3、调整数据奇数位于偶数前面(冒泡原理)
public static void reOrderArray(int [] array) {
int temp;
for(int i=0;i<array.length; i++){
for(int j=0; j<array.length-1; j++){
if(array[j]%2==0 && array[j+1]%2==1){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
}
4、获取最长回文字符串
public static String longestPalindrome1(String s) {
int maxPalinLength = 0;
String longestPalindrome = null;
int length = s.length();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
int len = j - i;
String curr = s.substring(i, j + 1);
if (isPalindrome(curr)) {
if (len > maxPalinLength) {
longestPalindrome = curr;
maxPalinLength = len;
}
}
}
}
return longestPalindrome;
}
public static boolean isPalindrome(String s) {
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
5、快速排序
public static void quickSort(int[] A, int start, int end) { if (start < end) { int pivotLocation = partition(A, start, end); quickSort(A, start, pivotLocation - 1); quickSort(A, pivotLocation + 1, end); } } private static int partition(int[] A, int start, int end) { int pivot = A[end]; int i = start; for (int j = start; j < end; ++j) { if (A[j] <= pivot) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; ++i; } } A[end] = A[i]; A[i] = pivot; return i; }