[Java] common Java sorting algorithms

Source: Internet
Author: User
/***** @ Author Loger **/public class sort {/*** select sorting * @ Param array * @ return */Public int [] selectsort (INT array []) {int out, in; int min; For (out = 0; out <array. length-1; out ++) {min = out; for (in = out + 1; in <array. length; In ++) {If (array [in] <array [Min]) {min = in ;}} if (Min! = Out) {array [out] = array [out] + array [Min]; array [Min] = array [out]-array [Min]; array [out] = array [out]-array [Min] ;}} return array ;} /*** bubble sort ** @ Param array * @ return */Public int [] bubblesort (INT array []) {for (INT I = 0; I <array. length-1; I ++) {for (Int J = I; j <array. length-i-1; j ++) {If (array [J]> array [J + 1]) {array [J] = array [J] + array [J + 1]; array [J + 1] = array [J]-array [J + 1]; array [J] = array [J]-array [J + 1] ;}} return array;}/**** insert sorting * @ Param array * @ return */Public int [] insertsort (INT array []) {for (INT I = 1; I <array. length; I ++) {int insert = array [I]; // The element to be inserted Int J = I-1; while (j> = 0 & Insert <array [J]) {array [J + 1] = array [J]; j --;} array [J + 1] = insert;} return array;}/*** main function * @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubint [] array = {1, 3, 2, 9, 8, 7, 10}; sort = new sort (); system. out. println ("original array:"); For (int A: array) {system. out. print (a + "");} // system. out. println ("\ n select sorting:"); // For (int A: sort. selectsort (array) {// foreach prints the array // system. out. print (a + ""); //} // system. out. println ("\ n Bubble Sorting:"); // For (int A: sort. bubblesort (array) {// foreach prints the array // system. out. print (a + ""); //} system. out. println ("\ n insert sorting:"); For (int A: sort. insertsort (array) {// foreach prints the array system. out. print (a + "");}}}

 

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.