快排java
⑴ 怎麼用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實現快速排序演算法
本人特地給你編的代碼
親測
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中的快速排序
快速排序是對冒泡排序的一種改進。它的基本思想是:通過一躺排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然後再按次方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。最壞情況的時間復雜度為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);
}
}
⑷ Java程序快速排序是怎樣的,舉個例子說明一下
publicclassQuickSort{
privatestaticvoidQuickSort(int[]array,intstart,intend){if(start<end){
intkey=array[start];//初始化保存基元
inti=start,j;//初始化i,j
for(j=start+1;j<=end;j++)
if(array[j]<key){//如果此處元素小於基元,則把此元素和i+1處元素交換,並將i加1,如大於或等於基元則繼續循環inttemp=array[j];
array[j]=array[i+1];
array[i+1]=temp;
i++;
}
}
array[start]=array[i];//交換i處元素和基元
array[i]=key;
QuickSort(array,start,i-1);//遞歸調用
QuickSort(array,i+1,end);
}
}
publicstaticvoidmain(String[]args){
int[]array=newint[]{11,213,134,44,77,78,23,43};
QuickSort(array,0,array.length-1);
for(inti=0;i<array.length;i++){
System.out.println((i+1)+"th:"+array[i]);
}
}
}
⑸ 求使用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實現
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數據結構中的快速排序方法
原理:
快速排序也是分治法思想的一種實現,他的思路是使數組中的每個元素與基準值(Pivot,通常是數組的首個值,A[0])比較,數組中比基準值小的放在基準值的左邊,形成左部;大的放在右邊,形成右部;接下來將左部和右部分別遞歸地執行上面的過程:選基準值,小的放在左邊,大的放在右邊。。。直到排序結束。
步驟:
1.找基準值,設Pivot = a[0]
2.分區(Partition):比基準值小的放左邊,大的放右邊,基準值(Pivot)放左部與右部的之間。
3.進行左部(a[0] - a[pivot-1])的遞歸,以及右部(a[pivot+1] - a[n-1])的遞歸,重復上述步驟。
排序效果:
⑼ 如何用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中快速排序的實現思路
快速排序法:快速排序法號稱是目前最優秀的演算法之一,實現思路是,將一個數組的排序問題看成是兩個小數組的排序問題,而每個小的數組又可以繼續看成更小的兩個數組,一直遞歸下去,直到數組長度大小最大為2