Simple numeric sorting algorithm bubble, select, direct Insert sort

Source: Internet
Author: User

1. Select the Sorting method

  

Find the first small number, put it in the first position, find the second smallest number, and in the second position, find the minimum value for each iteration. Look for a number at a time so that all the values will follow
The order is lined up well. Note that unlike bubbling, values are not exchanged during traversal. The end of a traversal is not exchanged.
 Packagecom.sphere.algorith.design;Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {int[] arr = {3,6,1,7,9,-8,2,5}; System.out.println ("Before sorting:" +arrays.tostring (arr)); int[] Sort_arr =Selection_sort (arr); System.out.println ("After sorting:" +arrays.tostring (Sort_arr)); }    //Select Sort Method     Public Static int[] Selection_sort (int[] Array) {                if(Array = =NULL|| Array.Length = = 0) {System.out.println ("The sorted array is empty!" "); return NULL; }        //Array Length        intLen =Array.Length; //Loop Counter        intI, J; intMinValue = 0;//record the minimum value in a traverse        intindex = 0;//index value that records the minimum value                 for(i = 0; i < len-1; i++) {MinValue= Array[i];//assuming that the first element is the smallestindex =i;  for(j = i+1; J < Len; J + +) {                if(Array[j] <minValue) {MinValue= Array[j];//each time the remaining minimum value and its subscript are obtained. index =J; }} Array[index]= Array[i];//at the end of a traversal, exchange the smallest value with the first elementArray[i] =MinValue; }        returnArray; }}

Before sorting: [3, 6, 1, 7, 9,-8, 2, 5]

After sorting: [-8, 1, 2, 3, 5, 6, 7, 9]

Simple numeric sorting algorithm bubble, select, direct insert 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.