好友
阅读权限10
听众
最后登录1970-1-1
|
Yes12
发表于 2023-11-20 09:04
public class RecType {
int key;
String data;
public RecType(int d)
{ key=d; }
}
class SqListSortClass {
final int MAXN = 100;
RecType[] R;
int n;
public void swap(int i, int j) {
RecType tmp = R[i];
R[i] = R[j];
R[j] = tmp;
}
public void CreateR(int[] a) {
R = new RecType[MAXN];
for (int i = 0; i < a.length; i++)
R[i] = new RecType(a[i]);
n = a.length;
}
public void Disp() {
for (int i = 0; i < n; i++)
System.out.print(R[i].key + "");
System.out.println();
}
public void CreateR1(int[] a) {
R = new RecType[MAXN];
for (int i = 0; i < a.length; i++)
R[i + 1] = new RecType(a[i]);
n = a.length;
}
public void Disp1() {
for (int i = 1; i <= n; i++)
System.out.print(R[i].key + "");
System.out.println();
}
public void InsertSort() {
RecType tmp;
int j;
for (int i = 1; i < n; i++) {
if (R[i].key < R[i - 1].key) {
tmp = R[i];
j = i - 1;
do {
R[j + 1] = R[j];
j--;
} while (j >= 0 && R[j].key > tmp.key);
R[j + 1] = tmp;
}
}
}
public void BinInsertSort() {
int low, high, mid;
RecType tmp;
for (int i = 1; i < n; i++) {
if (R[i].key < R[i - 1].key) {
tmp = R[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (tmp.key < R[mid].key)
high = mid - 1;
else
low = mid + 1;
}
for (int j = i - 1; j >= high + 1; j--)
R[j + 1] = R[j];
R[high + 1] = tmp;
}
}
}
public void ShellSort() {
RecType tmp;
int d = n / 2;
while (d > 0) {
for (int i = d; i < n; i++) {
tmp = R[i];
int j = i - d;
while (j >= 0 && tmp.key < R[j].key) {
R[j + d] = R[j];
j = j - d;
}
R[j + d] = tmp;
}
d = d / 2;
}
}
public void BubbleSort() {
boolean exchange = false;
for (int i = 1; i < n - 1; i++) {
exchange = false;
for(int j = n - 1; j > i; j--)
if (R[j].key < R[j - 1].key) {
swap(j, j - 1);
exchange = true;
}
if (!exchange) return;
}
}
}
class S686{
public static void main(String[] arge){
SqListSortClass ssc=new SqListSortClass();
int[] date ={6,3,9,5,4,6,8,7,5,2,4};
ssc.CreateR(date);
ssc.Disp();
ssc.InsertSort();
ssc.Disp();
}
} |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|