Java實(shí)現(xiàn)常見的排序算法代碼實(shí)例
Java排序算法代碼實(shí)現(xiàn)
以前學(xué)習(xí)的各種排序算法都忘光了,也就記住了個(gè)冒泡算法,最近又學(xué)習(xí)了一下各個(gè)排序算法的思路,所以就按照思路實(shí)現(xiàn)了以下幾個(gè)排序算法(冒泡排序、直接插入排序、直接選擇排序、快速排序),方便日后用到,特此記錄一下,以下為具體的實(shí)現(xiàn)。
代碼
public class SortAlgorithm {
public static void main(String[] args) {
int[] nums = {6, 1, 4, 2, 3, 9, 5, 8, 7};
// 冒泡排序
bubbleSort(nums);
// 直接插入排序
insertSort(nums);
// 直接選擇排序
directSelection(nums);
// 快速選擇排序
int[] numbers = quickSort(nums, 0, nums.length -1);
System.out.print("快速選擇排序: ");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
}
public static void bubbleSort (int[] nums) {
int temp;
for (int i= 0;i <= nums.length; i++) {
for (int j=i; j< nums.length-1; j++) {
if (nums[j+1] < nums[j]) {
temp = nums[j+1];
nums[j+1] = nums[j];
nums[j] = temp;
}
}
}
System.out.print("冒泡排序: ");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
}
public static void insertSort(int[] nums) {
// 存放有序的數(shù)組
int[] numbers = new int[nums.length];
int index = 0;
for(int i=0;i< nums.length;i++) {
// 當(dāng)前待插入元素
int current = nums[i];
// 存放臨時(shí)變量
int prefix = 0;
int temp = 0;
// 未找到插入點(diǎn)
boolean suc = false;
// 插入第一個(gè)元素
if (i == 0) {
numbers[index] = current;
} else {
for(int j = 0; j < numbers.length; j++) {
// 找到待插入的點(diǎn)
if (numbers[j] > current || suc) {
// 未找到插入點(diǎn)
if (!suc) {
index = j;
suc = true;
// 記錄下當(dāng)前節(jié)點(diǎn)
prefix = numbers[j];
} else {
temp = numbers[j];
// 將前一個(gè)節(jié)點(diǎn)的值賦值給當(dāng)前節(jié)點(diǎn)
numbers[j] = prefix;
prefix = temp;
}
}
}
// 若查找到插入節(jié)點(diǎn)
if (suc) {
numbers[index] = current;
} else {
numbers[i] = nums[i];
}
}
}
System.out.print("直接插入排序: ");
for (int number : numbers) {
System.out.print(number + " ");
}
System.out.println();
}
public static void directSelection(int[] nums) {
int index = 0;
for(int i=0; i < nums.length; i++) {
// 默認(rèn)不需要交換
boolean exchange = false;
int temp = nums[i];
for(int j=i; j< nums.length; j++) {
if (nums[j] < temp) {
// 尋找最小的值
temp = nums[j];
// 記錄最小值的下標(biāo)
index = j;
// 需要交換
exchange = true;
}
}
// 需要交換,則進(jìn)行交換
if (exchange) {
nums[index] = nums[i];
// 將第i輪較小的值賦值給下標(biāo)為i的節(jié)點(diǎn)
nums[i] = temp;
}
}
System.out.print("直接選擇排序: ");
for (int number : nums) {
System.out.print(number + " ");
}
System.out.println();
}
public static int[] quickSort(int[] nums, int start, int end) {
// 定義基準(zhǔn)
int k = nums[start];
// 左側(cè)定位到的下標(biāo)
int left = start;
// 右側(cè)定位到的下標(biāo)
int right = end;
// 存放臨時(shí)數(shù)據(jù)
int temp;
boolean compareLeft = false;
while (left != right) {
if (compareLeft) {
if (nums[left] > k) {
temp = nums[right];
nums[right] = nums[left];
nums[left] = temp;
// 基準(zhǔn)被踢到左側(cè),需要和左側(cè)數(shù)據(jù)比較
compareLeft = false;
} else {
left ++;
}
} else {
if (nums[right] < k) {
temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
// 基準(zhǔn)被踢到右側(cè),需要和左側(cè)數(shù)據(jù)比較
compareLeft = true;
} else {
right --;
}
}
}
if (left -1 > start) {
quickSort(nums, start, left -1);
}
if (right + 1 < end) {
quickSort(nums, right+ 1, end);
}
return nums;
}
}執(zhí)行結(jié)果如下:
冒泡排序: 1 2 3 4 5 6 7 8 9
直接插入排序: 1 2 3 4 5 6 7 8 9
直接選擇排序: 1 2 3 4 5 6 7 8 9
快速選擇排序: 1 2 3 4 5 6 7 8 9
到此這篇關(guān)于Java實(shí)現(xiàn)常見的排序算法代碼實(shí)例的文章就介紹到這了,更多相關(guān)Java排序算法代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用javax.sound實(shí)現(xiàn)簡(jiǎn)單音頻播放
這篇文章主要為大家詳細(xì)介紹了使用javax.sound實(shí)現(xiàn)簡(jiǎn)單音頻播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)多線程在線聊天
這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)多線程在線聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java Stream map, Collectors(toMap, toLis
這篇文章主要介紹了Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
Java中將base64編碼字符串轉(zhuǎn)換為圖片的代碼
這篇文章主要介紹了Java中將base64編碼字符串轉(zhuǎn)換為圖片,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03

