一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 堆排序實例(Java數組實現)

堆排序實例(Java數組實現)

2021-02-26 13:21GoldArowana Java教程

下面小編就為大家分享一篇使用Java數組實現堆排序的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

堆排序:利用大根堆

數組全部入堆,再出堆從后向前插入回數組中,數組就從小到大有序了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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對比)
  */
 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對比)
  */
 public T popMax() {
  swap(1, size--);
  shiftDown(1);
  return data[size + 1];
 }
 
 /**
  * @param child 孩子節點下角標是child,父節點下角表是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數組中某個元素的下角標
  * @param b data數組中某個元素的下角標
  * @return 哪個元素大就返回哪個的下角標
  */
 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數組中某個元素的下角標
  * @param b data數組中某個元素的下角標
  * @param c data數組中某個元素的下角標
  * @return 哪個元素大就返回哪個的下角標
  */
 private int max(int a, int b, int c) {
  int biggest = max(a, b);
  biggest = max(biggest, c);
  return biggest;
 }
 
 
 /**
  * @param father 父節點下角標是father,左右兩個孩子節點的下角表分別是: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即將更新,父、左、右三個結點誰大,newFather就是誰的下角標
 
   if (lchild > size) {//如果該father結點既沒有左孩子,也沒有右孩子
    return;
   } else if (rchild > size) {//如果該father結點只有左孩子,沒有右孩子
    newFather = max(father, lchild);
   } else {//如果該father結點既有左孩子,又有右孩子
    newFather = max(father, lchild, rchild);
   }
 
   if (newFather == father) {//說明father比兩個子結點都要大,表名已經是大根堆,不用繼續調整了
    return;
   } else {//否則,還需要繼續調整堆,直到滿足大根堆條件為止
    swap(father, newFather);//值進行交換
    father = newFather;//更新father的值,相當于繼續調整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
 }
}

堆排序:對數組進行構造堆(最大堆)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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,數組建堆
  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數組中某個元素的下角標
  * @param b data數組中某個元素的下角標
  * @return 哪個元素大就返回哪個的下角標
  */
 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數組中某個元素的下角標
  * @param b data數組中某個元素的下角標
  * @param c data數組中某個元素的下角標
  * @return 哪個元素大就返回哪個的下角標
  */
 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;//這里賦不賦值無所謂,如果把下面這個return改成break,那就必須賦值了
 
   if (lchild > size) {//如果沒有左、右孩子
    return;
   } else if (rchild > size) {//如果沒有右孩子
    newFather = max(father, lchild);
   } else {//如果有左、右孩子
    newFather = max(father, lchild, rchild);
   }
 
   if (newFather == father) {//如果原父結點就是三者最大,則不用繼續整理堆了
    return;
   } else {//父節點不是最大,則把大的孩子交換上來,然后繼續往下堆調整,直到滿足大根堆為止
    swap(newFather, father);
    father = newFather;//相當于繼續shiftDown(newFather)。假如newFather原來是father的左孩子,那就相當于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
 }
}

以上這篇堆排序實例(Java數組實現)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/noKing/p/7955197.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: yellow片在线观看 | 韩国三级hd中文字幕李采潭 | 欧美日韩精品一区二区三区视频 | 日本动漫打扑克动画片樱花动漫 | hd最新国产人妖ts视频 | 国产一卡二卡四卡免费 | 99视频精品国在线视频艾草 | jzjzjz日本在线观看 | 色综合视频一区二区三区 | 久久国产香蕉 | 国产欧美日韩不卡一区二区三区 | 脱了白丝校花的内裤猛烈进入 | 欧美交换乱理伦片120秒 | 91精品国产高清久久久久久 | 亚洲美日韩 | 操动漫美女视频 | 驯服有夫之妇HD中字日本 | www久久精品 | 日本在线播放视频 | 白丝h视频 | 亚洲一级特黄特黄的大片 | 日韩欧美国产综合精品 | 国产精品欧美亚洲韩国日本99 | 99国内精品久久久久久久黑人 | 精品国语国产在线对白 | 武侠古典久久亚洲精品 | 日本tube24xxxxx| 国产精品久久国产精品99 | 憋尿调教绝望之岛 | 成人免费草草视频 | 美女和男人一起差差 | 国产精品永久免费自在线观看 | 精品久久免费视频 | 97影视| 国产成人精品免费久久久久 | 韩日视频在线 | 免费岛国片 | 男女视频在线观看网站 | 好大好硬好深好爽想要小雪 | 四虎影视在线观看2413 | 草草线在成年免费视频网站 |