排序算法
选择排序:
![](https://img-blog.csdnimg.cn/20190322173119213.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpbjZZYW5n,size_16,color_FFFFFF,t_70)
// 选择排序
public static void selectSort(int[] arr){
for(int x=0;x<arr.length-1;x++){
for(int y=x+1;y<arr.length;y++){
if(arr[x]>arr[y]){
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
}
}
冒泡排序:
![](https://img-blog.csdnimg.cn/20190322173535860.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1hpbjZZYW5n,size_16,color_FFFFFF,t_70)
// 冒泡排序
public static void bubbleSort(int[] arr){
for(int x=0;x<arr.length-1;x++){
for(int y=0;y<arr.length-x-1;y++){ // -x:让每一次比较的元素减少,-1:避免角标越界
if(arr[y]>arr[y+1]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
实际开发使用:Arrays.sort()
公共数据值位置替换功能代码:
// 位置替换
public static void swap(int[] arr,int x,int y){
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}