冒泡排序
- class bubbleSort_Test{
- public static void main(String[] args){
- int[] arr = {24, 69, 80, 57, 13};
- bubbleSort(arr);
- print(arr);
- }
- public static void bubbleSort(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 temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- public static void print(int[] arr){
- for(int i = 0;i < arr.length;i++){
- System.out.print(arr[i] + " ");
- }
- }
- }
复制代码
选择排序
- class selectSort_Test{
- public static void main(String[] args){
- int[] arr = {24, 69, 80, 57, 13};
- bubbleSort(arr);
- print(arr);
- }
- public static void print(int[] arr){
- for(int i = 0;i < arr.length;i++){
- System.out.print(arr[i] + " ");
- }
- }
- public static void selectSort(int[] arr){
- for(int i = 0;i < arr.length-1;i++){
- for(int j = i+1;j < arr.length;j++){
- if(arr[i] > arr[j]){
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- }
复制代码
二分超找
- class binarySearch_Test{
- public static void main(String[] args){
- int[] arr = {24, 69, 80, 57, 13};
- System.out.println(arr,80);
- }
- public static int binarySearch(int[] arr,int value){
- int min = 0;
- int max = arr.length - 1;
- int mid = (min + max) / 2;
- while(arr[mid] != value){
- if(arr[mid] < value){
- min = mid + 1;
- }else if(arr[mid] > value){
- max = mid - 1;
- }
- mid = (min + max) / 2;
- if(min > max){
- return -1;
- }
- }
- return mid;
- }
- }
复制代码 |
|