Java implementation algorithm--select sort

Source: Internet
Author: User

The choice of sorting is also a relatively simple sort method, the principle is also relatively easy to understand, it is the same as the bubble sort, but the choice of the number of exchanges less than the bubble sort. Bubble sort is after each comparison, if the comparison of the two elements in the order of the order to be sorted in reverse, then to be exchanged, and the selection of the sequence is recorded only the smallest element of the subscript, after the end of all comparisons, the smallest element with the unsorted part of the sequence of the first element of the exchange, This reduces the number of exchanges and improves the sorting efficiency.


Take a look at the implementation code:

public class Choicesort {public static void _choicesort (integer[] a) {if (a = = NULL | | a.length <= 0) {return;} for (int i = 0; i < a.length-1; i++) {//Why here-1? Think about int min = i; /* Define the current subscript as the minimum subscript */for (int j = i + 1; j < A.length; J + +) {if (A[min] > A[j]) {/* If there is a keyword less than the current minimum value */min = j;/* This key The subscript of the word assigns a value to min */}}if (i! = min) {/* if min is not equal to I, the description finds the minimum value, interchange */int tmp = a[min];a[min] = a[i];a[i] = tmp;}}}

To write a test method:

public static void Main (string[] args) {integer[] list={49,38,65,97,76,13,27,14,10};                  Select Sort Choicesort._choicesort (list);         for (int i=0;i<list.length;i++) {System.out.print (list[i]+ "");    } System.out.println (); }

Output Result:


Favorite friends pay attention to me! Thanks for your support!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java implementation algorithm--select sort

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.