導航:首頁 > 源碼編譯 > java快速演算法

java快速演算法

發布時間:2022-04-25 06:16:28

1. java中快速排序的演算法舉個例子

package person.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
* class name: RapidSort
* description: Java快速排序法:數組和集合
* @author Jr
*
*/
public class RapidSort {
private Random ran = new Random(); // 聲明一個全局變數ran,用來隨機生成整數

/**
* method name: sortArray
* description: 對數組的快速排序,只能用於int[]類型的數組
* @return
*/
private void sortArray() {
int[] array = new int[10]; // 聲明數組長度為10
for (int i = 0 ; i < array.length; i++) {
array[i] = ran.nextInt(10) + 1; // 數組賦值
}
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}

/**
* method name: sortList
* description: 對集合的快速排序,可以用於List<Object>類型數組,
* 隱含意思就是對所有類型數組都適用
* @return
*/
private void sortList() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0 ; i < 10; i++) {
list.add(ran.nextInt(10) + 1); // 給集合賦10個值
}
Collections.sort(list);
System.out.println(list);
}

public static void main(String[] args) {
RapidSort rs = new RapidSort();
rs.sortArray();
rs.sortList();
}
}

2. Java如何用最有效的方法計算22*8

Java乘法運算還有位運算符也可以完成乘法運算
<<左移運算符 ,左移一位,相當於乘2
>>右移運算符 ,右移一位,相當於除2
22 乘 8=11 乘 16, 16是2的4次方,就是通過位運算符,11向左移4位

22 乘 8最有效的計算方法為: 11左移4位 及11<<4

你明白了嗎?

3. 求java快速排序演算法,最好是示例的那種,感激不盡

public static void main(String[] args) {
int[] arr = {1,4,7,2,5,8,3,6,9};
quickSort(arr);
}

public static void quickSort(int[] a) {
quickSort(a, 0, a.length - 1);
}

private static void quickSort(int[] a, int start, int end) {
int left = start;
int right = end - 1;
int pivot = a[end];

while (left < right) {
if (a[left] <= pivot) {
left++;
continue;
}
if (a[right] > pivot) {
right--;
continue;
}
swap(a, left++, right);
}

if (a[left] < pivot) {
left++;
}
swap(a, left, end);

if(left - 1 > start) {
quickSort(a, start, left - 1);
}
if(left + 1 < end) {
quickSort(a, left + 1, end);
}
}

4. 關於JAVA快速排序演算法問題,請大神指教啊!!!

//我按著你的思路寫的,自己看看吧,我運行過,結果正確
public class QuickSort {
public static void main(String[] args) {
int[] array = new int[]{2,5,3,8,7,0,1,4,6,9,10};
int left = 0;
int right = array.length-1;
qsort(array,left,right);
for(int index:array){
System.out.print(index+" ");
}
}
public static void qsort(int[] array,int left,int right){
int p;
if(left<right){
p = partition(array,left,right);
qsort(array,left,p-1);
qsort(array,p+1,right);
}
}
public static int partition(int[] arr,int left,int right){
int temp_val;
int temp_r, temp_l, temp_m;
temp_r = right;
temp_l = left;
temp_m = left;
boolean flag = true;
while(temp_l < temp_r){
if(flag){
if(arr[temp_m] > arr[temp_r]){
temp_val = arr[temp_m];
arr[temp_m] = arr[temp_r];
arr[temp_r] = temp_val;
temp_m = temp_r;
flag = false;
}
temp_r --;
}else{
if(arr[temp_m] < arr[temp_l]){
temp_val = arr[temp_m];
arr[temp_m] = arr[temp_l];
arr[temp_l] = temp_val;
temp_m = temp_l;
flag = true;
}
temp_l ++;
}
}
return temp_m;
}
}

5. 那位大大能詳細的講解一下JAVA中的快速排序

快速排序是對冒泡排序的一種改進。它的基本思想是:通過一躺排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然後再按次方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。最壞情況的時間復雜度為O(n2),最好情況時間復雜度為O(nlog2n)。

另外 java沒指針概念 可以認為是句柄
假設要排序的數組是A[1]……A[N],首先任意選取一個數據(通常選用第一個數據)作為關鍵數據,然後將所有比它的數都放到它前面,所有比它大的數都放到它後面,這個過程稱為一躺快速排序。一趟快速排序的演算法是:

1)、設置兩個變數I、J,排序開始的時候I:=1,J:=N;

2)以第一個數組元素作為關鍵數據,賦值給X,即X:=A[1];

3)、從J開始向前搜索,即由後開始向前搜索(J:=J-1),找到第一個小於X的值,兩者交換;

4)、從I開始向後搜索,即由前開始向後搜索(I:=I+1),找到第一個大於X的值,兩者交換;

5)、重復第3、4步,直到I=J;

例如:待排序的數組A的值分別是:(初始關鍵數據X:=49)

A[1] A[2] A[3] A[4] A[5] A[6] A[7]:

49 38 65 97 76 13 27

進行第一次交換後: 27 38 65 97 76 13 49

( 按照演算法的第三步從後面開始找)

進行第二次交換後: 27 38 49 97 76 13 65

( 按照演算法的第四步從前面開始找>X的值,65>49,兩者交換,此時I:=3 )

進行第三次交換後: 27 38 13 97 76 49 65

( 按照演算法的第五步將又一次執行演算法的第三步從後開始找)

進行第四次交換後: 27 38 13 49 76 97 65

( 按照演算法的第四步從前面開始找大於X的值,97>49,兩者交換,此時J:=4 )

此時再執行第三步的時候就發現I=J,從而結束一躺快速排序,那麼經過一躺快速排序之後的結果是:27 38 13 49 76 97 65,即所以大於49的數全部在49的後面,所以小於49的數全部在49的前面。

快速排序就是遞歸調用此過程——在以49為中點分割這個數據序列,分別對前面一部分和後面一部分進行類似的快速排序,從而完成全部數據序列的快速排序,最後把此數據序列變成一個有序的序列,根據這種思想對於上述數組A的快速排序的全過程如圖6所示:

初始狀態 {49 38 65 97 76 13 27}

進行一次快速排序之後劃分為 {27 38 13} 49 {76 97 65}

分別對前後兩部分進行快速排序 {13} 27 {38}

結束 結束 {49 65} 76 {97}

49 {65} 結束

結束//下面是一個示例,哪位給說說快速排序法的原理,下面的示例中指針和上下標移動我看不太懂,
public class QuickSort {
/**主方法*/
public static void main(String[] args) {
//聲明數組
int[] nums = {27, 8, 57, 9, 23, 41, 65, 19, 0, 1, 2, 4, 5};
//應用快速排序方法
quickSort(nums, 0, nums.length-1);
//顯示排序後的數組
for(int i = 0; i < nums.length; ++i) {
System.out.print(nums[i] + ",");
}
System.out.println("");
}

/**快速排序方法*/
public static void quickSort(int[] a, int lo0, int hi0) {
int lo = lo0;
int hi = hi0;

if (lo >= hi)
return;

//確定指針方向的邏輯變數
boolean transfer=true;

while (lo != hi) {
if (a[lo] > a[hi]) {
//交換數字
int temp = a[lo];
a[lo] = a[hi];
a[hi] = temp;
//決定下標移動,還是上標移動
transfer = (transfer == true) ? false : true;
}

//將指針向前或者向後移動
if(transfer)
hi--;
else
lo++;

//顯示每一次指針移動的數組數字的變化
/*for(int i = 0; i < a.length; ++i) {
System.out.print(a[i] + ",");
}
System.out.print(" (lo,hi) = " + "(" + lo + "," + hi + ")");
System.out.println("");*/
}

//將數組分開兩半,確定每個數字的正確位置
lo--;
hi++;
quickSort(a, lo0, lo);
quickSort(a, hi, hi0);
}
}

6. 用JAVA實現快速排序演算法

public void quickSort(int left,int right,int a[])
{
int l=left;
int r=right;
int pivot=a[(l+r)/2];//轉軸數
int temp=0;
while(l<r)
{
while(a[l]<pivot)l++;
while(a[r]>pivot)r--;

if(l>=r)break;

temp=a[l];
a[l]=a[r];
a[r]=temp;

if(a[l]==pivot)r--;
if(a[r]==pivot)l++;
}
if(l==r)
{
l++;
r--;
}
if(left<r)quickSort(left,r,a);
if(right>l)quickSort(l,right,a);
}

7. java演算法有哪些分別

您好:

java中的演算法,常見的有:遞歸、迭代、查找、排序(包含冒泡排序、選擇排序、插入排序、快速排序四種) 等,演算法有很多,一般數據結構中涉及到的都可以用java語言實現。

舉幾個例子:

1.遞歸的例子:

不一一舉例,僅供參考!

8. java快速排序演算法樞紐元的問題

package Utils.Sort; /** * 快速排序,要求待排序的數組必須實現 Comparable 介面 */ public class QuickSort implements SortStrategy { private static final int CUTOFF = 3; // 當元素數大於此值時採用快速排序 /** * 利用快速排序演算法對數組 obj 進行排序, 要求待排序的數組必須實現了 Comparable 介面 */ public void sort(Comparable[] obj) { if (obj == null) { throw new NullPointerException("The argument can not be null!"); } quickSort(obj, 0, obj.length - 1); } /** * 對數組 obj 快速排序 *@param obj 待排序的數組 *@param left 數組的下界 *@param right 數組的上界 */ private void quickSort(Comparable[] obj, int left, int right) { if (left + CUTOFF > right) { SortStrategy ss = new ChooseSort(); ss.sort(obj); } else { // 找出樞軸點,並將它放在數組最後面的位置 pivot(obj, left, right); int i = left, j = right - 1; Comparable tmp = null; while (true) { // 將 i, j 分別移到大於 / 小於樞紐值的位置 // 因為數組的第一個和倒數第二個元素分別小於和大於樞紐元, 所以不會發生數組越界 while (obj[++i].compareTo(obj[right - 1]) < 0) {} while (obj[--j].compareTo(obj[right - 1]) > 0) {} // 交換 if (i < j) { tmp = obj[i]; obj[i] = obj[j]; obj[j] = tmp; } else break; } // 將樞紐值與 i 指向的值交換 tmp = obj[i]; obj[i] = obj[right - 1]; obj[right - 1] = tmp; // 對樞紐值左側和右側數組繼續進行快速排序 quickSort(obj, left, i - 1); quickSort(obj, i + 1, right); } } /** * 在數組 obj 中選取樞紐元,選取方法為取數組第一個、 中間一個、最後一個元素中中間的一個。 將樞紐元置於倒數第二個位置, 三個中最大的放在數組最後一個位置,最小的放在第一個位置 *@param obj 要選擇樞紐元的數組 *@param left 數組的下界 *@param right 數組的上界 */ private void pivot(Comparable[] obj, int left, int right) { int center = (left + right) / 2; Comparable tmp = null; if (obj[left].compareTo(obj[ center]) > 0) { tmp = obj[left]; obj[left] = obj[center]; obj[center] = tmp; } if (obj[left].compareTo(obj[ right]) > 0) { tmp = obj[left]; obj[left] = obj[right]; obj[right] = tmp; } if (obj[center].compareTo(obj[ right]) > 0) { tmp = obj[center]; obj[center] = obj[right]; obj[center] = tmp; } // 將樞紐元置於數組的倒數第二個 tmp = obj[center]; obj[center] = obj[right - 1]; obj[right - 1] = tmp; } }

閱讀全文

與java快速演算法相關的資料

熱點內容
亞馬遜雲伺服器的選擇 瀏覽:810
單片機頻率發生器 瀏覽:732
備份與加密 瀏覽:623
用什麼app可以看論壇 瀏覽:52
javajdbcmysql連接 瀏覽:473
製作linux交叉編譯工具鏈 瀏覽:751
編程負數除以正數 瀏覽:512
app和aso有什麼區別 瀏覽:326
手機vmap是什麼文件夾 瀏覽:36
塔科夫鎖服如何選擇伺服器 瀏覽:290
消費者生產者問題java 瀏覽:61
程序員筱柒顧默結婚的時候 瀏覽:578
安卓截長屏怎麼弄 瀏覽:475
優信辦理解壓手續怎麼那麼慢 瀏覽:605
私有雲伺服器一體機安全嗎 瀏覽:430
python的tk界面禁用滑鼠 瀏覽:186
怎麼看伺服器mac地址 瀏覽:291
安卓如何將圖鏡像翻轉 瀏覽:325
操作系統設計與實現pdf 瀏覽:547
長虹空調遙控什麼app 瀏覽:739