Java Select sort and Bubble sort

Source: Internet
Author: User

Java Select sort and Bubble sort

1.datasorter.java

public class Datasorter {//Bubble sort method//main idea: Sort by ascending order, array element 22 comparison, large immediately after public static void Bubblesort (int[] a) {for (int i = 1; i < A.length; i++) {//control outer loop count for (int j = 0; J < A.length-i; J + +) {if (A[j] > A[j+1]) {//array element 22 comparison, large immediately trailing swap (A, J, j+1);} }}}//direct selection of the sorting method//main idea: in ascending order, each cycle to find the maximum number, put him in the last position of public static void Selectsort (int[] a) {int index;//Save the subscript for each comparison of the maximum value; for ( int i = 1; i < a.length; i++) {//control outer loop Count index = 0;for (int j = 1; J <= A.length-i; j + +) {if (A[j] > A[index]) {index = j;}} Swap (A, index, a.length-i);}} Swap array elements private static void swap (int[] A, int x, int y) {int tmp = a[x];a[x] = a[y];a[y] = tmp;} Output array element public static void show (int[] a) {for (int i = 0; i < a.length; i++) {System.out.println (a[i]);}}

  

2. Test class Test.java

public class Test {public static void main (string[] args) {int [] a = {9,2,1,8,0,3};D atasorter.selectsort (a);D atasorter.sh Ow (a);}}

  

3. Test results:

Java Select sort and Bubble sort

Related Article

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.