import java.util.Arrays;
import java.util.HashSet;
public class HelloWorld {
public static void main(String [] args) {
System.out.println("Hello World");
int [] arrays = {10,1,9,8,7};
for(int array : arrays){
System.out.print("--" + array + " ");
}
System.out.println("");
//popSelect(arrays);
//bubbleSort(arrays);
//selectSort(arrays);
//insertSort(arrays);
//quickSort(arrays);
sort(arrays);
for(int array : arrays){
System.out.print("--" + array + " ");
}
System.out.println("");
}
private static void sort(int [] arrays){
if(arrays == null || arrays.length < 0){
return;
}
Arrays.sort(arrays);
}
//快速排序法
private static void quickSort(int [] arrays){
recursionQuickSort(arrays,0,arrays.length-1);
}
private static void recursionQuickSort(int [] arrays, int low, int high){
int start = low;
int end = high;
int key = arrays[low];
while(end > start){
//从后往前比较
while(end > start && arrays[end] >= key){
//如果没有比关键值小的,比较下一个,直到有比
//关键值小的交换位置,然后又从前往后比较。
end--;
}
if(arrays[end] <= key){
int temp = arrays[end];
arrays[end] = arrays[start];
arrays[start] = temp;
}
//从前往后比较
while(end > start && arrays[start] <= key){
//如果没有比关键值大的,比较下一个,
//直到有比关键值大的交换位置
start++;
}
if(arrays[start] >= key){
int temp = arrays[start];
arrays[start] = arrays[end];
arrays[end] = temp;
}
//此时第一次循环比较结束,关键值的位置已经确定了。
//左边的值都比关键值小,右边的值都比关键值大,
//但是两边的顺序还有可能是不一样的,进行下面的递归调用
}
//递归
if (start > low) {
recursionQuickSort(arrays,low,start-1);
}
if(end < high){
recursionQuickSort(arrays, end+1, high);
}
for(int array : arrays){
System.out.print(array + " ");
}
System.out.println("");
}
//(直接插入)插入排序法
private static void insertSort(int [] arrays){
if(arrays == null || arrays.length < 0){
return;
}
int temp;
for(int i=1; i
for(int j = i; j > 0; j--){
if(arrays[j] < arrays[j-1]){
temp = arrays[j-1];
arrays[j-1] = arrays[j];
arrays[j] = temp;
}
}
for(int array : arrays){
System.out.print(array + " ");
}
System.out.println("");
}
}
//选择排序法
private static void selectSort(int [] arrays){
if(arrays == null || arrays.length < 0){
return;
}
for(int i=0; i < arrays.length; i++){
int temp = arrays[i];
int flag = i; //最小元素的下标
for(int j=i+1; j < arrays.length; j++){
if(arrays[j] < temp){
temp = arrays[j];
flag = j;
}
}
if (flag != i) {
arrays[flag] = arrays[i];
arrays[i] = temp;
}
for(int array : arrays){
System.out.print(array + " ");
}
System.out.println("");
}
}
//冒泡排序
private static void bubbleSort(int [] arrays){
if(arrays == null || arrays.length < 0){
return;
}
for(int i = 0; i < arrays.length - 1; i++){//外层循环控制排序趟数
for(int j = 0; j < arrays.length - 1 - i; j++){//内层循环控制每一趟排序多少次
if(arrays[j] > arrays[j+1]){
int temp = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1] = temp;
}
}
for(int array : arrays){
System.out.print(array + " ");
}
System.out.println("");
}
}
private static void popSelect(int [] arrays){
if(arrays == null || arrays.length < 0){
return;
}
for(int i = 0; i < arrays.length - 1; i++){
for(int j = i + 1; j < arrays.length; j++){
if(arrays[i] > arrays[j]){
int temp = arrays[i];
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
for(int array : arrays){
System.out.print(array + " ");
}
System.out.println("");
}
}
}
class A{
public boolean equals(Object obj){
return true;
}
}
class B{
public int hashCode(){
return 2;
}
}
class C{
public int hashCode(){
return 1;
}
public boolean equals(Object obj){
return true;
}
}
https://www.cnblogs.com/xhj123/p/6032683.html