• 二分查找2


     1 package com.wh.ObjectHomeWork;
     2 
     3 import java.util.Arrays;
     4 import java.util.Scanner;
     5 
     6 public class CharArray {
     7     private char[] words;
     8 
     9     public CharArray(char[] words) { // 构造方法
    10         this.words = words;
    11     }
    12 
    13     public void sort() { // 对数组进行冒泡排序
    14         for (int i = 0; i < words.length - 1; i++) {
    15             for (int j = 0; j < words.length - 1 - i; j++) {
    16                 if (words[j] > words[j + 1]) {
    17                     char t = words[j];
    18                     words[j] = words[j + 1];
    19                     words[j + 1] = t;
    20                 }
    21             }
    22         }
    23     }
    24 
    25     public int query(char key) {
    26         int index = 0;
    27         for (int i = 0; i < words.length; i++) {
    28             if (words[i] == key) {
    29                 index = i;
    30                 break;
    31             }
    32             if ((i == words.length - 1) && (words[i] != key)) {
    33                 index = -1;
    34             }
    35         }
    36         return index;
    37     }
    38 
    39     public int search(char key) {
    40         int start = 0, ends = words.length - 1;
    41         int middle;
    42         int index = 0;
    43         for (;;) {
    44             middle = (ends + start) / 2 + (ends + start) % 2;
    45             if (words[middle] == key) {
    46                 index = middle;
    47                 break;
    48             } else if (key < words[middle] && key >= words[start]) {
    49                 ends = middle;
    50             } else if (key > words[middle] && key <= words[ends]) {
    51                 start = middle;
    52             } else {
    53                 index = -1;
    54                 break;
    55             }
    56         }
    57         return index;
    58     }
    59 
    60     public static void main(String[] args) {
    61         Scanner sc = new Scanner(System.in);
    62         System.out.println("请随机输入一个数字:");
    63         int str;
    64         char num;
    65         for (;;) {
    66             str = sc.nextInt();
    67             if (str > 0 && str < 65535) {
    68                 num = (char) str;
    69                 break;
    70             } else {
    71                 System.out.println("请重新输入一个数字:");
    72             }
    73         }
    74         System.out.println("要查找的字符是:" + num);
    75         char[] arr = "qwertyuiopasdfghjklzxcvbnm".toCharArray();
    76         CharArray c1 = new CharArray(arr);
    77         int index2 = c1.query(num);
    78         System.out.println("排序前的索引是:" + index2);
    79         System.out.println("排序前:" + Arrays.toString(arr));
    80 
    81         c1.sort();
    82         System.out.println("排序后:" + Arrays.toString(arr));
    83         int index = c1.search(num);
    84         System.out.println("排序后的索引是" + index);
    85         sc.close();
    86     }
    87 }
  • 相关阅读:
    四、创建多线程、数据共享
    operator函数操作符
    三、线程传参
    二、线程创建、结束
    一、并发、进程、线程概念
    bagging和boosting的区别
    ID3,C4.5和CART三种决策树的区别
    7创建型模式之建造者模式
    6创建型模式之工厂模式与抽象工厂模式
    5创建型模式之简单工厂模式
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/5886656.html
Copyright © 2020-2023  润新知