[英]Recursive Sorting: quick sort syntax
程序应该使用快速排序对一组数字进行排序。 使用从 10,000,000 个随机生成的数字开始的数据集。 我一直在学习快速排序。 另一个,我完成了归并排序。 但是,快速排序是语法。 我不知道为什么它是语法。
public static void main(String[] args)throws Exception{
for(int n = 1; n<=100; n++)
{// begin for
int size = 10000000; //change this num to change the size of the random array 10,000,000
int[] a = new int[size];
int[] temp = new int[a.length]; //temporary array, it's empty.
//fill the array with random integers
for(int i = 0; i< a.length; i++)
a[i] = (int)(Math.random()*100000 + 1);
long startTime = System.nanoTime();
quickSort(a, temp, 0,(a.length - 1));
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.printf("%12.8f %n", (double)duration/100000000);
}// End for
}// end main
public static void quickSort(int[] a, int[] temp, int startIndex, int endIndex)
{// begin quickSort
int pivotIndex; //the index of pivot returned by the quciksort partition
if(startIndex < endIndex)
{//Begin if
pivotIndex = partition(a, temp, startIndex, endIndex);
quickSort(a, temp, startIndex, pivotIndex -1);
quickSort(a, temp, pivotIndex+1, endIndex);
}//End if
}//end quickSort
public static int partition(int[] a, int[] temp, int startIndex, int endIndex) {
int pivotIndex;
int pivot;
int midIndex = startIndex;
pivotIndex = (startIndex + endIndex) / 2;
pivot = a[pivotIndex];
swap(a, temp, pivotIndex, endIndex);
for (int i = startIndex; i < endIndex; i++) {
if (a[i] < pivot) ;
{
swap(a, temp, i, midIndex);
midIndex = midIndex + 1;
}
}
swap(a, temp, midIndex, endIndex);
return midIndex;
}// end partition
public static void swap(int[]a, int[]temp, int first, int second)
{//Begin swap
for(int i = first; i <= second; i++){
temp[i] = a[i];
}
int c;
c = a[first];
a[first] = a[second];
a[second] = c;
}//End swap
public static void writeLines(int[] a, String fileName) throws Exception
{ // Begin writeLines(int[] a, String fileName)
// create a File class object with the given file name
java.io.File out = new java.io.File(fileName);
// Create a PrintWriter output stream and link it to the File object
java.io.PrintWriter outfile = new java.io.PrintWriter(out);
// write the elements of an int array, separated by spaces
for (int i = 0; i < a.length; i++)
{ // Begin for (int i = 0; i < a.length; i++)
outfile.print(a[i] + " ");
} // End for (int i = 0; i < a.length; i++)
// print a newline at the end of the list of integers
outfile.println();
outfile.close();
} // End writeLines(int[] a, String fileName) throws Exception
}//end quickSORT
我有语法。 它说的是 quick.partition 和 quick.quickSort。
简单来说,Hoare 的快速排序通过以下方式工作:
您的算法使用了类似于 Hoare 和 Lomuto 分区策略的组合,但更像是 Lomuto。 有多种分区策略(选择如何将数组划分为部分)取决于数据容器的工作方式(Hoare 的策略要求容器可以随机访问或从每一端访问,如双链表,Lomuto 只需要一个可以在一个方向上读取的数据容器)但快速排序的基本算法是这样一种概念,即您将所有内容分成两堆“想要更小的值”和“想要更大的值”,在两堆之间反复交换东西直到“更小”和“更大” " 真的是这样,然后再对较小的一堆,然后对较大的一堆再次执行该过程。 如果您继续进行并划分工作,那么您将达到一切都已排序的地步。
如果你还在纠结,不妨考虑一下:如果你认为它就像将大量只使用字符 A 和 Z 的单词按字母顺序排序,你可能会有一种将所有东西堆成一堆的策略:2 堆以 A 开头的那些和以 Z 开头的那些。然后你做 4 堆,那些以 AA、AZ、ZA 和 ZZ 开头的堆。 然后你移动到 8 堆三个字母 AAA、AAZ、AZA、AZZ、ZAA、ZAZ、ZZA 和 ZZZ(如果你现在睡着了,这可能是合适的)。 这也是一种“分而治之的策略”; 你会达到一个程度,你已经把这些堆分开得如此之多,以至于它们每个只包含一个东西,然后如果你按顺序收集堆(如果你在布置它们时小心翼翼,它们就已经是有序的了) 然后它们被排序
哦,您的代码有一个错误,因为您在 if 之后添加了一个分号,这会创建一个空语句(使 if 无用),而且您似乎也没有使用随身携带的 temp 数组。 就像这个算法希望混合两种不同的排序策略但未完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.