Java implementation selection sorting

Source: Internet
Author: User


public static void Main (string[] args) {//array to sort int[] arr = {2, 5, 7, 1, 3, 8, 6, 4};//controls the subscript of each element in the array, the element symbol to be compared for the t i = 0; i < arr.length; i++) {//operation subscript for all elements compared with I element for (int j = i + 1; j < Arr.length; J + +) {//if arr[i] greater than arr[j]if (Arr[i] > Arr[j]) {//define temporary variable save Stored arr[i] and arr[j], the larger value of the int tmp = arr[i];//compares the two value of the interchange position arr[i] = arr[j];arr[j] = tmp;}}} for (int i = 0; i < arr.length; i++) {//output sorted from small to large order System.out.println (Arr[i]);}}


Sorting principle: The leftmost element is compared to all the elements on the right, and if the condition is met the position of the element on the two positions is exchanged, the maximum value is the leftmost.



Java implementation selection sorting

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.