您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java常見排序算法怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java常見排序算法怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
匯總:
每輪循環(huán)確定最值;
public void bubbleSort(int[] nums){ int temp; boolean isSort = false; //優(yōu)化,發(fā)現(xiàn)排序好就退出 for (int i = 0; i < nums.length-1; i++) { for (int j = 0; j < nums.length-1-i; j++) { //每次排序后能確定較大值 if(nums[j] > nums[j+1]){ isSort = true; temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } if(!isSort){ return; } else { isSort = false; } } }
每次選出最值,再交換到邊上;
public void selectSort(int[] nums){ for (int i = 0; i < nums.length-1; i++) { int index = i; int minNum = nums[i]; for (int j = i+1; j < nums.length; j++) { if(nums[j] < minNum){ minNum = nums[j]; index = j; } } if(index != i){ nums[index] = nums[i]; nums[i] = minNum; } } }
對循環(huán)的每個(gè)數(shù)找到屬于自己的位置插入;
public void insertionSort(int[] nums){ for (int i = 1; i < nums.length; i++) { int j = i; int insertNum = nums[i]; while(j-1 >= 0 && nums[j-1] > insertNum){ nums[j] = nums[j-1]; j--; } nums[j] = insertNum; } }
選一個(gè)基本值,小于它的放一邊,大于它的放另一邊;
public void quickSortDfs(int[] nums, int left, int right){ if(left > right){ return; } int l = left; int r = right; int baseNum = nums[left]; while(l < r){ //必須右邊先走 while(nums[r] >= baseNum && l < r){ r--; } while(nums[l] <= baseNum && l < r){ l++; } int temp = nums[l]; nums[l] = nums[r]; nums[r] = temp; } nums[left] = nums[l]; nums[l] = baseNum; quickSortDfs(nums, left, r-1); quickSortDfs(nums, l+1, right); }
分治算法;
//歸 public void mergeSortDfs(int[] nums, int l, int r){ if(l >= r){ return; } int m = (l+r)/2; mergeSortDfs(nums, l, m); mergeSortDfs(nums, m+1, r); merge(nums, l, m, r); } //并 private void merge(int[] nums, int left, int mid, int right){ int[] temp = new int[right-left+1]; int l = left; int m = mid+1; int i = 0; while(l <= mid && m <= right){ if(nums[l] < nums[m]){ temp[i++] = nums[l++]; } else { temp[i++] = nums[m++]; } } while(l <= mid){ temp[i++] = nums[l++]; } while(m <= right){ temp[i++] = nums[m++]; } System.arraycopy(temp, 0, nums, left, temp.length); }
引入步長減少數(shù)字交換次數(shù)提高效率;
public void shellBubbleSort(int[] nums){ for (int step = nums.length/2; step > 0 ; step /= 2) { for (int i = step; i < nums.length; i++) { for (int j = i-step; j >= 0; j -= step) { if(nums[j] > nums[j+step]){ int temp = nums[j]; nums[j] = nums[j+step]; nums[j+step] = temp; } } } } }
public void shellInsertSort(int[] nums){ for (int step = nums.length/2; step > 0; step /= 2) { for (int i = step; i < nums.length; i++) { int j = i; int insertNum = nums[i]; while(j-step >= 0 && nums[j-step] > insertNum){ nums[j] = nums[j-step]; j-=step; } nums[j] = insertNum; } } }
大頂堆實(shí)現(xiàn)升序,每次將最大值移到堆的最后一個(gè)位置上;
public void heapSort2(int[] nums) { for(int i = nums.length/2-1; i >= 0; i--){ sift(nums, i, nums.length); } for (int i = nums.length-1; i > 0; i--) { int temp = nums[0]; nums[0] = nums[i]; nums[i] = temp; sift(nums, 0, i); } } private void sift(int[] nums, int parent, int len) { int value = nums[parent]; for (int child = 2*parent +1; child < len; child = child*2 +1) { if(child+1 < len && nums[child+1] > nums[child]){ child++; } if(nums[child] > value){ nums[parent] = nums[child]; parent = child; } else { break; } } nums[parent] = value; }
按順序統(tǒng)計(jì)每個(gè)數(shù)出現(xiàn)次數(shù);
public void countSort(int[] nums){ int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for(int num : nums){ max = Math.max(max, num); min = Math.min(min, num); } int[] countMap = new int[max-min+1]; for(int num : nums){ countMap[num-min]++; } int i = 0; int j = 0; while(i < nums.length && j < countMap.length){ if(countMap[j] > 0){ nums[i] = j+min; i++; countMap[j]--; } else { j++; } } }
類似計(jì)數(shù)排序,不同點(diǎn)在于統(tǒng)計(jì)的是某個(gè)區(qū)間(桶)里的數(shù);
public void bucketSort(int[] nums){ int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for(int num : nums){ max = Math.max(max, num); min = Math.min(min, num); } int bucketCount = (max-min)/nums.length+1; List<List<Integer>> bucketList = new ArrayList<>(); for (int i = 0; i < bucketCount; i++) { bucketList.add(new ArrayList<>()); } for(int num : nums){ int index = (num-min)/nums.length; bucketList.get(index).add(num); } for(List<Integer> bucket : bucketList){ Collections.sort(bucket); } int j = 0; for(List<Integer> bucket : bucketList){ for(int num : bucket){ nums[j] = num; j++; } } }
按個(gè)、十、百位依次歸類排序;
public void radixSort(int[] nums){ int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int num : nums) { min = Math.min(min, num); max = Math.max(max, num); } for (int i = 0; i < nums.length; i++) { nums[i] -= min; } max -= min; int maxLen = (max+"").length(); int[][] bucket = new int[nums.length][10]; int[] bucketCount = new int[10]; for (int i = 0, n = 1; i < maxLen; i++, n*=10) { for (int num : nums) { int digitVal = num / n % 10; bucket[bucketCount[digitVal]][digitVal] = num; bucketCount[digitVal]++; } int index = 0; for (int j = 0; j < bucketCount.length; j++) { if(bucketCount[j] > 0){ for (int k = 0; k < bucketCount[j]; k++) { nums[index] = bucket[k][j]; index++; } } bucketCount[j] = 0; } } for (int i = 0; i < nums.length; i++) { nums[i] += min; } }
public void priorityQueueSort(int[] nums){ PriorityQueue<Integer> queue = new PriorityQueue<>(); for(int num : nums){ queue.offer(num); } for (int i = 0; i < nums.length; i++) { nums[i] = queue.poll(); } }
public void arraysApiSort(int[] nums){ Arrays.sort(nums); }
讀到這里,這篇“Java常見排序算法怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。