使用bubblesort排序数组列表的算法java问题

oknrviil  于 2021-07-06  发布在  Java
关注(0)|答案(4)|浏览(443)

我现在有个问题,我有一个大学作业,我们需要列出一个包含书籍的文件,应该从a到z排序,
我的排序算法是冒泡排序,目前不是按字母顺序排序,但不给错误,我看不出我应该在哪里改变,使其工作,因为编码似乎正确的我。
我们不允许使用集合,所以这就是我不使用sort()的原因。

package Book;
public class AlphabeticalOrderTitle{
    //Global variables
    public static String input;
    public static int bookId;
    public static String bookTitle;
    public static String authorName;
    public static boolean isAvailable;

    public static void main(String[] args) 
    {   
        ArrayList<Book> books = BubbleSort();
        System.out.println(linearSearch(books));
    }

    public static ArrayList<Book> loadData() {
        //Creating an array list;
        ArrayList<Book> books = new ArrayList<>();

        try {
            //Here we start reading our file
            BufferedReader br = new BufferedReader(new FileReader("Book.txt"));

            //This header string will allow to skip the header so does not mismatch with getter and setters.
            String header = br.readLine();

            //This string will read the lines.
            String contentLine = br.readLine();

            //Giving our array name data;
            String [] data;

            //Here we loop to continue the reading of data for each array box.
            while (contentLine != null) {

                data = contentLine.split(",");
                bookId = Integer.parseInt(data[0]);
                bookTitle = data[1];
                authorName = data[2];
                isAvailable = Boolean.parseBoolean(data[3]);
                books.add(new Book(bookId, bookTitle, authorName, isAvailable));
                contentLine = br.readLine();
            }

        }catch (IOException ex) {
            Logger.getLogger(SearchBookAuthor.class.getName()).log(Level.SEVERE, null,ex);
        }       
        return books;
    }

    public static int linearSearch(ArrayList<Book> array){

        //Variables for holding values
        int n;
        String temp;

        // Going one by one the elements in the array
        for(int g = 0; g < array.size(); g++){

                //Getting the array size from the file and giving the array name a size
                  n = array.size();
                  String names[] = new String[n];

                //Load all the names
                  for(int i = 0; i < n; i++) {
                      names[i] = array.get(g).getBookTitle();
                  }

                  //Bubble sort starts
                  for (int i = 0; i < n; i++) 
                  {
                      for (int j = i + 1; j < n; j++) 
                      {
                          if (names[i].compareTo(names[j]) > 0) 
                          {
                              temp = names[i];
                              names[i] = names[j];
                              names[j] = temp;
                          }
                      }
                  }
                 //Print sorted
                    System.out.println(names[n-1]);

        }      
        return -1;
    }
}

输出:

Captains
Romeo
Don
-1

我的目标是船长,唐,罗密欧。
我的book.txt包含如下内容:book
有什么建议要我修吗?非常感谢你。

70gysomp

70gysomp1#

因此,经过几天的工作,我已经来了一个工作的解决方案感谢大家。

public class Alphabetical_Order {

//Global variables
public static String input;
public static int bookId;
public static String bookTitle;
public static String authorName;
public static boolean isAvailable;

//Creating an array list;
public static ArrayList<Book> books = new ArrayList<>();

public static void main(String[] args) 
{
    loadData();
    int n = 0;
    String temp;
  //Scanner s = new Scanner(System.in);
    System.out.print("Enter number of names you want to enter:");

    //get size of arraylist
    for (int g = 0; g < books.size(); g ++) {
           n = books.size();

    }

    String names[] = new String[n];

    //Names to be get from user
    Scanner s1 = new Scanner(System.in);
    System.out.println("Enter all the names:");
    for(int i = 0; i < n; i++)
    {
        names[i] = books.get(i).getAuthorName();
    }
    for (int i = 0; i < n; i++) 
    {
        for (int j = i + 1; j < n; j++) 
        {
            if (names[i].compareTo(names[j])>0) 
            {
                temp = names[i];
                names[i] = names[j];
                names[j] = temp;
            }
        }
    }
    System.out.print("Names in Sorted Order:");
    for (int i = 0; i < n - 1; i++) 
    {
        System.out.print(names[i] + ",");
    }
    System.out.print(names[n - 1]);
}

public static void loadData() { 

    try {
        //Here we start reading our file
        BufferedReader br = new BufferedReader(new FileReader("Book.csv"));

        //This header string will allow to store the header so does not mistach with getter and setters.
        String header = br.readLine();

        //This string will read the lines.
        String contentLine = br.readLine();

        //Giving our array name data;
        String [] data;

        //Here we loop to continue the reading of data for each array box.
        while (contentLine != null) {

            data = contentLine.split(",");
            bookId = Integer.parseInt(data[0]);
            bookTitle = data[1];
            authorName = data[2];
            isAvailable = Boolean.parseBoolean(data[3]);
            books.add(new Book(bookId, bookTitle, authorName, isAvailable));
            contentLine = br.readLine();
        }

    }catch (IOException ex) {
        Logger.getLogger(SearchBookAuthor.class.getName()).log(Level.SEVERE, null,ex);
    } 

}}
sxissh06

sxissh062#

对于由n个元素组成的数组a[n],则冒泡排序的第一个循环总是以n-1结束。冒泡排序的思想是比较相邻的元素,然后在元素不有序时交换(根据用例增加/减少)。
所以,当i=n-1时,正如你在第一个for循环中提到的=>j=n-1。我们基本上是比较a[i=n-1]和a[j=n-1]。

//Bubble sort starts
                  for (int i = 0; i < n-1; i++) 
                  {
                      for (int j = i + 1; j < n; j++) 
                      {
                          if (names[i].compareTo(names[j]) > 0) 
                          {
                              temp = names[i];
                              names[i] = names[j];
                              names[j] = temp;
                          }
                      }
                  }

当你遇到这样的问题时,你可以试着用小数字替换,然后把循环内容写在纸上。有助于学习和建立逻辑。:)

a11xaf1n

a11xaf1n3#

首先,bubblesort()不是这个方法的合适名称,因为它所做的只是读取文件并将内容存储在列表中。
如果本课程不是高级算法类,那么您可以使用java库来对列表进行排序。
像这样的事情应该会起作用并产生所需的结果:
收藏。分类(书籍);
另外,我通常只做以下操作:list books=new arraylist<>();
如果必须实现气泡排序,请使用以下链接,该链接显示如何使用气泡排序对字符串数组进行排序:https://www.geeksforgeeks.org/sorting-strings-using-bubble-sort-2/

u5rb5r59

u5rb5r594#

气泡排序示例
我链接了一个冒泡排序示例。您可以单击java查看java版本。你可以看到你的和他们的有区别,尽管他们非常相似。
我要做的是手动操作。也就是说,拿一些纸,写下你的数组看起来像什么,然后假装你是电脑,看看你最终得到了什么。这对你来说是一个很好的锻炼,你可能会发现你做错了什么。

相关问题