導航:首頁 > 編程語言 > 快排java

快排java

發布時間:2021-12-08 12:48:27

❶ 快速排序java實現

class Solution {
private void swap(int[] nums, int i, int j) {
if (i != j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

private int partition(int[] nums, int left, int right) {
int i = left - 1;
int pivot = nums[right];
for (int j = left; j < right; ++j) {
if (nums[j] < pivot) {
swap(nums, ++i, j);
}
}
swap(nums, ++i, right);
return i;
}

private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int middle = partition(nums, left, right);
quickSort(nums, left, middle - 1);
quickSort(nums, middle + 1, right);
}

public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
}

❷ 用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(p<r){
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實現快速排序

class Solution {
private void swap(int[] nums, int i, int j) {
if (i != j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

private int partition(int[] nums, int left, int right) {
int i = left - 1;
int pivot = nums[right];
for (int j = left; j < right; ++j) {
if (nums[j] < pivot) {
swap(nums, ++i, j);
}
}
swap(nums, ++i, right);
return i;
}

private void quickSort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int middle = partition(nums, left, right);
quickSort(nums, left, middle - 1);
quickSort(nums, middle + 1, right);
}

public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length - 1);
return nums;
}
}

❹ 怎麼用java實現快速排序

package com.xinhua.test2;
public class Student {
int sno;
String name;
double chiness;
double math;
double english;
double three_sco;
double all_s;
public Student(int sno, String name, double chiness, double math, double english, double three_sco) {
this.sno = sno;
this.name = name;
this.chiness = chiness;
this.math = math;
this.english = english;
this.three_sco = three_sco;
}
@Override
public String toString() {
return "學號:" + sno + ", 名字:" + name + ", 語文成績:" + chiness + ", 數學成績:" + math + ", 英語成績:"
+ english + "總成績:"+(chiness+math+english+three_sco);
}
public static void main(String[] args) {
Student A=new Student(1, "張三", 118, 145, 114.5, 198);
Student B=new Student(2, "李四", 130,110.5,100,210);
Student C=new Student(3, "王五",142.5,120,87.5,245.5);
System.out.println("學生列表信息為:");
System.out.println(A.toString());
System.out.println(B.toString());
System.out.println(C.toString());
//
double a_scoAll=A.chiness+A.math+A.english+A.three_sco;
A.all_s=a_scoAll;
double b_scoAll=B.chiness+B.math+B.english+B.three_sco;
B.all_s=b_scoAll;
double c_sclAll=C.chiness+C.math+C.english+C.three_sco;
C.all_s=c_sclAll;
Student[] s_s={A,B,C};
System.out.println("按總成績從大到小排序為");
Student temp;
for(int i=0;i<s_s.length;i++){
for(int j=1;j<s_s.length-i;j++){
if(s_s[j-1].all_s < s_s[j].all_s){
temp=s_s[j-1] ;
s_s[j-1] =s_s[j];
s_s[j]=temp;
}
}
}
for(int i=0;i<s_s.length;i++){
System.out.println("第"+(i+1)+"名為:"+s_s[i].toString());
}
}
}

❺ 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();
}
}

❻ 快速排序 java版的 謝謝了!

你的不是不能運行.. 而是有死循環
sort()方法中的語句
while (i<j && arr[i]<povit );
此while循環沒有改變循環步長的代碼,所以執行到此時,程序會進入死循環,
根據你代碼的大致思路,把這個";"去掉就可以了
(還有你下面的while循環也要去掉第一個";" while循環是不用加";"的 與do while循環不同,注意語法)

❼ java快速排序

public class demo11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
QuickSort quick = new QuickSort();
int arr[] = { 4, 2, 6, 1, 5, 0, 8, -1 };
quick.Sort(arr, 0, arr.length-1);
for(int i:arr)
System.out.println(i);
}
}

class QuickSort {
public void Sort(int arr[], int left, int right) {
if(left == right) return;
System.out.println("sort:");
for(int i=left;i<=right;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println("");
int inser = arr[left];
int temp;
int le = left;
int re = right ;
while (le < re) {
while (le < re && inser < arr[re]) {
re--;
}
if(re==le) break;
temp = arr[re];
arr[re] = arr[le];
arr[le] = temp;
le++;
while (le < re && inser > arr[le]) {
le++;
}
if(re==le) break;
temp = arr[re];
arr[re] = arr[le];
arr[le] = temp;
re--;
}
arr[le]=inser;
if(le>left)
Sort(arr, left, le-1);
if(re<right)
Sort(arr,le+1,right);
}
}

排序的思路是:取數組的第一個數(arr[left])為參考值(inser),將比參考值(inser)小的數全部放到參考值左邊,比參考值(inser)大的全部放到參考值右邊。然後用相同的方法對參考值右邊和左邊的數組進行排序。

❽ 如何用java實現快速排序,簡答講解下原理

快速排序思想:
通過對數據元素集合Rn 進行一趟排序劃分出獨立的兩個部分。其中一個部分的關鍵字比另一部分的關鍵字小。然後再分別對兩個部分的關鍵字進行一趟排序,直到獨立的元素只有一個,此時整個元素集合有序。
快速排序的過程,對一個元素集合R[ low ... high ] ,首先取一個數(一般是R[low] )做參照 , 以R[low]為基準重新排列所有的元素。
所有比R[low]小的放前面,所有比R[low] 大的放後面,然後以R[low]為分界,對R[low ... high] 劃分為兩個子集和,再做劃分。直到low >= high 。
比如:對R={37, 40, 38, 42, 461, 5, 7, 9, 12}進行一趟快速排序的過程如下(注:下面描述的內容中元素下表從 0 開始):
開始選取基準 base = 37,初始位置下表 low = 0 , high = 8 , 從high=8,開始如果R[8] < base , 將high位置中的內容寫入到R[low]中, 將high位置空出來, low = low +1 ;
從low開始探測,由於low=1 , R[low] > base ,所以將R[low]寫入到R[high] , high = high -1 ;
檢測到low < high ,所以第一趟快速排序仍需繼續:
此時low=1,high=7,因為 R[high] < base ,所以將 R[high] 寫入到到R[low]中,low = low + 1;
從low開始探測,low = 2 , R[low] >base ,所以講R[low]寫入到R[high],high=high-1;
繼續檢測到 low 小於high
此時low=2,high=6,同理R[high] < base ,將R[high] 寫入到R[low]中,low=low+1;
從low繼續探測,low = 3 , high=6 , R[low] > base , 將R[low]寫入到R[high]中,high = high-1;
繼續探測到low小於high
此時low=3,high=5,同理R[high] < base,將R[high]寫入到R[low]中,low = low +1;
從low繼續探測,low = 4,high=5,由於R[low] > base , 將R[low]寫入到R[high]中,high = high -1 ;
此時探測到low == high == 4 ;該位置即是base所在的位置,將base寫入到該位置中.
然後再對子序列Rs1 = {12,9,7,5} 和 Rs2={461,42,38,40}做一趟快速排序,直到Rsi中只有一個元素,或沒有元素。
快速排序的Java實現:
private static boolean isEmpty(int[] n) {

return n == null || n.length == 0;
}

// ///////////////////////////////////////////////////
/**
* 快速排序演算法思想——挖坑填數方法:
*
* @param n 待排序的數組
*/
public static void quickSort(int[] n) {
if (isEmpty(n))
return;
quickSort(n, 0, n.length - 1);
}

public static void quickSort(int[] n, int l, int h) {
if (isEmpty(n))
return;
if (l < h) {
int pivot = partion(n, l, h);
quickSort(n, l, pivot - 1);
quickSort(n, pivot + 1, h);
}
}

private static int partion(int[] n, int start, int end) {
int tmp = n[start];
while (start < end) {
while (n[end] >= tmp && start < end)
end--;
if (start < end) {
n[start++] = n[end];
}
while (n[start] < tmp && start < end)
start++;
if (start < end) {
n[end--] = n[start];
}
}
n[start] = tmp;
return start;
}
在代碼中有這樣一個函數:
public static void quickSortSwap(int[] n, int l, int h)
該函數可以實現,元素集合中特定的 l 到 h 位置間的數據元素進行排序。

❾ 求使用java實現的快排演算法

① 代碼:

publicclassquicksortdemo{

privateintarray[];
privateintlength;

publicvoidsort(int[]inputArr){

if(inputArr==null||inputArr.length==0){
return;
}
this.array=inputArr;
length=inputArr.length;
quickSort(0,length-1);
}

privatevoidquickSort(intlowerIndex,inthigherIndex){

inti=lowerIndex;
intj=higherIndex;
//calculatepivotnumber
intpivot=array[lowerIndex+(higherIndex-lowerIndex)/2];
//Divideintotwoarrays
while(i<=j){
while(array[i]<pivot){
i++;
}
while(array[j]>pivot){
j--;
}
if(i<=j){
swap(i,j);
i++;
j--;
}
}
//callquickSort()methodrecursively
if(lowerIndex<j)
quickSort(lowerIndex,j);
if(i<higherIndex)
quickSort(i,higherIndex);
}

privatevoidswap(inti,intj){
inttemp=array[i];
array[i]=array[j];
array[j]=temp;
}

publicstaticvoidmain(Stringa[]){

quicksortdemosorter=newquicksortdemo();
int[]input={24,2,45,20,56,75,2,56,99,53,12};
sorter.sort(input);
for(inti:input){
System.out.print(i);
System.out.print("");
}
}
}

② 運行:

c:>javaquicksortdemo
22122024455356567599

❿ Java快速排序

原理:
快速排序也是分治法思想的一種實現,他的思路是使數組中的每個元素與基準值(Pivot,通常是數組的首個值,A[0])比較,數組中比基準值小的放在基準值的左邊,形成左部;大的放在右邊,形成右部;接下來將左部和右部分別遞歸地執行上面的過程:選基準值,小的放在左邊,大的放在右邊。。。直到排序結束。
步驟:
1.找基準值,設Pivot = a[0]
2.分區(Partition):比基準值小的放左邊,大的放右邊,基準值(Pivot)放左部與右部的之間。
3.進行左部(a[0] - a[pivot-1])的遞歸,以及右部(a[pivot+1] - a[n-1])的遞歸,重復上述步驟

閱讀全文

與快排java相關的資料

熱點內容
狗打籃球的電影 瀏覽:153
類似一路向西的電影有哪些 瀏覽:191
app如何實現霸屏 瀏覽:72
空間信息大數據 瀏覽:88
裕豐園學校的編程怎麼樣 瀏覽:980
農家的小地主叫蘇木蘭 瀏覽:833
如何看待產品合作中用戶數據的價值 瀏覽:909
義大利丁巴度的電影慾望之欲 瀏覽:415
win10u盤自動運行程序嗎 瀏覽:961
韓國電影有紋身的男演員 瀏覽:192
win10中國政府版 瀏覽:534
win1064位32位區別 瀏覽:341
有好的網址嗎?推薦一下在線看片 瀏覽:806
蘋果6s手機桌面設置在哪裡設置密碼 瀏覽:375
無限流中的系統與奶媽隊 瀏覽:29
歐美電影學生愛上女快遞員 瀏覽:360
匯編實驗循環程序實驗 瀏覽:574
武俠肉版小說 瀏覽:428
蘋果市值2014 瀏覽:48
根據文件名提取文件軟體 瀏覽:211

友情鏈接