本文为原创文章,转载请注明出处,谢谢你……
喜欢java并发编程的请加群:736156823
开始-->
1)选择:
直接上代码:
// left,right均为数组下标
public void selection(int as[], int left, int right) {
if (null == as) {
return;
} else if (left < 0 || right < 0) {
return;
} else {
int size = as.length;
if (size <= 0) {
return;
} else if (right - left <= 0) {
return;
} else {
int i, j;
for (i = left; i < right; i++) {
int min = i;
for (j = i + i; j <= right; j++) {
if (as[j] < as[min]) {
min = j;
}
}
swap(as, i, min);
}
}
}
}
// left,right均为数组下标
public void selectionV2(int as[], int left, int right) {
if (null == as) {
return;
} else if (left < 0 || right < 0) {
return;
} else {
int size = as.length;
if (size <= 0) {
return;
} else if (right - left <= 0) {
return;
} else {
for (int i = left; i < right; i++) {
int min = i;
for (int j = i + 1; j <= right; j++) {
if (as[j] < as[min]) {
min = j;
}
}
swap(as, i, min);
}
}
}
}
2)插入:
public void insertion(int[] a, int left, int right) {
for (int p = left + 1; p <= right; p++) {
int tmp = a[p], j;
for (j = p; j > left && tmp < a[j - 1]; j--) {
a[j] = a[j - 1];
}
a[j] = tmp;
}
}
public void insertionV2(int[] as, int left, int right) {
if (null == as) {
return;
} else if (left < 0 || right < 0) {
return;
} else {
int size = as.length;
if (size <= 0) {
return;
} else if (right - left <= 0) {
return;
} else {
int i;
for (i = right; i > left; i--) {
compare(as, i - 1, i);
}
for (i = left + 2; i <= right; i++) {
int j = i;
int v = as[i];
while (v < as[j - 1]) {
as[j] = as[j - 1];
j--;
}
as[j] = v;
}
}
}
}
private void compare(int[] as, int a, int b) {
if (as[a] > as[b]) {
swap(as, a, b);
}
}
3)冒泡:
// 两种个冒泡的实现其中一种
public void bubble(int[] as, int b, int e) {
if (b < 0 || e < 0) {
return;
}
int length = e - b;
if (null == as) {
return;
} else if (length <= 0) {
return;
} else if (as.length <= 0) {
return;
} else {
for (int j = b; j < e; j++) {
for (int i = e; i > j; i--) {
compare(as, i, i - 1);
}
}
}
}
// 两种个冒泡的实现其中一种
public void bubbleV2(int[] as, int b, int e) {
if (b < 0 || e < 0) {
return;
}
int length = e - b;
if (null == as) {
return;
} else if (length <= 0) {
return;
} else if (as.length <= 0) {
return;
} else {
for (int j = b; j <= e; j++) {
for (int i = e; ; i--) {
int t = i - 1;
if (t >= j) {
compare(as, i, t);
} else {
break;
}
}
}
}
}
private void compare(int[] as, int a, int b) {
if (as[a] > as[b]) {
swap(as, a, b);
}
}
喜欢java并发编程的请加群:736156823
后续添加代码注释,暂时先这样……要吃饭了。
有问题欢迎指正,这是新鲜出炉的,还未修饰
结束-->
本文为原创文章,转载请注明出处,谢谢你……