亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

堆排序?qū)嵗?Java數(shù)組實(shí)現(xiàn))

 更新時(shí)間:2017年12月04日 08:48:22   作者:GoldArowana  
下面小編就為大家分享一篇使用Java數(shù)組實(shí)現(xiàn)堆排序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

堆排序:利用大根堆

數(shù)組全部入堆,再出堆從后向前插入回?cái)?shù)組中,數(shù)組就從小到大有序了。

public class MaxHeap<T extends Comparable<? super T>> {
 private T[] data;
 private int size;
 private int capacity;
 
 public MaxHeap(int capacity) {
  this.data = (T[]) new Comparable[capacity + 1];
  size = 0;
  this.capacity = capacity;
 }
 
 public int size() {
  return this.size;
 }
 
 public boolean isEmpty() {
  return size == 0;
 }
 
 public int getCapacity() {
  return this.capacity;
 }
 
 /**
  * @return 查看最大根(只看不刪, 與popMax對(duì)比)
  */
 public T seekMax() {
  return data[1];
 }
 
 public void swap(int i, int j) {
  if (i != j) {
   T temp = data[i];
   data[i] = data[j];
   data[j] = temp;
  }
 }
 
 public void insert(T item) {
  size++;
  data[size] = item;
  shiftUp(size);
 }
 
 /**
  * @return 彈出最大根(彈出意味著刪除, 與seekMax對(duì)比)
  */
 public T popMax() {
  swap(1, size--);
  shiftDown(1);
  return data[size + 1];
 }
 
 /**
  * @param child 孩子節(jié)點(diǎn)下角標(biāo)是child,父節(jié)點(diǎn)下角表是child/2
  */
 public void shiftUp(int child) {
  while (child > 1 && data[child].compareTo(data[child / 2]) > 0) {
   swap(child, child / 2);
   child = child / 2;
  }
 }
 
 /**
  * @param a data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param b data數(shù)組中某個(gè)元素的下角標(biāo)
  * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo)
  */
 private int max(int a, int b) {
  if (data[a].compareTo(data[b]) < 0) {//如果data[b]大
   return b;//返回b
  } else {//如果data[a]大
   return a;//返回a
  }
 }
 
 /**
  * @param a data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param b data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param c data數(shù)組中某個(gè)元素的下角標(biāo)
  * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo)
  */
 private int max(int a, int b, int c) {
  int biggest = max(a, b);
  biggest = max(biggest, c);
  return biggest;
 }
 
 
 /**
  * @param father 父節(jié)點(diǎn)下角標(biāo)是father,左右兩個(gè)孩子節(jié)點(diǎn)的下角表分別是:father*2 和 father*2+1
  */
 public void shiftDown(int father) {
  while (true) {
   int lchild = father * 2;//左孩子
   int rchild = father * 2 + 1;//右孩子
   int newFather = father;//newFather即將更新,父、左、右三個(gè)結(jié)點(diǎn)誰大,newFather就是誰的下角標(biāo)
 
   if (lchild > size) {//如果該father結(jié)點(diǎn)既沒有左孩子,也沒有右孩子
    return;
   } else if (rchild > size) {//如果該father結(jié)點(diǎn)只有左孩子,沒有右孩子
    newFather = max(father, lchild);
   } else {//如果該father結(jié)點(diǎn)既有左孩子,又有右孩子
    newFather = max(father, lchild, rchild);
   }
 
   if (newFather == father) {//說明father比兩個(gè)子結(jié)點(diǎn)都要大,表名已經(jīng)是大根堆,不用繼續(xù)調(diào)整了
    return;
   } else {//否則,還需要繼續(xù)調(diào)整堆,直到滿足大根堆條件為止
    swap(father, newFather);//值進(jìn)行交換
    father = newFather;//更新father的值,相當(dāng)于繼續(xù)調(diào)整shiftDown(newFather)
   }
  }
 }
 
 public static <T extends Comparable<? super T>> void sort(T[] arr) {
  int len = arr.length;
  //入堆
  MaxHeap<T> maxHeap = new MaxHeap<T>(len);
  for (int i = 0; i < len; i++) {
   maxHeap.insert(arr[i]);
  }
  //出堆
  for (int i = len - 1; i >= 0; i--) {
   arr[i] = maxHeap.popMax();
  }
 }
 
 public static void printArr(Object[] arr) {
  for (Object o : arr) {
   System.out.print(o);
   System.out.print("\t");
  }
  System.out.println();
 }
 
 public static void main(String args[]) {
  Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6};
  printArr(arr);//3 5 1 7 2 9 8 0 4 6
  sort(arr);
  printArr(arr);//0 1 2 3 4 5 6 7 8 9
 }
}

堆排序:對(duì)數(shù)組進(jìn)行構(gòu)造堆(最大堆)

public class MaxHeap<T extends Comparable<? super T>> {
 private T[] data;
 private int size;
 private int capacity;
 
 public MaxHeap(int capacity) {
  this.capacity = capacity;
  this.size = 0;
  this.data = (T[]) new Comparable[capacity + 1];
 }
 
 public MaxHeap(T[] arr) {//heapify,數(shù)組建堆
  capacity = arr.length;
  data = (T[]) new Comparable[capacity + 1];
  System.arraycopy(arr, 0, data, 1, arr.length);
  size = arr.length;
  for (int i = size / 2; i >= 1; i--) {
   shiftDown(i);
  }
 }
 
 public int size() {
  return this.size;
 }
 
 public int getCapacity() {
  return this.capacity;
 }
 
 public boolean isEmpty() {
  return size == 0;
 }
 
 public T seekMax() {
  return data[1];
 }
 
 public void swap(int i, int j) {
  if (i != j) {
   T temp = data[i];
   data[i] = data[j];
   data[j] = temp;
  }
 }
 
 public void insert(T item) {
  size++;
  data[size] = item;
  shiftUp(size);
 }
 
 public T popMax() {
  swap(1, size--);
  shiftDown(1);
  return data[size + 1];
 }
 
 public void shiftUp(int child) {
  while (child > 1 && data[child].compareTo(data[child / 2]) > 0) {
   swap(child, child / 2);
   child /= 2;
  }
 }
 
 /**
  * @param a data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param b data數(shù)組中某個(gè)元素的下角標(biāo)
  * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo)
  */
 private int max(int a, int b) {
  if (data[a].compareTo(data[b]) < 0) {//如果data[b]大
   return b;//返回b
  } else {//如果data[a]大
   return a;//返回a
  }
 }
 
 /**
  * @param a data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param b data數(shù)組中某個(gè)元素的下角標(biāo)
  * @param c data數(shù)組中某個(gè)元素的下角標(biāo)
  * @return 哪個(gè)元素大就返回哪個(gè)的下角標(biāo)
  */
 private int max(int a, int b, int c) {
  int biggest = max(a, b);
  biggest = max(biggest, c);
  return biggest;
 }
 
 public void shiftDown(int father) {
  while (true) {
   int lchild = father * 2;
   int rchild = father * 2 + 1;
   int newFather = father;//這里賦不賦值無所謂,如果把下面這個(gè)return改成break,那就必須賦值了
 
   if (lchild > size) {//如果沒有左、右孩子
    return;
   } else if (rchild > size) {//如果沒有右孩子
    newFather = max(father, lchild);
   } else {//如果有左、右孩子
    newFather = max(father, lchild, rchild);
   }
 
   if (newFather == father) {//如果原父結(jié)點(diǎn)就是三者最大,則不用繼續(xù)整理堆了
    return;
   } else {//父節(jié)點(diǎn)不是最大,則把大的孩子交換上來,然后繼續(xù)往下堆調(diào)整,直到滿足大根堆為止
    swap(newFather, father);
    father = newFather;//相當(dāng)于繼續(xù)shiftDown(newFather)。假如newFather原來是father的左孩子,那就相當(dāng)于shiftDown(2*father)
   }
  }
 }
 
 public static <T extends Comparable<? super T>> void sort(T[] arr) {
  int len = arr.length;
  MaxHeap<T> maxHeap = new MaxHeap<>(arr);
  for (int i = len - 1; i >= 0; i--) {
   arr[i] = maxHeap.popMax();
  }
 }
 
 public static void printArr(Object[] arr) {
  for (Object o : arr) {
   System.out.print(o);
   System.out.print("\t");
  }
  System.out.println();
 }
 
 public static void main(String args[]) {
  Integer[] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6};
  printArr(arr);//3 5 1 7 2 9 8 0 4 6
  sort(arr);
  printArr(arr);//0 1 2 3 4 5 6 7 8 9
 }
}

以上這篇堆排序?qū)嵗?Java數(shù)組實(shí)現(xiàn))就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 垃圾回收機(jī)制詳解(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)

    Java 垃圾回收機(jī)制詳解(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)

    在系統(tǒng)運(yùn)行過程中,會(huì)產(chǎn)生一些無用的對(duì)象,這些對(duì)象占據(jù)著一定的內(nèi)存,如果不對(duì)這些對(duì)象清理回收無用對(duì)象的內(nèi)存,可能會(huì)導(dǎo)致內(nèi)存的耗盡,所以垃圾回收機(jī)制回收的是內(nèi)存。下面通過本文給大家詳細(xì)介紹java垃圾回收機(jī)制,一起學(xué)習(xí)吧
    2017-02-02
  • java多線程實(shí)現(xiàn)有序輸出ABC

    java多線程實(shí)現(xiàn)有序輸出ABC

    這篇文章主要為大家詳細(xì)介紹了java多線程實(shí)現(xiàn)有序輸出ABC,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • java實(shí)現(xiàn)音樂播放器完整代碼(調(diào)整顯示音量大小、調(diào)整進(jìn)度、圖片切換)

    java實(shí)現(xiàn)音樂播放器完整代碼(調(diào)整顯示音量大小、調(diào)整進(jìn)度、圖片切換)

    這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)音樂播放器(調(diào)整顯示音量大小、調(diào)整進(jìn)度、圖片切換)的相關(guān)資料,這本身是老師布置的一個(gè)作業(yè),寫完感覺不錯(cuò)分享給大家,需要的朋友可以參考下
    2023-07-07
  • Java設(shè)計(jì)模式中的單例模式解析

    Java設(shè)計(jì)模式中的單例模式解析

    這篇文章主要介紹了Java設(shè)計(jì)模式中的單例模式解析,單例模式確保一個(gè)類只有一個(gè)實(shí)例,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例,需要的朋友可以參考下
    2023-11-11
  • 新手也能看懂的SpringBoot異步編程指南(簡單易懂)

    新手也能看懂的SpringBoot異步編程指南(簡單易懂)

    這篇文章主要介紹了新手也能看懂的SpringBoot異步編程指南(簡單易懂),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Spring 單元測(cè)試中如何進(jìn)行 mock的實(shí)現(xiàn)

    Spring 單元測(cè)試中如何進(jìn)行 mock的實(shí)現(xiàn)

    這篇文章主要介紹了Spring 單元測(cè)試中如何進(jìn)行 mock的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Java冒泡排序法和選擇排序法的實(shí)現(xiàn)

    Java冒泡排序法和選擇排序法的實(shí)現(xiàn)

    這篇文章主要介紹了Java冒泡排序法和選擇排序法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 淺析SpringBoot中的過濾器和攔截器

    淺析SpringBoot中的過濾器和攔截器

    過濾器和攔截器都是為了在請(qǐng)求到達(dá)目標(biāo)處理器(Servlet或Controller)之前或者之后插入自定義的處理邏輯,下面就跟隨小編來看看它們二者的區(qū)別和具體使用吧
    2024-03-03
  • Java 對(duì) Cookie增刪改查的實(shí)現(xiàn)示例

    Java 對(duì) Cookie增刪改查的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java 對(duì) Cookie增刪改查的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解

    Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解

    這篇文章主要介紹了Java多線程 ReentrantReadWriteLock原理及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論