1 Public classQuickSort {2 3 //Insert Sort4 //The sequence before insertion is sorted, comparing the newly inserted value with the previous value5 //until we find the right place.6 Public Static int[] QuickSort (int[] arr) {7 8 for(intj=1;j<arr.length;j++){9 intKey =Arr[j];Ten inti = J-1; One A while(I>=0 && arr[i]<key) { -ARR[I+1] =Arr[i]; -i = I-1; theARR[I+1] =key; - } - } - + returnarr; - } + A //query whether a value V is within the array, or output ' NIL ' if the subscript is output in the array at Public StaticString Quicksortin (int[] arr,intV) { -list<integer> list =NewArraylist<integer>(); - for(inti = 0;i<arr.length;i++){ - if(V = =Arr[i]) { -List.add (i);//if V is inside the array, the subscript is stored in the list - } in } - to if(List! =NULL&&!list.isempty ()) {//If the list is not empty, the loop output + -StringBuilder str =NewStringBuilder (""); the for(inti = 0; I < list.size (); i++){ *Str.append ("V =" + "arr[" +list.get (i) + "]"); $ }Panax Notoginseng returnstr.tostring (); - the}Else{//If list is empty, output ' NIL ' + return"NIL"; A } the } + - //Select Sort $ Public Static voidSelectsort (int[] a) { $ intI//the end position of the ordered area - intJ//starting position of the unordered area - intMin//the smallest element position in an unordered region the - for(i=0; i<a.length; i++) {Wuyimin=i; the - //find the smallest element between "a[i+1] ... a[n]" and assign it to Min. Wu for(j=i+1; j<a.length; J + +) { - if(A[j] <A[min]) Aboutmin=J; $ } - - //if min!=i, then Exchange A[i] and A[min]. - //after the exchange, the elements that guarantee the a[0] ... a[i] are ordered. A if(min! =i) { + intTMP =A[i]; theA[i] =A[min]; -A[min] =tmp; $ } the } the } the the //querying the maximum value of an array - Public Static intSelectmax (int[] arr) { in intmax = Arr[0]; the for(intI =0;i<arr.length;i++){ the if(arr[i]>max) { AboutMax =Arr[i]; the } the } the returnMax; + } - the //the minimum value of the query arrayBayi Public Static intSelectmin (int[] arr) { the intMin = arr[0]; the for(intI =0;i<arr.length;i++){ - if(arr[i]<min) { -Min =Arr[i]; the } the } the returnmin; the } - the the Public Static voidMain (string[] args) { the int[] AAA = {1,5,2,3,4,55,11,22,33,4,22,1};94 int[] bbb = {31,41,59,26,41,58}; the Selectsort (BBB); the for(inta:bbb) { theSystem.out.print (A + "");98 } About System.out.println (); -System.out.println (Quicksortin (bbb,41));101 System.out.println (Selectmin (BBB));102 }103}
Java Code Test---Insert sort and select sort