成都创新互联网站制作重庆分公司

java实现快排的代码 java实现快排的代码是什么

请问一下java快速排序源代码

快速排序:

创新互联成立于2013年,是专业互联网技术服务公司,拥有项目网站制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元柳林做网站,已为上家服务,为柳林各地企业和个人服务,联系电话:18982081108

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class QuickSort implements SortUtil.Sort{

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data) {

quickSort(data,0,data.length-1);

}

private void quickSort(int[] data,int i,int j){

int pivotIndex=(i+j)/2;

//swap

SortUtil.swap(data,pivotIndex,j);

int k=partition(data,i-1,j,data[j]);

SortUtil.swap(data,k,j);

if((k-i)1) quickSort(data,i,k-1);

if((j-k)1) quickSort(data,k+1,j);

}

/**

* @param data

* @param i

* @param j

* @return

*/

private int partition(int[] data, int l, int r,int pivot) {

do{

while(data[++l]pivot);

while((r!=0)data[--r]pivot);

SortUtil.swap(data,l,r);

}

while(lr);

SortUtil.swap(data,l,r);

return l;

}

}

改进后的快速排序:

package org.rut.util.algorithm.support;

import org.rut.util.algorithm.SortUtil;

/**

* @author treeroot

* @since 2006-2-2

* @version 1.0

*/

public class ImprovedQuickSort implements SortUtil.Sort {

private static int MAX_STACK_SIZE=4096;

private static int THRESHOLD=10;

/* (non-Javadoc)

* @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])

*/

public void sort(int[] data) {

int[] stack=new int[MAX_STACK_SIZE];

int top=-1;

int pivot;

int pivotIndex,l,r;

stack[++top]=0;

stack[++top]=data.length-1;

while(top0){

int j=stack[top--];

int i=stack[top--];

pivotIndex=(i+j)/2;

pivot=data[pivotIndex];

SortUtil.swap(data,pivotIndex,j);

//partition

l=i-1;

r=j;

do{

while(data[++l]pivot);

while((r!=0)(data[--r]pivot));

SortUtil.swap(data,l,r);

}

while(lr);

SortUtil.swap(data,l,r);

SortUtil.swap(data,l,j);

if((l-i)THRESHOLD){

stack[++top]=i;

stack[++top]=l-1;

}

if((j-l)THRESHOLD){

stack[++top]=l+1;

stack[++top]=j;

}

}

//new InsertSort().sort(data);

insertSort(data);

}

/**

* @param data

*/

private void insertSort(int[] data) {

int temp;

for(int i=1;idata.length;i++){

for(int j=i;(j0)(data[j]data[j-1]);j--){

SortUtil.swap(data,j,j-1);

}

}

}

}

java快速排序简单代码

.example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px} 排序算法是《数据结构与算法》中最基本的算法之一。排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。以下是快速排序算法:

快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要 Ο(nlogn) 次比较。在最坏状况下则需要 Ο(n2) 次比较,但这种状况并不常见。事实上,快速排序通常明显比其他 Ο(nlogn) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来。

快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists)。

快速排序又是一种分而治之思想在排序算法上的典型应用。本质上来看,快速排序应该算是在冒泡排序基础上的递归分治法。

快速排序的名字起的是简单粗暴,因为一听到这个名字你就知道它存在的意义,就是快,而且效率高!它是处理大数据最快的排序算法之一了。虽然 Worst Case 的时间复杂度达到了 O(n?),但是人家就是优秀,在大多数情况下都比平均时间复杂度为 O(n logn) 的排序算法表现要更好,可是这是为什么呢,我也不知道。好在我的强迫症又犯了,查了 N 多资料终于在《算法艺术与信息学竞赛》上找到了满意的答案:

快速排序的最坏运行情况是 O(n?),比如说顺序数列的快排。但它的平摊期望时间是 O(nlogn),且 O(nlogn) 记号中隐含的常数因子很小,比复杂度稳定等于 O(nlogn) 的归并排序要小很多。所以,对绝大多数顺序性较弱的随机数列而言,快速排序总是优于归并排序。

1. 算法步骤

从数列中挑出一个元素,称为 "基准"(pivot);

重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;

递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序;

2. 动图演示

代码实现 JavaScript 实例 function quickSort ( arr , left , right ) {

var len = arr. length ,

    partitionIndex ,

    left = typeof left != 'number' ? 0 : left ,

    right = typeof right != 'number' ? len - 1 : right ;

if ( left

求使用java实现的快排算法

① 代码:

public class quicksortdemo {

private int array[];

private int length;

public void sort(int[] inputArr) {

if (inputArr == null || inputArr.length == 0) {

return;

}

this.array = inputArr;

length = inputArr.length;

quickSort(0, length - 1);

}

private void quickSort(int lowerIndex, int higherIndex) {

int i = lowerIndex;

int j = higherIndex;

// calculate pivot number

int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];

// Divide into two arrays

while (i = j) {

while (array[i]  pivot) {

i++;

}

while (array[j]  pivot) {

j--;

}

if (i = j) {

swap(i, j);                

i++;

j--;

}

}

// call quickSort() method recursively

if (lowerIndex  j)

quickSort(lowerIndex, j);

if (i  higherIndex)

quickSort(i, higherIndex);

}

private void swap(int i, int j) {

int temp = array[i];

array[i] = array[j];

array[j] = temp;

}

public static void main(String a[]){

quicksortdemo sorter = new quicksortdemo();

int[] input = {24,2,45,20,56,75,2,56,99,53,12};

sorter.sort(input);

for(int i:input){

System.out.print(i);

System.out.print(" ");

}

}

}

② 运行:

c:\java quicksortdemo

2 2 12 20 24 45 53 56 56 75 99

用JAVA实现快速排序算法?

本人特地给你编的代码

亲测

public class QuickSort {

public static int Partition(int a[],int p,int r){

int x=a[r-1];

int i=p-1;

int temp;

for(int j=p;j=r-1;j++){

if(a[j-1]=x){

// swap(a[j-1],a[i-1]);

i++;

temp=a[j-1];

a[j-1]=a[i-1];

a[i-1]=temp;

}

}

//swap(a[r-1,a[i+1-1]);

temp=a[r-1];

a[r-1]=a[i+1-1];

a[i+1-1]=temp;

return i+1;

}

public static void QuickSort(int a[],int p,int r){

if(pr){

int q=Partition(a,p,r);

QuickSort(a,p,q-1);

QuickSort(a,q+1,r);

}

}

public static void main(String[] stra){

int a[]={23,53,77,36,84,76,93,13,45,23};

QuickSort(a,1,10);

for (int i=1;i=10;i++)

System.out.println(a[i-1]);

}

}

举一个简单java快速排序的例子?

Java中的快速排序一个简单的例子

public class QuickSort {

public static void sort(Comparable[] data, int low, int high) {

// 枢纽元,一般以第一个元素为基准进行划分

Comparable pivotKey = data[low];

// 进行扫描的指针i,j;i从左边开始,j从右边开始

int i = low;

int j = high;

if (low high) {

// 从数组两端交替地向中间扫描

while (i j) {

while (i j data[j].compareTo(pivotKey) 0) {

j--; }

// end while

if (i j) {

// 比枢纽元素小的移动到左边

data[i] = data[j];

i++;

}

// end if

while (i j data[i].compareTo(pivotKey) 0) {

i++;

}

// end while

if (i j) {

// 比枢纽元素大的移动到右边

data[j] = data[i];

j--;

}

// end if

}

// end while

// 枢纽元素移动到正确位置

data[i] = pivotKey;

// 前半个子表递归排序

sort(data, low, i - 1);

// 后半个子表递归排序

sort(data, i + 1, high);

}

// end if

}

// end sort

public static void main(String[] args) {

// 在JDK1.5版本以上,基本数据类型可以自动装箱

// int,double等基本类型的包装类已实现了Comparable接口

Comparable[] c = { 4, 9, 23, 1, 45, 27, 5, 2 };

sort(c, 0, c.length - 1);

for (Comparable data : c) {

System.out.println(data);

}

}

}

真的是很服你,你把这个新建一个类放里面

在主方法里面这样写:

自己建个数组Comparable[] data,

定义参数int low, int high

QuickSort qs = new QuickSort();

qs.sort([] data, low, high);


文章题目:java实现快排的代码 java实现快排的代码是什么
分享路径:http://cxhlcq.com/article/hjdios.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部