package com.sort;
import java.util.Arrays;
/**
* 〈一句话功能简述〉;
* 〈冒泡排序〉
*
* @author jxx
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class pop {
public static void main(String[] args){
int[] arr = {2,4,5,1,7,9};
System.out.println(Arrays.toString(sortPop(arr)));
}
public static int[] sortPop(int[] arr) {
for(int i=0;i<arr.length - 1;i++) {
for(int j=0;j<arr.length - 1 - i;j++) {
if(arr[j] > arr[j+1]) {
int tmp = arr[i];
arr[i] = arr[j+1];
arr[j+1] = tmp;
}
}
}
return arr;
}
}
package com.sort;
import java.util.Arrays;
/**
* 〈一句话功能简述〉;
* 〈快速排序〉
*
* @author jxx
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class Rapid {
public static void main(String[] args){
int[] arr = {2,4,5,1,7,9};
System.out.println(Arrays.toString(sortRapid(arr,0,arr.length-1)));
}
public static int[] sortRapid(int[] arr,int low,int high) {
int i,j,temp,t;
if(low > high) {
return null;
}
temp = arr[low];
i=low;
j=high;
while(i<j) {
//右边
while(temp<=arr[j]&&i<j) {
j--;
}
//左边
while(temp>=arr[i]&&i<j) {
i++;
}
if(i < j) {
//交换位置
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
//将参考数挪到指定位置
arr[low] = arr[i];
arr[i] = temp;
//递归左边
sortRapid(arr,low,j-1);
//递归右边
sortRapid(arr,j+1,high);
return arr;
}
}
package com.search;
/**
* 〈一句话功能简述〉;
* 〈二分查找法〉
*
* @author jxx
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class binSearch {
public static void main(String[] args){
int[] arr = {1,2,3,4,5,6};
System.out.println(binSearch(3,arr));
System.out.println(binSearchReturn(5,arr,0,arr.length-1));
}
public static int binSearch(int flag,int[] arr) {
if(arr.length == 0) {
return -1;
}
int start = 0;
int end = arr.length - 1;
while(start <= end) {
int mid = (start+end)/2;
if(flag == arr[mid]) {
return mid;
} else if(flag < arr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static int binSearchReturn(int flag,int[] arr,int start,int end) {
if(start > end) {
return -1;
} else {
int mid = (start+end)/2;
if(flag == arr[mid]) {
return mid;
} else if(flag < arr[mid]) {
return binSearchReturn(flag,arr,start,mid - 1);
} else {
return binSearchReturn(flag,arr,mid + 1,end);
}
}
}
}