About selection sorting in Java and bubble sorting method

Source: Internet
Author: User

One, the method is to directly pass in an array to sort (select Sort method)

public static void Selectsort (Int. arr[]) {for    (int i = 0; i < arr.length-1; i++) {for (int j = i+1; J < Arr.leng Th J + +) {if (Arr[j]<arr[i]) {int temp=arr[i];arr[i]=arr[j];arr[j]=temp;}}}    

Two, this kind of thing passed in an array and an array number (select Sort method)

public static void Sel_sort (int[] x, int n) {          int k, I, M, T;          for (k=0; k<n-1; k++) {  //multiple-trip sort               m = k;              for (i=k+1; i<n; i++) {                  if (X[i] < x[m])                m = i;            }              t = x[k];              X[K] = x[m];              X[m] = t;          }      }

Three, bubble Sort method

private static void bubble sort (int[] arr) {for (int j = 0; J < Arr.length-1; J + +) {for (int i=0;i<arr.length-1-j;i++) {if (AR R[i]>arr[i+1]) {int temp=arr[i];arr[i]=arr[i+1];arr[i+1]=temp;}}}}

About selection sorting in Java and bubble sorting method

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.