JAVA Implementation of sorting by selection

Source: Internet
Author: User

Selecting sorting is also a simple sorting method, and its principle is also easy to understand. It is the same as the number of comparisons for Bubble sorting, but the number of exchanges for sorting is less than that for Bubble sorting. Bubble Sorting is after each comparison. If the order of the two elements to be compared is the opposite of the order to be sorted, the two elements to be compared are exchanged, however, when sorting is selected, only the subscript of the smallest element is recorded in each traversal process. After all the comparisons are completed, the minimum element is exchanged with the first element of the unordered sequence, this reduces the number of exchanges and improves the sorting efficiency.

The Java implementation code is as follows:

Public class chosesort {// constructor without parameterspublic chosesort () {}; // constructor with parameterspublic int [] chosesort (INT [] intarr) {for (INT I = 0; I <intarr. length; I ++) {int lowindex = I; for (Int J = I + 1; j <intarr. length; j ++) {If (intarr [J] <intarr [lowindex]) {lowindex = J ;}} // exchange the current first element with the smallest element in the sequence following it, that is, place the smallest element in the front-end int temp = intarr [I]; intarr [I] = intarr [lowindex]; intarr [lowindex] = temp;} return intarr;} public static void main (string [] ARGs) {chosesort = new chosesort (); int [] intarr = {5, 6, 9, 2, 4, 3, 8}; int [] intarraftersort = chosesort. chosesort (intarr); For (INT arritem: intarraftersort) {system. out. print (arritem + "");}}}

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.