記錄自己整理的幾個java排序代碼
public class Index {
public static void main(String[] args) {
#int[] a = { 1, 4, 5, 6, 8, 2, 3, 9, 6, };
// selectSort(a);
// bubleSort(a);
// insertSort(a);
// quickSort(a, 0, a.length - 1);
// quickSort(a, 0, a.length - 1);
shellSort(a) ;
for (int b : a) {
System.out.println(b);
}
}
// select sort,從後面的列表中選出最大的放到前面
public static void selectSort(int[] arr) {
for (int i = 0; i
for (int j = i + 1; j
if (arr[i] int tem = arr[ j];
arr[j] = arr[i];
arr[i] = tem;
}
}
}
}
// buble sort,子迴圈中,兩兩比較交換,形似冒泡,從而將大的數放到後面
public static void bubleSort(int[] arr) {
for (int i = 0; i
# for (int j = 0; j
if (arr[j] > arr[j + 1]) {
int tem = arr[j + 1];
arr[j + 1] = arr[j];
arr [j] = tem;
}
}
}
}
# // insert sort,假定前面的是有序的,子循環中,用目標元素和前面的比較,遇到大的就向前移動,遇到小的就插入該位置
public static void insertSort(int[] arr) {
for (int i = 1; i
int j = i;
int temp = arr[i];
while (j > 0) {
if (temp
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
j--;
}
}
}
# // quick sort,類似折半的思想,讓某個值左邊的都小於它,右邊的都大於它,然後兩邊遞歸
public static void quickSort(int[] arr, int low, int high) {
int start = low;
int end = high;
int key = arr[start];
while (end > start) {
while (end > start && arr[end] >= key) {
end --;
}
if (arr[end] int tem = arr[end];
arr[end] = arr[start];
arr [start] = tem;
}
while (start start++;
}
if (arr[start] > = key) {
int tem = arr[start];
arr[start] = arr[end];
arr[end] = tem;
}
quickSort(arr, low, start - 1);
if (end quickSort( arr, end + 1, high);
}
public static void shellSort(int[] arr) {
int d = arr.length / 2;
for (int i = 0; i for (int j = i; j if (arr [j] > arr[j + d]) {
int tem = arr[j];
arr[j] = arr[j + d];
arr[j + d] = tem ;
}
}
}
d = d / 2;
}
以上是java基本排序實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java實現“一次編寫,到處運行”通過編譯成字節碼並在Java虛擬機(JVM)上運行。 1)編寫Java代碼並編譯成字節碼。 2)字節碼在任何安裝了JVM的平台上運行。 3)使用Java原生接口(JNI)處理平台特定功能。儘管存在挑戰,如JVM一致性和平台特定庫的使用,但WORA大大提高了開發效率和部署靈活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)