package exercise;
public class Paixu {
public static void main(String[] args) {
//排序前
int []a = {1,2,3,4,5,-1,-2,-3,-4,-5};
System.out.println("排序前:");
for (int i = 0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
//-----------------------------------------------------------------------------------------------
//冒泡排序
int temp = 0;
for(int i = 0;i<a.length-1;i++){
for(int j = i;j<a.length;j++){
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("冒泡排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//----------------------------------------------------------------------------------------------
//选择排序
for(int i = 0;i<a.length;i++){
int position = i;
for(int j = i+1;j<a.length;j++){
if(a[position]>a[j]){
position = j;
}
}
int temp1 = a[i];
a[i] = a[position];
a[position] = temp1;
}
System.out.println("选择排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//-----------------------------------------------------------------------------------------------
//插入排序
for(int i = 0;i<a.length;i++){
int b = a[i];
int index = i-1;
while (index>=0&&b>a[index]) {
a[index+1]=a[index];
index--;
}
a[index+1]=b;
}
System.out.println("插入排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}
public class Paixu {
public static void main(String[] args) {
//排序前
int []a = {1,2,3,4,5,-1,-2,-3,-4,-5};
System.out.println("排序前:");
for (int i = 0;i<a.length;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
//-----------------------------------------------------------------------------------------------
//冒泡排序
int temp = 0;
for(int i = 0;i<a.length-1;i++){
for(int j = i;j<a.length;j++){
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("冒泡排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//----------------------------------------------------------------------------------------------
//选择排序
for(int i = 0;i<a.length;i++){
int position = i;
for(int j = i+1;j<a.length;j++){
if(a[position]>a[j]){
position = j;
}
}
int temp1 = a[i];
a[i] = a[position];
a[position] = temp1;
}
System.out.println("选择排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//-----------------------------------------------------------------------------------------------
//插入排序
for(int i = 0;i<a.length;i++){
int b = a[i];
int index = i-1;
while (index>=0&&b>a[index]) {
a[index+1]=a[index];
index--;
}
a[index+1]=b;
}
System.out.println("插入排序后:");
for(int i = 0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}