Java Code Test---Insert sort and select sort

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.