GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — homework/SortAlgorithm.java at master · melochey/homework

Expand file tree

/

SortAlgorithm.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
220 lines (197 loc) · 5.15 KB
/

SortAlgorithm.java

Copy path

File metadata and controls

220 lines (197 loc) · 5.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.server.components;
public class SortAlgorithm {
public static void main(String[] a){
int s[] ={0,7,10,1,6,23,4,12,22};
// sort(s,0,7);
// for(int i=0;i<s.length;i++){
// System.out.println(s[i]);
// }
finalMap(s,8);
}
public static void sortInsert(int[] nums){
for(int i=0;i<nums.length-1;i++){
int j=i+1;
int temp=nums[j];
while(i>0&&temp<nums[i]){
nums[j]=nums[i];
j--;
i--;
}
nums[j]=temp;
}
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
public static void sortSelect(int[] nums ){
for(int i=0;i<nums.length-1;i++){
int min=i;
for(int j=i+1;j<nums.length;j++){
if(nums[min]>nums[j]){
min=j;
}
}
if(i!=min){
int t=nums[i];
nums[i]=nums[min];
nums[min]=t;
}
}
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]);
}
}
public static void Sort(int[] array)
{
aux = new int[array.length]; // 仅分配一次
mergeSort(array, 0, array.length - 1);
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
public static void mergeSort(int[] a,int l,int h){
if(l>=h){
return;
}
int m=l+(h-l)/2;
mergeSort(a, l, m);
mergeSort(a, m+1, h);
merge(a,l,m,h);
}
private static int aux[];
public static void merge(int[] a,int l,int m,int h){
int i=l,j=m+1;
//把元素拷贝到辅助数组中
for (int k = l; k <= h; k++)
{
aux[k] = a[k];
}
//然后按照规则将数据从辅助数组中拷贝回原始的array中
for (int k = l; k <= h; k++)
{
//如果左边元素没了, 直接将右边的剩余元素都合并到到原数组中
if (i > m)
{
a[k] = aux[j++];
}//如果右边元素没有了,直接将所有左边剩余元素都合并到原数组中
else if (j > h)
{
a[k] = aux[i++];
}//如果左边右边小,则将左边的元素拷贝到原数组中
else if (aux[i]<aux[j])
{
a[k] = aux[i++];
}
else
{
a[k] = aux[j++];
}
}
}
public static int partition(int[] nums, int low, int high) {
int key = nums[low];//基准数
int i = low;//左指针
int j = high;//右指针
if (low < high) {
while (i < j) {
//形象点可以理解为,右指针左移
while (i < j && nums[j] >= key) {
j--;
}
if (i < j) {
nums[i] = nums[j];
i++;
}
//形象点可以理解为,左指针右移
while (i < j && nums[i] <= key) {
i++;
}
if (i < j) {
nums[j] = nums[i];
j--;
}
}
//把key填入最后的位置,即基准数位
nums[i] = key;
}
return i;
}
private static int Partition(int[] array, int lo, int hi)
{
int i = lo, j = hi + 1;
while (true)
{
//从左至右扫描,如果碰到比基准元素array[lo]小,则该元素已经位于正确的分区,i自增,继续比较i+1;
//否则,退出循环,准备交换
while (array[++i]<array[lo])
{
//如果扫描到了最右端,退出循环
if (i == hi) break;
}
//从右自左扫描,如果碰到比基准元素array[lo]大,则该元素已经位于正确的分区,j自减,继续比较j-1
//否则,退出循环,准备交换
while (array[--j]>array[lo])
{
//如果扫描到了最左端,退出循环
if (j == lo) break;
}
//如果相遇,退出循环
if (i >= j) break;
//交换左a[i],a[j]右两个元素,交换完后他们都位于正确的分区
Swap(array, i, j);
}
//经过相遇后,最后一次a[i]和a[j]的交换
//a[j]比a[lo]小,a[i]比a[lo]大,所以将基准元素与a[j]交换
Swap(array, lo, j);
//返回扫描相遇的位置点
return j;
}
public static void Swap(int[] a,int i,int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
public static int[] sort(int[] nums, int low, int high) {
if (low < high) {
int mid = Partition(nums, low, high);
//左边
sort(nums, low, mid - 1);
//右边
sort(nums, mid + 1, high);
}
return nums;
}
public static void Sink(int[] pq,int k,int N){
while (2 * k <= N)
{
int j = 2 * k;
if(j==N){
}else{
if (pq[j]<pq[j + 1]) j++;
}
//去左右子节点中,稍大的那个元素做比较
//如果父节点比这个较大的元素还大,表示满足要求,退出
if (pq[k]>pq[j]) break;
//否则,与子节点进行交换
Swap(pq, k, j);
k = j;
}
}
public static void finalMap(int[] pq,int N){
for (int k = N / 2; k >= 1; k--)
{
Sink(pq, k, N);
}
while(N>0){
//根元素从1开始,0不存放值
int max = pq[1];
//将最后一个元素和根节点元素进行交换
Swap(pq, 1, N--);
//对根节点从上至下重新建堆
Sink(pq,1,N);
//将最后一个元素置为空
pq[N + 1] = 0;
System.out.println(max);
}
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.