Choosing a sorting algorithm Java and Python implementations

Source: Internet
Author: User

Java implementation

 PackageCommon; Public classsimplearithmetic {/*** Select sort * Input shaping array: A[n] "4, 5, 3, 7" * 1. The array value of the array number I (I belongs to [0, N-2]) is a[i], i.e. the first cycle * 2. Assuming a[i] is an array a[k] (k belongs to [i,n- 1]), that is, the initialization min =i * 3 is performed at the minimum value of a[min]. The array value of array number m (m belongs to [i+1,n-1]) is a[m], which is the second cycle * 4. If A[M] < a[min], min = m; min always records the minimum value in A[k] * 5. When min! = I, that is, the second step of the Assumption A[i] is a[m] The minimum value is not true, you need to swap the value of a[min] with A[i] *@paramarr Shaping Array*/     Public Static voidSelectsort (int[] arr) {        intn =arr.length; intMin = 0; intTMP = 0;  for(inti = 0; i < n-1; i++) {min=i;  for(intm = i + 1; M < n; m++){                if(Arr[m]<arr[min]) min =m; }            if(I! =min) {tmp=Arr[i]; Arr[i]=Arr[min]; Arr[min]=tmp; }//End of If}//End of for            }     Public Static voidMain (string[] args) {//TODO Auto-generated method stubs        int[] arr = {12,23,9,24,15,3,18};                        Simplearithmetic.selectsort (arr);  for(inti = 0; i < arr.length; i++) {System.out.print (Arr[i]+" "); }        //Output 3 9    }}

Python implementation

Python 2.7.9 (default, Dec, 12:24:55) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "license ()"  forMore information.>>>classsimplearithmetic:defSelectsort (Self,arr): V_len=Len (arr) v_index_i=0 whileV_index_i! = v_len-1: I=Arr[v_index_i] V_min=I forMinchArr[v_index_i+1: V_len]:ifM <V_min:v_min=mifI! =v_min:v_tmp=Arr.index (v_min) arr[v_index_i]=v_min Arr[v_tmp]=I v_index_i= v_index_i + 1>>> sa =simplearithmetic ()>>> arr = [12,23,9,24,15,3,18]>>>Sa.selectsort (arr)>>>arr[3, 9, 12, 15, 18, 23, 24]>>>

Choosing a sorting algorithm Java and Python implementations

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.