【问题标题】:Finding the index of the first word starting with a given alphabet form a alphabetically sorted list查找以给定字母表开头的第一个单词的索引,形成按字母顺序排序的列表
【发布时间】:2014-01-10 16:07:34
【问题描述】:

基于当前的实现,我将得到一个数组列表,其中包含来自某个来源的按字母顺序(A-Z 或 Z-A)排序的大约 1000 个唯一名称。

我需要找到以给定字母开头的第一个单词的索引。

所以更准确地说,当我选择一个字母时,例如。 “M”,它应该给我从排序列表中以“M”开头的单词第一次出现的索引。

这样我应该能够找到从 26 个字母中的每个字母开始的所有第一个单词的索引。

请帮我找到一个不影响速度的解决方案。


更新:

其实在得到1000个唯一名字后,排序也是我的一个逻辑完成的。
如果这可以在进行排序本身时完成,我可以避免在排序后重复列表以查找字母表的索引。

这可能吗?

谢谢,

【问题讨论】:

    标签: sorting search arraylist


    【解决方案1】:

    我希望这段小代码对您有所帮助。我猜这个问题与Java有关,因为你提到了ArrayList。

    String[] unsorted = {"eve", "bob", "adam", "mike", "monica", "Mia", "marta", "pete", "Sandra"};
        ArrayList<String> names = new ArrayList<String>(Arrays.asList(unsorted));
    
        String letter = "M"; // find index of this
    
         class MyComp implements Comparator<String>{
            String first = "";
            String letter;
    
            MyComp(String letter){
                this.letter = letter.toUpperCase();
            }
    
            public String getFirst(){
                return first;
            }
    
            @Override
            public int compare(String s0, String s1) {
    
                if(s0.toUpperCase().startsWith(letter)){
                    if(s0.compareTo(first) == -1 || first.equals("")){
                        first = s0;
                    }
                }
    
                return s0.toUpperCase().compareTo(s1.toUpperCase());
            }
    
        };
    
        MyComp mc = new MyComp(letter);
    
        Collections.sort(names, mc);
    
        int index = names.indexOf(mc.getFirst()); // the index of first name starting with letter
    

    我不确定是否可以在比较器中存储名字的索引而无需太多开销。无论如何,如果您实现自己的排序算法版本,例如快速排序,你应该知道元素的索引,并且可以在排序时计算索引。这取决于您选择的排序算法和实现。事实上,如果我知道你的排序是如何实现的,我们可以插入索引计算。

    【讨论】:

      【解决方案2】:

      所以我想出了自己的解决方案。

      package test.binarySearch;
      
      import java.util.Random;
      
      /**
       *
       * Binary search to find the index of the first starting in an alphabet 
       * 
       * @author Navaneeth Sen <navaneeth.sen@multichoice.co.za>
       */
      class SortedWordArray
      {
      
          private final String[] a;                 // ref to array a
          private int nElems;               // number of data items
      
          public SortedWordArray(int max)          // constructor
          {
              a = new String[max];             // create array
              nElems = 0;
          }
      
          public int size()
          {
              return nElems;
          }
      
          public int find(String searchKey)
          {
              return recFind(searchKey, 0, nElems - 1);
          }
      
          String array = null;
          int arrayIndex = 0;
      
          private int recFind(String searchKey, int lowerBound,
                  int upperBound)
          {
              int curIn;
      
              curIn = (lowerBound + upperBound) / 2;
              if (a[curIn].startsWith(searchKey))
              {
                  array = a[curIn];
                  if ((curIn == 0) || !a[curIn - 1].startsWith(searchKey))
                  {
                      return curIn;              // found it
                  }
                  else
                  {
                      return recFind(searchKey, lowerBound, curIn - 1);
                  }
              }
              else if (lowerBound > upperBound)
              {
                  return -1;             // can't find it
              }
              else                          // divide range
              {
                  if (a[curIn].compareTo(searchKey) < 0)
                  {
                      return recFind(searchKey, curIn + 1, upperBound);
                  }
                  else                       // it's in lower half
                  {
                      return recFind(searchKey, lowerBound, curIn - 1);
                  }
              }  // end else divide range
          }  // end recFind()
      
          public void insert(String value)    // put element into array
          {
              int j;
              for (j = 0; j < nElems; j++)        // find where it goes
              {
                  if (a[j].compareTo(value) > 0)            // (linear search)
                  {
                      break;
                  }
              }
              for (int k = nElems; k > j; k--)    // move bigger ones up
              {
                  a[k] = a[k - 1];
              }
              a[j] = value;                  // insert it
              nElems++;                      // increment size
          }  // end insert()
      
          public void display()             // displays array contents
          {
              for (int j = 0; j < nElems; j++)       // for each element,
              {
                  System.out.print(a[j] + " ");  // display it
              }
              System.out.println("");
          }
      }  // end class OrdArray
      
      class BinarySearchWordApp
      {
      
          static final String AB = "12345aqwertyjklzxcvbnm";
          static Random rnd = new Random();
      
          public static String randomString(int len)
          {
              StringBuilder sb = new StringBuilder(len);
              for (int i = 0; i < len; i++)
              {
                  sb.append(AB.charAt(rnd.nextInt(AB.length())));
              }
              return sb.toString();
          }
      
          public static void main(String[] args)
          {
              int maxSize = 100000;             // array size
              SortedWordArray arr;                  // reference to array
              int[] indices = new int[27];
              arr = new SortedWordArray(maxSize);   // create the array
      
              for (int i = 0; i < 100000; i++)
              {
                  arr.insert(randomString(10));   //insert it into the array
              }
      
              arr.display();                 // display array
              String searchKey;
              for (int i = 97; i < 124; i++)
              {
                  searchKey = (i == 123)?"1":Character.toString((char) i);
      
                  long time_1 = System.currentTimeMillis();
                  int result = arr.find(searchKey);
                  long time_2 = System.currentTimeMillis() - time_1;
                  if (result != -1)
                  {
                      indices[i - 97] = result;
                      System.out.println("Found " + result + "in "+ time_2 +" ms");
                  }
                  else
                  {
                      if (!(i == 97))
                      {
                          indices[i - 97] = indices[i - 97 - 1];
                      }
      
                      System.out.println("Can't find " + searchKey);
                  }
              }
      
              for (int i = 0; i < indices.length; i++)
              {
                  System.out.println("Index [" + i + "][" + (char)(i+97)+"] = " + indices[i]);
              }
          }  // end main()
      
      } 
      

      欢迎所有 cmets。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-15
        • 2012-11-28
        • 2018-07-13
        • 1970-01-01
        • 2013-05-14
        相关资源
        最近更新 更多