Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串排序数组_Java_Arrays_Algorithm_Sorting - Fatal编程技术网

Java 字符串排序数组

Java 字符串排序数组,java,arrays,algorithm,sorting,Java,Arrays,Algorithm,Sorting,所以我尝试用一种算法对字符串数组进行排序 注意:对于此分配,我不允许使用任何内置排序函数 public boolean insert(String s) { boolean result = false; int index = 0; int k = 0; String temp = ""; if (numUsed < values.length) { if (index == 0) { values[index] = s;

所以我尝试用一种算法对字符串数组进行排序

注意:对于此分配,我不允许使用任何内置排序函数

public boolean insert(String s)
{
  boolean result = false;
  int index = 0;
  int k = 0;
  String temp = "";

  if (numUsed < values.length)
  {
    if (index == 0) 
    {
      values[index] = s;
    }    
    else 
    {
      index = 0;
      while (values[index].compareTo(s) < 0);
      k = index;
      while (k < numUsed)
      {
        values[k + 1] = values[k];
      }
      values[index] = s;
    }
    numUsed++;
    result = true;
  }
}
公共布尔插入(字符串s)
{
布尔结果=假;
int指数=0;
int k=0;
字符串temp=“”;
如果(numUsed
给定“苹果”、“猫”和“蜜蜂”的输入,输出的顺序与输入的顺序相同。无论我做什么,它似乎永远不会排序


有人能帮我找到问题吗?

从基础开始。假设你面前有一大堆随机排序的索引卡,你需要按字母顺序排列它们。一个简单但可行的方法是首先找到所有以A开头的单词,然后将它们放在一个单独的堆中(提示:如何在程序中模拟单独的堆?)。然后遍历第一堆,找到所有以B开头的单词,并将它们放在排序后的一堆中(另一个提示)

我建议将当前代码放在一边,重新开始。编写一个循环,遍历未排序的数组,并查找最接近字典开头的单词。以下是一些psuedocode,让您开始使用:

String leastSoFar = "ZZZZZZ";
for each String in values:
    compare the string to leastSoFar
    if the string is closer to the start of the dictionary than leastSoFar:
        leastSoFar = ???? //I'll let you fill those in

对集合进行排序和向集合中添加元素通常是两个独立的功能。当然,可以将元素添加到集合中,以便对元素进行排序。。。但这与简单地对元素数组进行排序是完全不同的任务

如果您只是想实现一个简单的排序算法,那么一个简单的(但不是最优的)易于编码的算法就是“延迟替换排序”。伪码fpr按升序排序的算法如下所述:

 Begin DELAYEDSORT
     For ITEM=1 to maximum number of items in list-1
        LOWEST=ITEM
        For N=ITEM+1 to maximum number of items in list
           Is entry at position N lower than entry at position LOWEST?
              If so, LOWEST=N
        Next N
        Is ITEM different from LOWEST
           If so, swap entry at LOWEST with entry in ITEM
     Next ITEM
  End DELAYEDSORT
延迟替换排序算法简单易懂,易于编码。它通常比冒泡排序更快(交换次数更少),但时间复杂度O(n^2)仍然很差,因此不适合对非常大的数据集进行排序

如果确实要将项目添加到已排序的集合中,则可以将新项目添加到集合的末尾,并使用上面的方法执行此操作。如果您使用的数据集大于几百或几千个元素,那么效率将很低

另一种仍然具有O(n^2)时间复杂度,但可用于组合加法和排序的解决方案是“插入排序”,其伪代码如下所示:

// The values in A[i] are checked in-order, starting at the second one
for i ← 1 to i ← length(A)
  {
    // at the start of the iteration, A[0..i-1] are in sorted order
    // this iteration will insert A[i] into that sorted order
    // save A[i], the value that will be inserted into the array on this iteration
    valueToInsert ← A[i]
    // now mark position i as the hole; A[i]=A[holePos] is now empty
    holePos ← i
    // keep moving the hole down until the valueToInsert is larger than 
    // what's just below the hole or the hole has reached the beginning of the array
    while holePos > 0 and valueToInsert < A[holePos - 1]
      { //value to insert doesn't belong where the hole currently is, so shift 
        A[holePos] ← A[holePos - 1] //shift the larger value up
        holePos ← holePos - 1       //move the hole position down
      }
    // hole is in the right position, so put valueToInsert into the hole
    A[holePos] ← valueToInsert
    // A[0..i] are now in sorted order
  }
//按顺序检查[i]中的值,从第二个值开始
因为我← 一对一← 长度(A)
{
//在迭代开始时,[0..i-1]按排序顺序排列
//此迭代将在排序顺序中插入[i]
//保存一个[i],该值将在此迭代中插入数组
值插入← A[i]
//现在将位置i标记为孔;A[i]=A[holePos]现在为空
霍雷普斯← 我
//继续向下移动孔,直到值ToInsert大于
//孔的正下方或孔已到达阵列的开头
而holePos>0且valueToInsert
您的代码有很多问题。尝试更干净的方法。特别是尝试将添加值与排序值分开,因为这是两件事(或者您想以排序方式添加它们?)。作为旁注:您的第一个while循环将导致一个无止境的循环,在该行的末尾使用“;”进行循环。请使用loog进行排序算法抱歉,我没有指定。我确实需要按排序顺序输入值。我的代码在IDE中的格式要好得多,但我一粘贴到这里就忘了重新格式化。我修正了“;”,现在我得到了蜜蜂,零,零。有没有办法解决这个问题?关闭IDE中的选项卡。显示问题似乎是由于代码中的制表符和空格的邪恶混合。请给我们,而不仅仅是一个函数。