package introductionToAlgorithms;
public class QuickSort {
static int[] a = {3,1,7,4,10,6,8,9,2,5};
static int findPivot(int low,int high) {
int i = low, j = high+1;
int pivotValue = a[low];
while( true ) {
while( a[--j] > pivotValue );
System.out.println(" j = " + j);
while( i < j && a[++i] < pivotValue );
System.out.println(" i = " + i);
if( i >= j ) break;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
a[low] = a[j];
a[j] = pivotValue;
return j;
}
static void quickSort(int low, int high){
if( low < high ) {
int pivot = findPivot(low,high);
quickSort(low,pivot-1);
quickSort(pivot+1,high);
}
}
static void printArray() {
for(int value: a)
System.out.print(value + " ");
System.out.println();
}
public static void main(String[] args) {
printArray();
quickSort(0,a.length-1);
printArray();
}
}
分享到:
相关推荐
#include #include #include ... //快速排序 void QSort(SqList &,int,int); //子序列快速排序 int Partition(SqList &,int,int); //一趟快速排序 void PrintSqList(SqList); //显示表中的所有元素
例如,合并排序的实现可能涉及到动态内存分配、指针操作,而快速排序则需要理解如何使用指针进行数组元素的交换和分割。 在实际编码过程中,我们需要注意以下几点: 1. 递归深度限制:对于大规模数据,要警惕递归...
一个快速排序的代码,运行成功的啊。和大家分享一下啊
C 语言实现的快速排序代码详解 快速排序是常用的排序算法之一,它的基本思想是选择一个元素作为基准,将数组分为两个部分,其中一个部分的元素小于基准,另一个部分的元素大于基准,然后递归地对这两个部分进行排序...
数据结构--快速排序C++源代码,自己编写调试,代码简单易懂,不长
下面是一个带有注释的C#快速排序代码示例: ```csharp using System; using System.Collections.Generic; class QuickSortExample { // 快速排序函数 static void QuickSort(int[] arr, int low, int high) { ...
快速排序是一种高效的排序算法,由英国计算机科学家C.A.R. Hoare在1960年提出。它的基本思想是分治法(Divide and ...通过学习和比较这些代码,你将能够深入理解快速排序的工作原理,并能够在自己的项目中灵活运用。
快速排序是一种高效的排序算法,由英国计算机科学家...总的来说,C#实现的快速排序代码简洁明了,适用于对数组进行高效排序的需求。不过,需要注意的是,对于大量数据的排序,可以考虑并行化或优化枢轴选择以提高性能。
"快速排序算法java代码" 快速排序算法是由Tony Hoare在1960年提出的一种排序算法,它的平均时间复杂度为O(n log n),是目前最快的排序算法之一。下面我们将详细地讲解快速排序算法的java代码实现。 快速排序算法的...
快速排序是一种高效的排序算法,由英国计算机科学家C.A.R. Hoare在1960年提出。它的基本思想是分治法(Divide and Conquer),即把一个大问题分解成若干个小问题来解决,最终将小问题的结果合并得到原问题的解。在这...
java 快速排序实现。可以跑的代码 java 快速排序实现。可以跑的代码 java 快速排序实现。可以跑的代码 java 快速排序实现。可以跑的代码
以下是一个简单的C++快速排序代码实现: ```cpp #include using namespace std; void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition(int arr[], int low, int high) { int pivot = ...
在VS2010中编译运行快速排序代码,需要注意项目设置、包含文件、链接器设置等,确保编译环境支持所需的库和标准。通过调试工具可以观察排序过程,帮助理解算法的执行步骤。 总的来说,快速排序是一种在实际应用中...
用C++写了以上三种排序算法,对初学数据结构的同学一个参考