[英]How to perform an Insertion Sort algorithm on an array of objects based on a String parameter?
我有兩個類:Movie2 和 TestMovie2。
電影2如下:
public class Movie2
{
private String title;
private int year;
private String studio;
Movie2(String title, int year, String studio)
{
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public String getStudio()
{
return studio;
}
public void setStudio(String studio)
{
this.studio = studio;
}
}
TestMovie2如下:
public class TestMovie2
{
private static Movie2[] myMovies = new Movie2[10];
private static void addMovies()
{
myMovies[0] = new Movie2("The Muppets Take Manhattan", 2001, "Columbia Tristar");
myMovies[1] = new Movie2("Mulan Special Edition", 2004, "Disney");
myMovies[2] = new Movie2("Shrek 2", 2004, "Dreamworks");
myMovies[3] = new Movie2("The Incredibles", 2004, "Pixar");
myMovies[4] = new Movie2("Nanny McPhee", 2006, "Universal");
myMovies[5] = new Movie2("The Curse of the Were-Rabbit", 2006, "Aardman");
myMovies[6] = new Movie2("Ice Age", 2002, "20th Century Fox");
myMovies[7] = new Movie2("Lilo & Stitch", 2002, "Disney");
myMovies[8] = new Movie2("Robots", 2005, "20th Century Fox");
myMovies[9] = new Movie2("Monsters", 2001, "Pixar");
}
private static void printMovies()
{
for (Movie2 i : myMovies)
{
System.out.printf("\n%-30s%10s%30s", i.getTitle(), i.getYear(), i.getStudio());
}
}
private static void sortTitles()
{
for (Movie2 i : myMovies)
{
}
}
public static void main (String args[])
{
addMovies();
printMovies();
sortTitles();
}
}
在 sortTitles() 中,我試圖創建一個算法,該算法將根據標題對對象(電影)進行排序。 不幸的是,我不能使用 Arrays.sort() 或任何類似的東西。 如果有人能給我一個字符串排序器的例子,然后逐行解釋它是如何工作的,我將不勝感激,這樣我就可以在我的項目中實現一個。
編輯:我不能使用 Arrays.sort() 因為它違反了作業的說明。
我相信做這個項目的目的是為了學習排序算法。 我認為給你代碼不是一個好主意,你不會從中學到很多東西。 這是一種稱為插入排序的簡單排序算法,例如: http : //www.java2novice.com/java-sorting-algorithms/insertion-sort/ 。 您可以使用 String.compareTo(anotherString) 對標題進行排序。
此外,還有許多其他不錯的排序算法可以用來提高性能,例如歸並排序和快速排序。 我可以幫你完成算法,但不會給你代碼。 祝你好運。 :)
選項 1:使用 Arrays.sort() 就像其他人建議的那樣,您應該能夠使用 Arrays.sort()。
現在,Arrays.sort() 對像 int[] 這樣的原始類型自動按升序排序例如:int[] numbers,然后是 Arrays.sort(numbers)。
在您的情況下,因為您想對非原始類型的電影數組進行排序。 您需要編寫一個比較器類並將其傳遞給 Arrays.sort(movies, [Your MoviesComparator Instance])。 查看此鏈接Sorting中的示例 3,它創建了一個比較器來對薪水進行排序,您想創建一個類似的比較器並按標題進行排序。 希望這會有所幫助。
選項 2:編寫自己的排序方法。 我提供了一個使用選擇排序的示例。
public class Solution {
/**
* Movie pojo
*/
public static class Movie {
private String title;
private int year;
private String studio;
Movie(String title, int year, String studio) {
this.title = title;
this.year = year;
this.studio = studio;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getStudio() {
return studio;
}
@Override
public String toString() {
return "Title" + this.getTitle() + " : " + "Year" + this.getYear() + " : " + "Studio" + this.getStudio();
}
}
/**
* Sort movies on titles using selection sort
* @param movies
*/
public static void sortMovieTitles(Movie[] movies) {
/*Algorithm: (https://en.wikipedia.org/wiki/Selection_sort)
Idea is to iterate & find the min element in every iteration and place it lowest index position */
for (int i = 0; i < movies.length - 1; ++i)
{
int minIndex = i;
for (int j = i + 1; j < movies.length; ++j)
{
if (movies[j].title.compareTo(movies[minIndex].title) < 0)
{
minIndex = j;
}
}
Movie tempMovie = movies[i];
movies[i] = movies[minIndex];
movies[minIndex] = tempMovie;
}
//print
for (int i = 0; i < movies.length; i++) {
System.out.println(movies[i]);
}
}
/**
* Main
* @param args input args
*/
public static void main(String[] args)
{
Movie[] myMovies = new Movie[10];
myMovies[0] = new Movie("The Muppets Take Manhattan", 2001, "Columbia Tristar");
myMovies[1] = new Movie("Mulan Special Edition", 2004, "Disney");
myMovies[2] = new Movie("Shrek 2", 2004, "Dreamworks");
myMovies[3] = new Movie("The Incredibles", 2004, "Pixar");
myMovies[4] = new Movie("Nanny McPhee", 2006, "Universal");
myMovies[5] = new Movie("The Curse of the Were-Rabbit", 2006, "Aardman");
myMovies[6] = new Movie("Ice Age", 2002, "20th Century Fox");
myMovies[7] = new Movie("Lilo & Stitch", 2002, "Disney");
myMovies[8] = new Movie("Robots", 2005, "20th Century Fox");
myMovies[9] = new Movie("Monsters", 2001, "Pixar");
sortMovieTitles(myMovies);
}
}
//Output:
TitleIce Age : Year2002 : Studio20th Century Fox
TitleLilo & Stitch : Year2002 : StudioDisney
TitleMonsters : Year2001 : StudioPixar
TitleMulan Special Edition : Year2004 : StudioDisney
TitleNanny McPhee : Year2006 : StudioUniversal
TitleRobots : Year2005 : Studio20th Century Fox
TitleShrek 2 : Year2004 : StudioDreamworks
TitleThe Curse of the Were-Rabbit : Year2006 : StudioAardman
TitleThe Incredibles : Year2004 : StudioPixar
TitleThe Muppets Take Manhattan : Year2001 : StudioColumbia Tristar
想通了,代碼如下:
private static void sortTitles(Movie2[] input)
{
for (int i = 1; i < input.length; i++)
{
Movie2 temp;
for (int j = i; j > 0; j--)
{
if (input[j].getTitle().compareTo(input[j-1].getTitle()) < 0)
{
temp = input[j];
input[j] = input[j - 1];
input[j - 1] = temp;
}
}
}
for (Movie2 i : input)
{
System.out.printf("\n%-30s%10s%30s", i.getTitle(), i.getYear(), i.getStudio());
}
}
該算法改變了這一點:
The Muppets Take Manhattan 2001 Columbia Tristar
Mulan Special Edition 2004 Disney
Shrek 2 2004 Dreamworks
The Incredibles 2004 Pixar
Nanny McPhee 2006 Universal
The Curse of the Were-Rabbit 2006 Aardman
Ice Age 2002 20th Century Fox
Lilo & Stitch 2002 Disney
Robots 2005 20th Century Fox
Monsters 2001 Pixar
對此:
Ice Age 2002 20th Century Fox
Lilo & Stitch 2002 Disney
Monsters 2001 Pixar
Mulan Special Edition 2004 Disney
Nanny McPhee 2006 Universal
Robots 2005 20th Century Fox
Shrek 2 2004 Dreamworks
The Curse of the Were-Rabbit 2006 Aardman
The Incredibles 2004 Pixar
The Muppets Take Manhattan 2001 Columbia Tristar
謝謝大家的幫助!
好吧,閱讀您的問題后,這里是基於字符串對數組進行排序的代碼,
import java.util.Arrays;
public class InsertionSortString
{
public static void main(String[] args)
{
int a, b;
String temp;
String[] strArray = {"Eric","David","Carol","Ben","Andy","Blair"};
System.out.println(Arrays.toString(strArray));
for(b = 1; b < strArray.length; b++)
{
temp = strArray[b];
a = b - 1;
while (a >= 0)
{
if (temp.compareTo(strArray[a]) > 0)
{
break;
}
strArray[a + 1] = strArray[a];
a--;
}
strArray[a + 1] = temp;
System.out.println(Arrays.toString(strArray));
}
System.out.println(Arrays.toString(strArray));
}
}
同時,您可以參考下面的資源以獲取有關 Java 中插入排序的更多信息,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.