"JAVA" algorithm sort (i)--select Sort method

Source: Internet
Author: User

First, preface

In the previous article we have a summary of all the sorting algorithm, the following small series and to share with you the choice of sorting algorithm.

Ii. Core points of knowledge

The core idea of choosing sort is that I am the biggest .

Select Sort (Selection sort) is a simple and intuitive sorting algorithm. Regardless of the language of the textbook, will be used to select the sorting algorithm to compare size. Although the code is a few lines, it is important to understand how it is thought. Here are some of the small series of their own knowledge:

It works by selecting the smallest (or largest) element of the data element to be sorted each time, storing it at the beginning of the sequence until all the data elements to be sorted are finished.

Eg: for example, from large to small, take a value A1, and then A2-an compared with other values, if A1 < A2, then the value of A2 assigned to A1,A1 value to A2, so that the completion of a message Exchange-the final A1 is the largest.

                                                 图一 依次比较

                                                 图二 核心思想图
Iii. Examples of thinking

Example: Randomly enter 4 integers, then they are sorted from large to small.

thinking: using the choice of sorting method, from the outer loop first find a A0, in turn, and the internal cycle of comparison, who replaced who big.

                                                 图三 运算思路

Java code:

Publicclass Testbubblesort{ Public Static void Main(string[] args){//define array of numbersaThe size andargsThe same lengthint[]a=New int[args.length]; To giveaAssignment, value, andargsEqual for (int i=0; i<args.length; i++){//Convert string to Integer dataa[I] =Integer.parseint(Args[i]); }//Select sort algorithm from large to smallSelectionsort(a);System. out.println("Select Sort algorithm from large to small:");Show(a); }//Select sort Public Static void Selectionsort(int a[]){//Set up an outer loop, first find the first value 0, loop in turn. for (int i=0; i<a.length; i++){//inner loop, after loop, assign the maximum value to the outer loop value. for (int j=i+1; j<a.length; j + +){if (A[i]<a[j]){int Temp=a[I];a[I] =a[J];a[J] =Temp; }}}}//Display the contents of the array Public Static void Show(int a[]){//Display the contents of an array for (int i=0; i<a.length; i++){System. out.Print(a[i]+""); }    }}

Results show:

                                                 图三 结果展示
Iv. Summary

Code like life! In short, each algorithm is a connotation, to better understand the algorithm, it is necessary to practice a lot, but also to understand the deep, understanding the inside of the idea. Finally, come on! ~~

"JAVA" algorithm sort (i)--select Sort method

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.