Skip to content

Commit 1404882

Browse files
committed
ok
1 parent 2dc5684 commit 1404882

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/com/leetcode/Sort.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,17 @@ public void shellSort(int nums[]) {
157157
/***************************************希尔排序******************************************/
158158

159159
/********************************堆排序*******************************************/
160-
public int findKthLargest(int[] nums, int k) {
161-
if (nums == null || nums.length < k) {
162-
return 0;
160+
public void duiSort(int[] nums) {
161+
if (nums == null || nums.length <= 1) {
162+
return;
163163
}
164164
int heapSize = nums.length;
165165
buildHeap(nums, heapSize);
166-
for (int i = nums.length-1; i > nums.length-k; i--) {
167-
swap(nums, 0, i);
166+
while (heapSize > 0) {
167+
swap(nums, 0, heapSize - 1);
168168
heapSize--;
169169
heapify(nums, 0, heapSize);
170170
}
171-
return nums[0];
172171
}
173172

174173
public void buildHeap(int[] nums, int heapSize) {
@@ -217,5 +216,8 @@ public static void main(String[] args) {
217216
nums = new int[]{5, 2, 6, 1, 7, 3, 2};
218217
s.shellSort(nums);
219218
System.out.println("希尔排序:" + Arrays.toString(nums));
219+
nums = new int[]{5, 2, 6, 1, 7, 3, 2};
220+
s.duiSort(nums);
221+
System.out.println("堆排序:" + Arrays.toString(nums));
220222
}
221223
}

0 commit comments

Comments
 (0)