演算法:快速排序,演算法:排序

來源:互聯網
上載者:User

演算法:快速排序,演算法:排序


快速排序演算法原理:見描述。

代碼如下:

package com.huan;import java.util.Arrays;import java.util.Random;public class QuickSort {public static void main(String[] args) {int[] data = new int[10];for (int i = 0; i < 10; i++){data[i] = new Random().nextInt(1000);}System.out.println(Arrays.toString(data));quicksort(data, 0, data.length - 1);System.out.println(Arrays.toString(data));}public static void quicksort(int[] data, int i, int j){if(i < j){int m = i, n = j;int k = data[i];while(i < j){while (i < j && data[j] > k){j--;}data[i] = data[j];while (i < j && data[i] < k){i++;}data[j] = data[i];}data[i] = k;quicksort(data, m, i-1);quicksort(data, i+1, n);}}}


編寫一個演算法,實現快速排序

/************************************************************************/
/* 快速排序 */
/************************************************************************/
void CommonALG::quickSort(float *arr, int left, int right)
{
int ltemp = left;
int rtemp = right;
float f = arr[(left + right) / 2];
float t = 0;
while(ltemp < rtemp)
{
while(arr[ltemp]<f)
++ltemp;
while(arr[rtemp]>f)
--rtemp;

if(ltemp<=rtemp)
{
t = arr[ltemp];
arr[ltemp] = arr[rtemp];
arr[rtemp] = t;
--rtemp;
++ltemp;
}
}

if(ltemp==rtemp)
ltemp++;
if(left<rtemp)
quickSort(arr,left,ltemp-1);
if(ltemp<right)
quickSort(arr,rtemp+1,right);
}
 
c++快速排序演算法代碼

你看百度百科吧,講得非常詳細:
baike.baidu.com/view/19016.htm
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.