当前位置: 首页 > 知识库问答 >
问题:

需要关于Arraylist快速排序和分区的帮助吗

曾华翰
2023-03-14

说明:

给定读取用户ID(直到-1)的main(),完成快速排序()和分区()方法,使用快速排序算法按升序对ID进行排序,并每行输出一个排序后的ID。

示例输入:

kaylasimms 
julia 
myron1994 
kaylajones 
-1

样本输出:

julia 
kaylajones
kaylasimms
myron1994 

我运行并构建了我的代码,它没有显示任何错误,所以我猜我的方式有问题。输出仅按我输入的顺序显示字符串,但没有-1。

我的代码:

import java.util.Scanner;
import java.util.ArrayList;

public class UserIDSorting {
   // TODO: Write the partitioning algorithm - pick the middle element as the 
   //       pivot, compare the values using two index variables l and h (low and high), 
   //       initialized to the left and right sides of the current elements being sorted,
   //       and determine if a swap is necessary
   

public static int partition(ArrayList<String> numbers, int lowIndex, int highIndex) {
   // Pick middle element as pivot
   int midpoint = (lowIndex + (highIndex - lowIndex) / 2);
   int pivot = midpoint;
   
   boolean done = false;
   while (!done) {
      // Increment lowIndex while numbers[lowIndex] < pivot
      while (lowIndex < pivot) {
         lowIndex += 1;
      }
      
      // Decrement highIndex while pivot < numbers[highIndex]
      while (pivot < highIndex) {
         highIndex -= 1;
      }
      
      // If zero or one elements remain, then all numbers are 
      // partitioned. Return highIndex.
      if (lowIndex >= highIndex) {
         done = true;
      }
      else {
         // Swap numbers[lowIndex] and numbers[highIndex]
         int temp = lowIndex;
         lowIndex = highIndex;
         highIndex = temp;
         
         // Update lowIndex and highIndex
         lowIndex += 1;
         highIndex -= 1;
      }
   }
   
   return highIndex;
}

public static void quicksort(ArrayList<String> numbers, int lowIndex, int highIndex) {
   // Base case: If the partition size is 1 or zero 
   // elements, then the partition is already sorted
   if (lowIndex >= highIndex) {
      return;
   }
   
   // Partition the data within the array. Value lowEndIndex 
   // returned from partitioning is the index of the low 
   // partition's last element.
   int lowEndIndex = partition(numbers, lowIndex, highIndex);
   
   // Recursively sort low partition (lowIndex to lowEndIndex) 
   // and high partition (lowEndIndex + 1 to highIndex)
   quicksort(numbers, lowIndex, lowEndIndex);
   quicksort(numbers, lowEndIndex + 1, highIndex);
}  

   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      ArrayList<String> userIDList = new ArrayList<String>();

      String userID = "";

      
      while (!userID.equals("-1")) {
         userID = scnr.next();
         userIDList.add(userID);
      }
      
      // Initial call to quicksort 
      quicksort(userIDList, 0, userIDList.size() - 2);

      for (int i = 0; i < userIDList.size(); ++i) {
         System.out.println(userIDList.get(i));
      }
   }
}

共有1个答案

颜实
2023-03-14

在某个时刻,你需要交换这些值,对吗?你计算索引,但你从来没有做过像数字这样的事情。设置(索引、值)。。。结果与输入完全相同,这并不奇怪。

 类似资料:
  • 我想对我的文件内容进行排序。我的文件内容是学生姓名,他们的学生编号,他们的班级,他们的成绩。这些数据由“;”分隔。首先,我需要计算平均值和字母等级。我已经计算过了,但是我需要将所有内容写入另一个文件,顺序必须是最高等级到最低等级。我该怎么办?

  • 我的java课有一个实验室。我拥有一切,除了不能得到正常工作的平均方法。每当我运行程序时,平均值都是从随机值中计算出来的,而不是更新的值。 程序测试 未排序的数组类(我之前忘记附加)

  • 我正在学习快速排序在第四算法课程,罗伯特塞奇威克。 我想知道quicksort代码的以下分区是长度为n的数组中比较的个数。

  • 在一个HTML文件中, 包含许多 ,而在另一个 中, 包含许多 。使用我需要的JavaScript,当我悬停在第一个上时,第一个 的背景颜色会发生变化,以此类推... 匿名用户 你的问题是如此令人困惑,提供一个屏幕截图或绘图表明你实际想要什么。

  • 但我想知道如何选择我希望的支点,例如在这个整数列表中,8、7、1、9、11、5、6,我希望选择键6作为我代码中的支点。或者我想选9或者其他什么。我怎样才能把它写进我的代码?非常感谢任何帮助。

  • 我不太明白我在做什么,我做错了什么。请帮我修改/完成我的代码。我应该用你选择的输入数据创建至少3个Student对象,以使用类的构造函数初始化Student对象的所有数据字段。声明ArrayList对象以保存学生对象。将学生对象添加到ArrayList对象。调用Student类的toString方法,使用ArrayList对象中的Student对象打印学生的全名,后跟出生日期和每个学生的地址。 如