package com.huadee.array;
import java.util.Arrays;
public class ArrayDemo05 {
public static void main(String[] args) {
System.out.println("第一种数组排序 利用Arrays的sort的方法");
int a[] = {45,21,78,12,95,65,32,2,15};
Arrays.sort(a);
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第二种数组排序 利用冒泡排序的方法");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i]<a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
System.out.println("");
System.out.println("第三种数组排序 利用選擇排序的方法");
for (int i = 0; i < a.length; i++) {
int lowerIndex = i;
//找出最小的的一個
for (int j = i+1; j < a.length; j++) {
if (a[j] < a[lowerIndex]) {
lowerIndex = j;
}
}
//交換
int temp = a[i];
a[i] = a[lowerIndex];
a[lowerIndex] = temp;
}
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第四种数组排序 利用插入排序的方法");
for (int i = 0; i < a.length; i++) {
// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j >0; j--) {
if (a[j] < a[j-1]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第五种数组排序 利用希尔排序的方法");
//分组
for(int increment=a.length/2; increment>0; increment/=2 ){
//每个组内排序
for (int i = 0; i < a.length; i++) {
int temp = a[i];
int j = 0;
for (j=i; j >= increment; j-=increment) {
if (temp<a[j-increment]) {
a[j] = a[j-increment];
}else{
break;
}
}
a[j]=temp;
}
}
for (int i : a) {
System.out.print(i+",");
}
}
}
import java.util.Arrays;
public class ArrayDemo05 {
public static void main(String[] args) {
System.out.println("第一种数组排序 利用Arrays的sort的方法");
int a[] = {45,21,78,12,95,65,32,2,15};
Arrays.sort(a);
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第二种数组排序 利用冒泡排序的方法");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i]<a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
System.out.println("");
System.out.println("第三种数组排序 利用選擇排序的方法");
for (int i = 0; i < a.length; i++) {
int lowerIndex = i;
//找出最小的的一個
for (int j = i+1; j < a.length; j++) {
if (a[j] < a[lowerIndex]) {
lowerIndex = j;
}
}
//交換
int temp = a[i];
a[i] = a[lowerIndex];
a[lowerIndex] = temp;
}
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第四种数组排序 利用插入排序的方法");
for (int i = 0; i < a.length; i++) {
// i从一开始,因为第一个数已经是排好序的啦
for (int j = i; j >0; j--) {
if (a[j] < a[j-1]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
for (int i : a) {
System.out.print(i+",");
}
System.out.println("");
System.out.println("第五种数组排序 利用希尔排序的方法");
//分组
for(int increment=a.length/2; increment>0; increment/=2 ){
//每个组内排序
for (int i = 0; i < a.length; i++) {
int temp = a[i];
int j = 0;
for (j=i; j >= increment; j-=increment) {
if (temp<a[j-increment]) {
a[j] = a[j-increment];
}else{
break;
}
}
a[j]=temp;
}
}
for (int i : a) {
System.out.print(i+",");
}
}
}