Using decomposition method to learn algorithm 001--select sort

Source: Internet
Author: User

When we are learning algorithms, one of the problems that we often encounter is that we can read but not write. The reason is that there is no understanding in place.

So how can you understand the position? I think the key is "decomposition", that is, the algorithm node in the hierarchy and steps to break out.

Step-by-step implementation.

Today, let's break down one of the algorithms in the sorting algorithm--select sort.

Why is it called the selection sort?

One of the simplest sorting algorithms is this: first, find the smallest element in the array, and second, swap it with the first element of the group (if the first element is the smallest element then it swaps with itself). Again, find the smallest element in the remaining element and swap it with the second element of the array. This is repeated until the entire array is sorted. This method is called Select Sort.

You can see that the key to selecting a sort is to select minimum value. This name is the core idea of this algorithm. Let's look at the implementation of a complete algorithm first.

        Select sort public static void SelectSort0 (int[] a) {int min=0;for (int i=0;i<a.length;i++) {min=i;for (int j=i+1;j< a.length;j++) {if (A[min]>a[j]) {min=j;}} int tmp=0;tmp=a[i];a[i]=a[min];a[min]=tmp;} for (int i=0;i<a.length;i++) {System.out.print (A[i] + "");}}

This algorithm, after reading more easy to understand, but not remember. I will disassemble this algorithm in the following way.

First step: Find the minimum (index)

Given an array from which to find the first occurrence of the smallest value (its index), how can this algorithm be implemented?

I want to use a loop to get it done, the code is as follows:

public static int Findminindex (int[] a) {int min=0;for (int i=0;i<a.length;i++) {if (A[min]>a[i]) {min=i;}} return min;}

The idea of sorting is to find the smallest index of the yourselves sequence, and to exchange the elements of the current starting index, each time it is traversed.

So we need an overloaded version that can specify the "starting index" for itself:

public static int Findminindex (int[] a,int beginindex) {int min=beginindex;for (int i=beginindex;i<a.length;i++) {if ( A[min]>a[i]) {min=i;}} return min;}

Where the second parameter is the index of the start traversal of array A.

Step two: Swap index elements

This is very simple, directly on the code

public static void Exchange (int[] A,int i,int j) {int temp=a[i];a[i]=a[j];a[j]=temp;}

Step three: Make a combination

In the main method of selecting a sort, the array is traversed once. Each iteration, the element that finds the smallest value corresponds to the index, and the current starting index of the element is exchanged. After the traversal is complete, the array is sorted.

public static void Selectsort (int[] a) {for (int i=0;i<a.length;i++) {int Beginindex=i;int min = Findminindex (A, BEGININDEX); Exchange (A,min,i);}}

The complete code is as follows:

Package Asen.yang;public class Selectsort {public static void main (string[] args) {//TODO auto-generated method stubint[] a={5,1,4,8,3,9,0,2,7,6,1,2,5}; Selectsort (a); for (int i=0;i<a.length;i++) {System.out.print (a[i]+ "");}} public static int Findminindex (int[] a) {int min=0;for (int i=0;i<a.length;i++) {if (A[min]>a[i]) {min=i;}} return min;} public static int Findminindex (int[] a,int beginindex) {int min=beginindex;for (int i=beginindex;i<a.length;i++) {if ( A[min]>a[i]) {min=i;}} return min;} public static void Exchange (int[] A,int i,int j) {int temp=a[i];a[i]=a[j];a[j]=temp;} public static void Selectsort (int[] a) {for (int i=0;i<a.length;i++) {int Beginindex=i;int min = Findminindex (A, BEGININDEX); Exchange (A,min,i);}} Select sort public static void SelectSort0 (int[] a) {int min=0;for (int i=0;i<a.length;i++) {min=i;for (int j=i+1;j< a.length;j++) {if (A[min]>a[j]) {min=j;}} int tmp=0;tmp=a[i];a[i]=a[min];a[min]=tmp;} for (int i=0;i<a.length;i++) {System.out.print (A[i] + "");}}}

When learning the algorithm, if the final code is not clear, then you can try to use the decomposition of the way to remember its key points. And then combine.

For classic algorithms such as sorting, almost all of the development languages have been implemented in the class library. The purpose of our study is not only to repeat what has been achieved, but more importantly, to master thought in learning. And thought, more embodied in the key points. Therefore, to decompose the algorithm, to find the key point, understand the core idea, is the learning algorithm, should focus on the point of view.

The next step is to decompose the other algorithms according to this idea. From this point of view, in the process of learning algorithms, there will be more gains.

Using decomposition method to learn algorithm 001--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.