如何在包含特定年份的对象数组中二进制搜索特定年份

问题描述 投票:0回答:1

我正在尝试获取代码来搜索特定年份制作的歌曲

我已经尝试实现自己的二进制搜索代码,但是它没有工作,而是在我从未要求输入时请求输入

类:

public class MusicV3
{
// instance variables
private int year;
private String title;
private String artist;

// Constructor for objects of class Music
public MusicV3(String t, int y, String a)
{
    // initialize instance variables
    title = t;
    year = y;
    artist = a;
}

public String getTitle()
{
    return title;
}

public void setTitle(String t)
{
    title = t;
}

public String getArtist()
{
    return artist;
}

public void setArtist(String a)
{
    artist = a;
}

public int getYear()
{
    return year;
}

public void setTitle(int y)
{
    year = y;
}

public String toString()
{
    String str = String.format( "%-25s %4d   %-20s ", title,  year , artist);
    return str;
}

}

测试者类:

     public class MusicV3Tester
     {
      public static void main(String[]args)
     {
    int find = 0;
    MusicV3[] songs = new MusicV3[10];
    MusicV3[] sortedSongs = new MusicV3[songs.length];
    songs[0] = new MusicV3("Sugar", 2014, "Maroon 5");
    songs[1] = new MusicV3("Mercy", 2016, "Shawn Mendes");
    songs[2] = new MusicV3("Shape of You", 2017, "Ed Sheeran");
    songs[3] = new MusicV3("Photograph", 2014, "Ed Sheeran");
    songs[4] = new MusicV3("Closer", 2016, "The Chainsmokers");
    songs[5] = new MusicV3("Galway Girl", 2017, "Ed Sheeran");
    songs[6] = new MusicV3("Counting Stars", 2013, "OneRepublic");
    songs[7] = new MusicV3("7 Years", 2015, "Lukas Graham");
    songs[8] = new MusicV3("Night Changes", 2014, "One Direction");
    songs[9] = new MusicV3("What Makes You Beautiful", 2011, "One Direction");

    printSongs(songs);
    System.out.println();

    sortedSongs = sortBySong(songs);
    System.out.println("Song list sorted by songs:");
    //printSongs(sortedSongs);
    System.out.println();

    System.out.println("Searching for the song: Paris");
    find = findSong(songs, "Paris");
    if(find != -1){
        System.out.println("We found Paris in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Paris is not in the song list");
    System.out.println();

    System.out.println("Searching for the song: Sugar");
    find = findSong(songs, "Sugar");
    if(find != -1){
        System.out.println("We found Sugar in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Sugar is not in the song list");
    System.out.println();

    sortedSongs = sortByYear(songs);
    System.out.println("Song list sorted by year:");
    //printSongs(sortedSongs);
    System.out.println();

    System.out.println("Searching for the year: 2000");
    find = findYear(songs, 2000);
    if(find != -1){
        System.out.println("We found songs made in the year 2000 in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Songs made in the year 2000 are not in the song list");
    System.out.println();

    System.out.println("Searching for the year: 2014");
    findYear(songs, 2014);
    if(find != -1){
        System.out.println("We found songs made in the year 2014 in the song list: ");
        System.out.println(sortedSongs[find]);
    }
    else
        System.out.println("Songs made in the year 2014 are not in the song list");
    System.out.println();

    sortedSongs = sortByArtist(songs);
    System.out.println("Song list sorted by artist:");
    //printSongs(sortedSongs);
    System.out.println();

    System.out.println("Searching for the artist: Sia");
    findArtist(songs, "Sia");
    System.out.println();

    System.out.println("Searching for the artist: Ed Sheeran");
    findArtist(songs, "Ed Sheeran");
    System.out.println();
}

public static void printSongs(MusicV3[] s)
{
    System.out.println("Song                      Year   Artist");
    System.out.println("-------------------------------------------------------");
    for(int i = 0; i < s.length; i++)
        System.out.println(s[i]);
}

public static MusicV3[] sortBySong(MusicV3 [] songs){
    MusicV3[] sortedList = songs;
    int i;
    int k;
    int posmax;
    MusicV3 temp;
    for ( i = songs.length - 1 ; i >= 0 ; i-- )
    {
       posmax = 0;
         for ( k = 0 ; k <= i ; k++ )
         {
             if (songs[k].getTitle().compareTo(songs[posmax].getTitle()) > 0)
             posmax = k;
            }

            temp = songs[i];
            songs[i] = songs[posmax];
            songs[posmax] = temp;
        }
return sortedList;
}

public static MusicV3[] sortByYear(MusicV3 [] movies){
    MusicV3[] sortedList = movies;
    int i;
    int k;
    int posmax;
    MusicV3 temp;
    for ( i = movies.length - 1 ; i >= 0 ; i-- )
    {
       posmax = 0;
         for ( k = 0 ; k <= i ; k++ )
         {
             if (movies[k].getYear()> movies[posmax].getYear())
             posmax = k;
            }

            temp = movies[i];
            movies[i] = movies[posmax];
            movies[posmax] = temp;
}
return sortedList;
}

public static MusicV3[] sortByArtist(MusicV3 [] songs){
    MusicV3[] sortedList = songs;
    int i;
    int k;
    int posmax;
    MusicV3 temp;
    for ( i = songs.length - 1 ; i >= 0 ; i-- )
    {
       posmax = 0;
         for ( k = 0 ; k <= i ; k++ )
         {
             if (songs[k].getArtist().compareTo(songs[posmax].getArtist()) > 0)
             posmax = k;
            }

            temp = songs[i];
            songs[i] = songs[posmax];
            songs[posmax] = temp;
        }
return sortedList;
}

public static int findSong( MusicV3[] songs, String song){
 int high = songs.length;
 int low = -1;
 int probe;

 while ( high - low > 1 )
 {
 probe = ( high + low ) / 2;
 if (songs[probe].getTitle().compareTo(song) > 0)
  high = probe;
 else 
  low = probe;
 }

 if ( low >= 0 && songs[low].getTitle().compareTo(song) == 0){
return low;
}
else{
 return -1;
}
}

public static int findYear(MusicV3[] songs, int year){
 int high = songs.length - 1;
 int low = 0;
 int probe;

 while (low <= high)
 {
 probe = ( high + low ) / 2;
 if (songs[probe].getYear() == year)
  return probe;
  if(songs[probe].getYear() > year)
  low = probe;
else 
 low = probe + 1;
} 
return -1;
}

public static void findArtist(MusicV3[] s, String artist)
{
    int found = 0;
    for(int i = 0; i < s.length; i++){
        if (s[i].getArtist().compareTo(artist) == 0)
        {
            System.out.println(s[i]);
            found++;
        }
    }
    if (found == 0)
    { // we have not found the location
        System.out.println("There are no songs on the songs list made in the year " + artist);
        System.out.println();
    } 
    else
    {
        System.out.print("There were " + found + " listings for " + artist);
        System.out.println();
    }
}

}

忽略歌曲和艺术家搜索,我可以做的并且没有问题,但是使用年份搜索,它没有正常工作,因为它应该正确打印特定年份给出的所有歌曲(2000年和2014年)但它不是,而是根本不工作,并出于某种原因要求输入。这是我第一次进行二分查找,所以我对此很陌生。

java arrays binary-search
1个回答
0
投票

如果你只返回其中一个的索引(甚至不是第一个那个),你怎么可能显示所有这些?

尝试返回结果列表。或者Pair<int, int>的索引开始和结束

© www.soinside.com 2019 - 2024. All rights reserved.