Update the section of heap.

This commit is contained in:
krahets
2023-05-25 20:25:19 +08:00
parent b9178bc7d6
commit 11c835d79c
36 changed files with 22 additions and 24 deletions

View File

@@ -36,11 +36,9 @@ class MaxHeap {
/* 交换元素 */
void _swap(int i, int j) {
int a = _maxHeap[i];
int b = _maxHeap[j];
int tem = a;
_maxHeap[i] = b;
_maxHeap[j] = tem;
int tmp = _maxHeap[i];
_maxHeap[i] = _maxHeap[j];;
_maxHeap[j] = tmp;
}
/* 获取堆大小 */

View File

@@ -41,10 +41,8 @@ class MaxHeap {
/* 交换元素 */
private void swap(int i, int j) {
int a = maxHeap.get(i);
int b = maxHeap.get(j);
int tmp = a;
maxHeap.set(i, b);
int tmp = maxHeap.get(i);
maxHeap.set(i, maxHeap.get(j));
maxHeap.set(j, tmp);
}

View File

@@ -37,10 +37,8 @@ class MaxHeap {
/* 交换元素 */
#swap(i, j) {
const a = this.#maxHeap[i],
b = this.#maxHeap[j],
tmp = a;
this.#maxHeap[i] = b;
const tmp = this.#maxHeap[i];
this.#maxHeap[i] = this.#maxHeap[j];
this.#maxHeap[j] = tmp;
}

View File

@@ -35,8 +35,7 @@ class MaxHeap:
def swap(self, i: int, j: int):
"""交换元素"""
a, b = self.max_heap[i], self.max_heap[j]
self.max_heap[i], self.max_heap[j] = b, a
self.max_heap[i], self.max_heap[j] = self.max_heap[j], self.max_heap[i]
def size(self) -> int:
"""获取堆大小"""

View File

@@ -36,10 +36,8 @@ class MaxHeap {
/* 交换元素 */
private swap(i: number, j: number): void {
const a = this.maxHeap[i],
b = this.maxHeap[j],
tmp = a;
this.maxHeap[i] = b;
const tmp = this.maxHeap[i];
this.maxHeap[i] = this.maxHeap[j];
this.maxHeap[j] = tmp;
}

View File

@@ -48,10 +48,8 @@ pub fn MaxHeap(comptime T: type) type {
// 交换元素
fn swap(self: *Self, i: usize, j: usize) !void {
var a = self.max_heap.?.items[i];
var b = self.max_heap.?.items[j];
var tmp = a;
try self.max_heap.?.replaceRange(i, 1, &[_]T{b});
var tmp = self.max_heap.?.items[i];
try self.max_heap.?.replaceRange(i, 1, &[_]T{self.max_heap.?.items[j]});
try self.max_heap.?.replaceRange(j, 1, &[_]T{tmp});
}