Algorithm Note _008: Select sort and Bubble sort "brute force method"

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

2.1 Selection Sorting principle Introduction

2.2 Specific encoding (select sort)

Introduction to 2.3 bubble sorting principle

2.4 Specific coding (bubble sort)

1 problem description

given a sortable sequence of n elements (for example, numbers, characters, and strings), rearrange them in a non-descending manner.

2 Solutions 2.1 Selection Sorting principle Introduction

at the beginning of the selection sort, we scan the entire list from the first element, find its smallest element, exchange it with the first element, swap the minimum element with the first element, and then we scan the remaining n-1 elements from the second element to find the n-1 The smallest element in the element, exchanging the smallest element with the second element, and then starting the scan from the third element ... in general, it is from the first I element to start scanning, find the first n-i+1 The smallest element in the element, the smallest element and the first I The position of the element exchange. In this way, after the n-1 Traversal, the list is sorted out.

2.2 Specific encoding (select sort)
 PackageCom.liuzhen.chapterThree; Public classSelectionsort { Public Static voidGetselectionsort (int[] a) {        intmin = 0;//for storing the smallest element ordinal in the n-i sequence        inttemp = 0;//swapping intermediate variables for array element values//print output unsorted before array sequenceSystem.out.print ("Before sorting:");  for(intp = 0;p < a.length;p++) System.out.print (A[p]+ "\ T");                System.out.println ();  for(inti = 0;i < a.length-1;i++) {min=i;  for(intj = I+1;j < a.length;j++){                if(A[j] <a[min]) min=J; }            //values for interchange a[i] and A[min]temp =A[i]; A[i]=A[min]; A[min]=temp; //Print output Each time you select the sort resultSystem.out.print ("Sort" + (i+1) + "Trip:");  for(intp = 0;p < a.length;p++) System.out.print (A[p]+ "\ T");        System.out.println (); }    }         Public Static voidMain (String args[]) {int[] A = {89,45,68,90,29,34,17};    Getselectionsort (a); }}

Operation Result:

Before sorting: 89 45 68 90 29 34 17sort the 1th trip: -45 68 90) 29 34 thesort the 2nd trip:17 in68 90 $34 89sort the 3rd trip:17 29 the90 45 the89sort the 4th trip:17 29 34 $     -68 89sort the 5th trip:17 29 34 45   about89sort the 6th trip:17 29 34) 45 68   the        -

Introduction to 2.3 bubble sorting principle

we start with the first element of the list, comparing the two adjacent elements in the list, and if the first element is greater than the second element, the position of the two elements is swapped, otherwise the previous action is repeated starting at the second element position. Once repeated, the largest element sinks to the last position in the list. This is done until n-1 times, and the list is sorted out.

2.4 Specific coding (bubble sort)
 PackageCom.liuzhen.chapterThree; Public classBubblesort { Public Static voidGetbubblesort (int[] a) {        inttemp; //print output unsorted before array sequenceSystem.out.print ("Before sorting:");  for(intp = 0;p < a.length;p++) System.out.print (A[p]+ "\ T");                    System.out.println ();  for(inti = 0;i < a.length-1;i++){                         for(intj = 0;j < a.length-1-i;j++){                if(A[j+1] <A[j]) {                    //values for interchange a[j] and a[j+1]temp =A[j]; A[J]= A[j+1]; A[j+1] =temp; }            }            //Print output Each time you select the sort resultSystem.out.print ("Sort" + (i+1) + "Trip:");  for(intp = 0;p < a.length;p++) System.out.print (A[p]+ "\ T");        System.out.println (); }    }         Public Static voidMain (String args[]) {int[] A = {89,45,68,90,29,34,17};    Getbubblesort (a); }}

Operation Result:

Before you sort: the        1th trip:about    34.    -     -     Sort 2nd:All-in-a-            sort 3rd trip:45    the            4th trip:    A.   The 5th of the                  89-in-A-sort tour:     -     Sort 6th trip:        A.    

Algorithm Note _008: Select sort and Bubble sort "brute force 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.