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

时间:2019-03-24 03:47:03

标签: java arrays binary-search

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

我尝试实现自己的二进制搜索代码,但是它不起作用,而是在我从未要求输入时要求输入

班级:

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年和2000年)中的所有歌曲,因此无法正常运行2014年),但并非如此,相反,它根本无法正常工作,并出于某种原因要求提供意见。这是我第一次进行二进制搜索,因此我对此很陌生。

1 个答案:

答案 0 :(得分:0)

仅返回其中一个的索引(甚至不返回第一个索引)时,如何显示所有这些索引?

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