java 几种简单排序实现代码

来源:互联网 发布:广联达软件官方售价 编辑:程序博客网 时间:2024/05/20 22:31
import java.sql.Array;
import java.util.Arrays;
import java.util.Random;


public class TestArray {
public static void main(String[] args) {
array1();
System.out.println("****");
array2();
System.out.println("&&&&&&&&");
array3();
System.out.println("^^^^^^^^^");
array5();
System.out.println("********");
array6();
System.out.println("&&&&&&&&");
array7();
System.out.println("^^^^^^^^^");
array8();
System.out.println("#########");
array9();
System.out.println("^^^^^^^^^");
array10();
}
//随机获取字符数组中元素组成一个姓名
static void array1() {
String first[] = { "张", "王", "李", "赵", "何", "谢" };
String second[] = { "雷", "磊", "有", "成", "中", "兵" };


String m = first[(int) (Math.random() * first.length)];
String n = second[(int) (Math.random() * second.length)];


System.out.println(m + n);


}
//获取100以内的前20个素数
static void array2() {
int fr[] = new int[20];
int s = 0;
for (int i = 1; i < 100; i++) {
boolean falg = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
falg = false;
break;
}
}
if (falg) {
fr[s] = i;
System.out.print(fr[s] + " ");
s++;
if (s == 19)
break;
}
}
}


// 将一个数组里的元素循环放到另一个数组里去
static void array3() {
int a[] = new int[20];
int b[] = { 3, 6, 8, 23, 5, 4 };


int c = a.length % b.length;// a数组的长度与b数组的长度去模
int d = a.length / b.length;
if (c != 0) {
d++;
}
for (int i = 0; i < d; i++) {


int x = b.length;
if (c != 0 && i == d - 1) {
x = c;
}
System.arraycopy(b, 0, a, i * b.length, x);
}
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
//选择排序
static void array5() {
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };


for (int i = 0; i < a.length; i++) {
int max = 0;
int pos = 0;
for (int j = i; j < a.length; j++) {
if (max < a[j]) {
max = a[j];
pos = j;
}
}
for (int j = pos - 1; j >= i; j--) {
a[j + 1] = a[j];
}
a[i] = max;


}
System.out.println(Arrays.toString(a));
}
//选择排序
static void array6() {
int arr[] = { 13, 7, 9, 4, 6, 81, 23, 37, 5, 42, 10 };
int max = 0;
int pos = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i; j < arr.length; j++) {
if (arr[j] > max) {
max = arr[j];
pos = j;
}
}
for (int m = pos - 1; m >= i; m--) {
arr[m + 1] = arr[m];
}
arr[i] = max;
max = 0;
}
System.out.println(Arrays.toString(arr));
}
//插入排序
static void array7() {
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1, 8 };
System.out.println(Arrays.toString(a));
for (int i = 0; i < a.length; i++) {
int max = a[i];
int temp = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] > max) {
max = a[j];
temp = j;
}
}
a[temp] = a[i];
a[i] = max;
}
System.out.println(Arrays.toString(a));
}


static void array8() {
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
System.out.println("排序之前:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
// 直接插入排序
for (int i = 1; i < a.length; i++) {
// 待插入元素
int temp = a[i];
int j;
/*
* for (j = i-1; j>=0 && a[j]>temp; j--) { //将大于temp的往后移动一位 a[j+1] =
* a[j]; }
*/
for (j = i - 1; j >= 0; j--) {
// 将大于temp的往后移动一位
if (a[j] > temp) {
a[j + 1] = a[j];
} else {
break;
}
}
a[j + 1] = temp;
}
System.out.println();
System.out.println("排序之后:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}


static void array9() {
int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
for (int i = 0; i < a.length; i++) {
int max = a[i];
int pos = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] > max) {
max = a[j];
pos = j;
}
}
a[pos] = a[i];
a[i] = max;


}
System.out.println(Arrays.toString(a));
}


static void array10() {
int[] a = { 49, 38, 65, 97, 76, 13, 27, 54, 78, 34, 12, 64, 1 };
int len = a.length;
for(int i = 0;i < a.length;i++){
for(int j = len - 1;j >i;j--){
if(a[j] > a[j - 1]){
int temp = a[j];
a[j] = a[j - 1];
a[j -1] = temp;

}
}
}
System.out.println(Arrays.toString(a));
}
}
0 0