Three ways to sort Ava array elements

Source: Internet
Author: User

In Java, when using array to sort the function, there are four kinds of methods: fast sorting method, bubbling method, choosing Sort method, inserting sort method.
The fast sorting method mainly uses a method arrays.sort () implementation in arrays.
The bubbling method uses a traversal array to compare the minimum or maximum values by a continuous comparison.
The selection sort method is to use the first data of the array as the maximum or minimum value, and then to output an ordered array by comparing loops.

package com.study.test;import java.util.arrays;public class testarrsort {     public static void main (String[] args)  {         sortarr ()  ;    }    public static  Void sortarr () {        int arr[] = { 3,  5, 2, 11};        arrays.sort (arr);         system.out.println (arrays.tostring (arr));    }     //  sort the array, the Bubbling method     //  bubble method is used to compare the traversal arrays, and the minimum or maximum value is traversed by a constant comparison. &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;SORTARR1 ()  {         int arr[] = { 3, 5, 2, 11, 35, 20, 15  };        for  (int i = 0; i < arr.length; i++)  {             for  (int j = i +  1; j < arr.length; j++)  {                 if  (Arr[j] < arr[i])  {                      int tmp = 0;                     tmp = arr[j];                     arr[j] = arr[i];                      arr[i] = tmp;                }             }             system.out.println (Arr[i]);        }     }    //  sorting by array, sorting method     //  The selection sort method is to use the first data of the array as the maximum or minimum value, and then to output an ordered array by comparing loops. &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;VOID&NBSP;SORTARR2 ()  {         int arr[] = { 3, 5, 2, 11, 35, 20,  15,200,99 };        for  (int i = 0; i  < arr.length ; i++)  {             int min = i;             for  (int j = i + 1; j < arr.length; j++)  {                 if  (arr[min] > &NBSP;ARR[J])  {                     min = j;                 }            }             if  (min != i)  {                 int temp  = arr[i];                 arr[i] = arr[min];                 arr[min] = temp;            }                          system.out.println (Arr[i]);        }     }}


This article is from the "Sima" blog, make sure to keep this source http://9274590.blog.51cto.com/9264590/1761086

Three ways to sort Ava array elements

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.