本人特地給你編的代碼
10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有盂縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
親測(cè)
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]);
}
}
.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} 排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過(guò)程中需要訪問(wèn)外存。常見(jiàn)的內(nèi)部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數(shù)排序等。以下是快速排序算法:
快速排序是由東尼·霍爾所發(fā)展的一種排序算法。在平均狀況下,排序 n 個(gè)項(xiàng)目要 Ο(nlogn) 次比較。在最壞狀況下則需要 Ο(n2) 次比較,但這種狀況并不常見(jiàn)。事實(shí)上,快速排序通常明顯比其他 Ο(nlogn) 算法更快,因?yàn)樗膬?nèi)部循環(huán)(inner loop)可以在大部分的架構(gòu)上很有效率地被實(shí)現(xiàn)出來(lái)。
快速排序使用分治法(Divide and conquer)策略來(lái)把一個(gè)串行(list)分為兩個(gè)子串行(sub-lists)。
快速排序又是一種分而治之思想在排序算法上的典型應(yīng)用。本質(zhì)上來(lái)看,快速排序應(yīng)該算是在冒泡排序基礎(chǔ)上的遞歸分治法。
快速排序的名字起的是簡(jiǎn)單粗暴,因?yàn)橐宦?tīng)到這個(gè)名字你就知道它存在的意義,就是快,而且效率高!它是處理大數(shù)據(jù)最快的排序算法之一了。雖然 Worst Case 的時(shí)間復(fù)雜度達(dá)到了 O(n?),但是人家就是優(yōu)秀,在大多數(shù)情況下都比平均時(shí)間復(fù)雜度為 O(n logn) 的排序算法表現(xiàn)要更好,可是這是為什么呢,我也不知道。好在我的強(qiáng)迫癥又犯了,查了 N 多資料終于在《算法藝術(shù)與信息學(xué)競(jìng)賽》上找到了滿(mǎn)意的答案:
快速排序的最壞運(yùn)行情況是 O(n?),比如說(shuō)順序數(shù)列的快排。但它的平攤期望時(shí)間是 O(nlogn),且 O(nlogn) 記號(hào)中隱含的常數(shù)因子很小,比復(fù)雜度穩(wěn)定等于 O(nlogn) 的歸并排序要小很多。所以,對(duì)絕大多數(shù)順序性較弱的隨機(jī)數(shù)列而言,快速排序總是優(yōu)于歸并排序。
1. 算法步驟
從數(shù)列中挑出一個(gè)元素,稱(chēng)為 "基準(zhǔn)"(pivot);
重新排序數(shù)列,所有元素比基準(zhǔn)值小的擺放在基準(zhǔn)前面,所有元素比基準(zhǔn)值大的擺在基準(zhǔn)的后面(相同的數(shù)可以到任一邊)。在這個(gè)分區(qū)退出之后,該基準(zhǔn)就處于數(shù)列的中間位置。這個(gè)稱(chēng)為分區(qū)(partition)操作;
遞歸地(recursive)把小于基準(zhǔn)值元素的子數(shù)列和大于基準(zhǔn)值元素的子數(shù)列排序;
2. 動(dòng)圖演示
代碼實(shí)現(xiàn) JavaScript 實(shí)例 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
快速排序:
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;
}
}
改進(jìn)后的快速排序:
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);
}
}
}
}
一趟快速怕序的具體做法是:附設(shè)兩個(gè)指針low和high,他們的初值分別為low和high,設(shè)樞軸記錄的關(guān)鍵字為privotkey,則首先從high所指位置向前搜索找到第一個(gè)關(guān)鍵字小于pivotkey的記錄和樞軸記錄互相交換,然后從low所指向的位置起向后搜索,找到第一個(gè)關(guān)鍵字大于privotkey的記錄和樞軸記錄互相交換,重復(fù)這兩步直至low==high位置.
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class 快速排序_1 {
public static void main(String[] args) throws InterruptedException {
int test[] = {15,23,56,7,13,52,20,7};
new 快速排序_1().qSort(test, 0, test.length-1);
for(int k:test) System.out.println(k);
}
public void qSort(int []array,int low,int high){
if(low
int privot=partition(array,low,high);
qSort(array,low,privot-1);
qSort(array,privot+1,high);
}
}
public int partition(int [] array,int low,int high){
/**
* 選擇 low位置 作為曲軸(支點(diǎn))
*/
int pivot=array[low];
int temp=0;
/**
* 如果 low
*/
while(low
/**
* 先從 high端 開(kāi)始判斷
*/
while(low=pivot) high--;
/**
* 進(jìn)行 置換操作
*/
if(low
array[low]=array[high];
low++;
}
/**
* 從 low 端判斷
*/
while(low
/**
* 進(jìn)行 置換操作
*/
if(low
array[high]=array[low];
high--;
}
}
array[low]=pivot;
return low;
}
}
① 代碼:
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("?");
}
}
}
② 運(yùn)行:
c:\java?quicksortdemo
2?2?12?20?24?45?53?56?56?75?99
分享名稱(chēng):快速排序算法代碼java 簡(jiǎn)單的排序算法代碼
轉(zhuǎn)載源于:http://www.rwnh.cn/article4/dooccoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、做網(wǎng)站、微信公眾號(hào)、App開(kāi)發(fā)、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容