1. //数组线性查找
    2. String[] arr = new String[]{"JJ","DD","MM","BB","GG","AA"};
    3. String dest = "BB";
    4. boolean isFlag = true;
    5. for(int i = 0;i < arr.length;i++){
    6. if(dest.equals(arr[i])){
    7. sout("找到了元素");
    8. isFlag = false;
    9. break;
    10. }
    11. }
    12. if(isFlag){
    13. sout("没找到")
    14. }
    1. //二分法查找
    2. //前提所要查找的数组必须有序
    3. int[] arr = new int[]{-12,-1,0,2,4,8,9,12,45,78,153,952};
    4. int dest = 4;
    5. int head = 0;//初始的首索引
    6. int end = arr.length - 1;//初始的末索引
    7. boolean isFlag = true;
    8. while(head <= end){
    9. int middle = (head + end) / 2;
    10. if(dest == arr[middle]){
    11. sout("找到了指定元素");
    12. isFlag = false;
    13. break;
    14. }else if(arr[middle] > dest){
    15. end = middle -1;
    16. }else{
    17. head = middle + 1;
    18. }
    19. }
    1. //冒泡排序
    2. int[] arr = new int[]{56,12,45,32,6545,213,45,123,45,20};
    3. for(int i = 0;i < arr.length - 1,i++){
    4. for(int j = 0;j < arr.length - 1 - i;j++){
    5. if(arr[j] > arr[j + 1]){
    6. int temp = arr[j];
    7. arr[j] = arr[j + 1];
    8. arr[j + 1] = temp;
    9. }
    10. }
    11. }
    1. //快速排序
    2. public class QuickSort {
    3. private static void swap(int[] data, int i, int j) {
    4. int temp = data[i];
    5. data[i] = data[j];
    6. data[j] = temp;
    7. }
    8. private static void subSort(int[] data, int start, int end) {
    9. if (start < end) {
    10. int base = data[start];
    11. int low = start;
    12. int high = end + 1;
    13. while (true) {
    14. while (low < end && data[++low] - base <= 0)
    15. ;
    16. while (high > start && data[--high] - base >= 0)
    17. ;
    18. if (low < high) {
    19. swap(data, low, high);
    20. } else {
    21. break;
    22. }
    23. }
    24. swap(data, start, high);
    25. subSort(data, start, high - 1);//递归调用
    26. subSort(data, high + 1, end);
    27. }
    28. }
    29. public static void quickSort(int[] data){
    30. subSort(data,0,data.length-1);
    31. }
    32. public static void main(String[] args) {
    33. int[] data = { 9, -16, 30, 23, -30, -49, 25, 21, 30 };
    34. System.out.println("排序之前:\n" + java.util.Arrays.toString(data));
    35. quickSort(data);
    36. System.out.println("排序之后:\n" + java.util.Arrays.toString(data));
    37. }
    38. }