数组基本操作
开始学习JAVA,为了熟悉java的语言环境,于是采用很笨拙的方式,编写数组的基本排序算法。
1 package javabase; 2 3 public class Arraybase { 4 public static void main(String[] args){ 5 int[] array = {11,13,2,5,4,6,8,7,9,11,20,18,19}; 6 int[] array_1 = {1,2,3,4,5,6,7,8,9,10}; 7 printArray(array); 8 sumArray(array); 9 getMax(array); 10 getMin(array); 11 selectSort(array); 12 bubbleSort(array); 13 insectSort(array); 14 shellSort(array); 15 quickSort(array); 16 binarySearch(array_1,10); 17 } 18 19 20 /* 21 * 定义数组输出功能 22 */ 23 public static void printArray(int[] arr){ 24 System.out.print("["); 25 for(int i=0;i<arr.length;i++){ 26 if(i==arr.length-1){ 27 System.out.println(arr[i]+"]"); 28 } 29 else System.out.print(arr[i]+", "); 30 } 31 } 32 /* 33 * 定义数组求和功能 34 * 35 */ 36 public static void sumArray(int[] arr){ 37 int sum=0; 38 for(int i=0;i<arr.length;i++){ 39 sum+=arr[i]; 40 } 41 System.out.println(sum); 42 } 43 /* 44 * 获取最值,包括最大值和最小值 45 */ 46 public static void getMax(int[] arr){ 47 int max = arr[0]; 48 for(int i=1;i<arr.length;i++){ 49 if(max<arr[i]) max=arr[i]; 50 } 51 System.out.println(max); 52 } 53 public static void getMin(int[] arr){ 54 int min = arr[0]; 55 for(int i=1;i<arr.length;i++){ 56 if(min>arr[i]) min=arr[i]; 57 } 58 System.out.println(min); 59 } 60 /* 61 * 交换数组元素 62 */ 63 public static void swapArray(int[] arr, int firstIndex, int lastIndex){ 64 int temp=arr[firstIndex]; 65 arr[firstIndex]=arr[lastIndex]; 66 arr[lastIndex]=temp; 67 } 68 /* 69 * 对数组进行选择排序 70 */ 71 static void selectSort(int[] arr){ 72 for(int i=0;i<arr.length-1;i++){ 73 int key=i; 74 for(int j=i+1;j<arr.length;j++){ 75 if(arr[key]>arr[j]){ 76 key=j; 77 } 78 } 79 if(key!=i){ 80 swapArray(arr,i,key); 81 } 82 } 83 printArray(arr); 84 } 85 /* 86 * 对数组进行冒泡排序 87 */ 88 static void bubbleSort(int[] arr){ 89 for(int i=0;i<arr.length-1;i++){ 90 for(int j=0; j<arr.length-1-i;j++){ 91 if(arr[j]>arr[j+1]){ 92 int temp=arr[j]; 93 arr[j]=arr[j+1]; 94 arr[j+1]=temp; 95 } 96 } 97 } 98 printArray(arr); 99 100 } 101 /* 102 * 对数组进行插入排序 103 */ 104 static void insectSort(int[] arr){ 105 for(int i=1;i<arr.length;i++){ 106 int temp=arr[i]; 107 int j=i-1; 108 while(j>=0&&temp<arr[j]){ 109 arr[j+1]=arr[j]; 110 j--; 111 } 112 arr[j+1]=temp; 113 } 114 printArray(arr); 115 } 116 /* 117 * 对数组进行shell排序 118 */ 119 static void shellSort(int[] arr){ 120 for(int h=arr.length/2;h>0;h/=2){ 121 for(int i=h;i<arr.length;i++){ 122 int j=i%h; 123 while(arr[j]<arr[i]) j+=h; 124 int temp=arr[i]; 125 for(int k=i-h;k>=j;k-=h){ 126 arr[k+h]=arr[k]; 127 } 128 arr[j]=temp; 129 } 130 } 131 printArray(arr); 132 } 133 /* 134 * 对数组进行快速排序 135 */ 136 static void quickSort(int[] arr){ 137 int low=0, high=arr.length-1; 138 int key=arr[low]; 139 while(low!=high){ 140 while(arr[high]>key&&high>low) high--; 141 arr[low]=arr[high]; 142 while(arr[low]<key&&low<high) low++; 143 arr[high]=arr[low]; 144 } 145 arr[high]=key; 146 printArray(arr); 147 } 148 /* 149 * 对数组二分查找排序 150 */ 151 static int binarySearch(int[] arr,int num){ 152 int low=0,high=arr.length-1; 153 while(low<=high){ 154 int middle=(low+high)>>1; 155 if(num==arr[middle]){ 156 System.out.print(middle); 157 return middle;} 158 else if(num>arr[middle]){ 159 low=middle+1; 160 }else high=middle-1; 161 } 162 return -1; 163 } 164 165 166 }
个人觉得帮助还是蛮大的,也请论坛的牛人推荐下学习java的好的方法。

浙公网安备 33010602011771号