Java 快速排序算法未通过测试
我与一个小组一起制作了一个快速排序算法,用于在课外练习java编码(不是家庭作业问题)。我的代码似乎适合大多数QuickSort实现,但在运行测试工具时,它会失败5/7次测试。我的实现有什么明显的问题吗Java 快速排序算法未通过测试,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,我与一个小组一起制作了一个快速排序算法,用于在课外练习java编码(不是家庭作业问题)。我的代码似乎适合大多数QuickSort实现,但在运行测试工具时,它会失败5/7次测试。我的实现有什么明显的问题吗 public class QuickSort implements SortInterface { public void swap(Note[] Note, int i, int j) { Note temp = Note[i]; Note[i] =
public class QuickSort implements SortInterface {
public void swap(Note[] Note, int i, int j) {
Note temp = Note[i];
Note[i] = Note[j];
Note[j] = temp;
}
// TODO Auto-generated method stub pseudo here
public void quicksort(Note[] Note, long m, long l){
if(m<l){
//choose a pivot, median of three
long pivot_index = pivotMaker(Note, m, l);
//partition the array about the pivot, return index of pivot, where swaps happen
pivot_index = partition(Note, pivot_index);
quicksort(Note, m, pivot_index-1 ); // breaks up lesser than pivot side
quicksort(Note, pivot_index+1, l); // breaks up higher than pivot side
} // end if
}
public long pivotMaker(Note [] Note, long m, long l) // physically creates a median of three pivot
{
int center = (int)((m + l) / 2);
// order left & center
if (Note[(int)(m)].getID() > Note[center].getID())
swap(Note,(int)m, center);
// order left & right
if (Note[(int)(m)].getID() > Note[(int)l].getID())
swap(Note,(int)m, (int)l);
// order center & right
if (Note[center].getID() > Note[(int)l].getID())
swap(Note,center, (int)l);
swap(Note,center, (int)l - 1); // put pivot on right
return Note[(int)(l-1)].getID(); // return median value
}
//median of 3
// end pivot maker
public int partition(Note[] Note, long pivot_index){ // will place the pivot in its final index
return(0);
}
public Note[] sort(Note[] s) {
quicksort(s, 0, s.length-1);
return s;
}
public static void main(String[] args) {
// make an array of notes
Note q = new Note(" ", " ");
Note n = new Note("CSCI 230 Project Plan",
"Each person will number their top 5 choices.\n" +
"By next week, Dr. Hill will assign which piece\n" +
"everyone will work on.\n");
n.tag("CSCI 230");
n.tag("final project");
Note[] Note = {q,n, new Note(" ", " "), new Note(" ", " "), new Note(" ", " "), new Note(" ", " "), new Note(" ", " ")};
//print out not id's
//call QuickSort
System.out.println(Note);
//print out note_id's
}
}
public类快速排序实现SortInterface{
公共无效掉期(注[]注,int i,int j){
注释温度=注释[i];
注[i]=注[j];
注[j]=温度;
}
//TODO在此自动生成方法存根
公共无效快速排序(注[]注,长m,长l){
if(m Note[center].getID())
互换(注,(整数)m,中间);
//左、右顺序
if(Note[(int)(m)].getID()>Note[(int)l].getID())
掉期(注,(整数)m,(整数)l);
//订单中心&右侧
if(Note[center].getID()>Note[(int)l].getID())
互换(注释,中间,(内部)l);
交换(注意,中间,(int)l-1);//将枢轴放在右边
返回注释[(int)(l-1)].getID();//返回中间值
}
//中位数为3
//端部枢轴制造机
公共int分区(注意[]注意,长pivot_索引){//将把pivot放在它的最终索引中
返回(0);
}
公共注释[]排序(注释[]s){
快速排序(s,0,s.length-1);
返回s;
}
公共静态void main(字符串[]args){
//做一系列笔记
注释q=新注释(“,”);
注n=新注(“CSCI 230项目计划”,
“每个人将对他们的前5个选择进行编号。\n”+
“到下周,希尔博士将指定哪一块\n”+
“每个人都将继续工作。\n”);
n、 标签(“CSCI 230”);
n、 标签(“最终项目”);
注[]注={q,n,新注(“,”),新注(“,”),新注(“,”),新注(“,”),新注(“,”),新注(“,”)};
//打印出来的不是身份证
//调用快速排序
系统输出打印项次(注);
//打印便笺
}
}
如果包含一个成功的测试和一个失败的测试,会更容易。但很明显,分区尚未实现:
`public int partition(Note[] Note, long pivot_index)` always returns `0`.
另外,您正在使用索引l-1
,我认为您打算使用l
:
swap(Note,center, (int)l - 1);
您可以添加一些失败的最小测试用例分区代码在哪里?最好将枢轴保持在中心位置,而不是将其交换到右侧。根据分区方法的不同,左半部分递归调用要么是快速排序(data,low,pivot-1),要么是快速排序(data,low,pivot),而右半部分通常是快速排序(data,pivot+1,high)。感谢您的帮助,我不敢相信我忽略了这一点。