package com.tgyt.demo;
import java.util.Arrays;
public class Eg01 {
public static void main(String[] args) {
int[] nums = {2, 5, 11, 3, 22, 6, 91, 13, 35, 4};
//对数组进行排序,使用的是Arrays自带的sort方法
Arrays.sort(nums);
// mpSearch(nums);
// xzSearch(nums);
// crSearch(nums);
// fxSearch(nums);
//升序
for (int i = 0;i<nums.length;i++) {
System.out.print(nums[i]+",");
}
System.out.println();
//降序
for (int i = nums.length-1;i>=0;i--) {
System.out.print(nums[i]+",");
}
}
//冒泡排序
public static int[] mpSearch(int[] nums) {
for (int i = 0;i<nums.length;i++) {
for (int j = i+1;j<nums.length;j++) {
if (nums[i] > nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
return nums;
}
//选择排序
public static int[] xzSearch(int[] nums) {
for (int i = 0;i<nums.length;i++) {
int mix = i;
for (int j = i+1;j<nums.length;j++) {
if (nums[mix] > nums[j]) {
mix=j;
}
}
if (mix!=i) {
int temp = nums[i];
nums[i] = nums[mix];
nums[mix] = temp;
}
}
return nums;
}
//插入排序
public static int[] crSearch(int[] nums) {
for (int i = 1;i<nums.length;i++) {
for (int j = i;j>0;j--) {
if (nums[j] < nums[j-1]) {
int temp = nums[j-1];
nums[j-1] = nums[j];
nums[j] = temp;
} else break;
}
}
return nums;
}
//反向排序
public static int[] fxSearch(int[] nums) {
int temp;
int length = nums.length;
for (int i = 0;i<length/2;i++) {
temp = nums[i];
nums[i] = nums[length-1-i];
nums[length-1-i] = temp;
}
return nums;
}
}