Brute Force Method-select sort, bubble sort

Source: Internet
Author: User

Time always makes me feel a sense of frustration after knowing, perhaps because I never want to record the past.

3.1.1 Select sort (n elements, 0~n-1, ascending, unstable)

Make an I scan of array A (0<=i<=n-2), find the smallest element each time from the last N-i element, and then swap it with the AI.

Code implementation
/*** Select sort (Ascending) *@paramarrays sorted by array **/     Public Static voidSelectsort (int[] Array) {         for(inti=0;i<array.length-1;i++){            intmin=i;  for(intj=i+1;j<array.length;j++){                if(array[min]>Array[j]) {min=J; }            }            if(min!=i) {                inttemp=Array[i]; Array[i]=Array[min]; Array[min]=temp; }        }    }
Algorithm analysis

Therefore, for any input, the selection sort is an θ (N2) algorithm.

3.1.1 Bubble sort (n elements, 0~n-1, ascending, stable)

Compare adjacent elements Aj and aj+1, if aj>aj+1, swap their positions, in turn, and finally, the largest element "sinks" to the last position. After repeating the n-1, the order is lined up.

Code implementation
/*** Bubble Sort (Ascending) *@paramarrays sorted by array **/     Public Static voidBubblesort (int[] Array) {         for(inti=0;i<array.length-1;i++){             for(intj=0;j<array.length-1-i;j++){                if(array[j]>array[j+1]){                    inttemp=Array[j]; ARRAY[J]=array[j+1]; Array[j+1]=temp; }            }        }    }

Algorithm analysis

The worst case and average are θ (n2). In fact, it is possible to make some improvements to the above code, and when the array is iterated over again without exchanging elements, then it is already sorted. The code demonstrates the following:

Code implementation

/*** Bubble Sort (Ascending) *@paramarrays sorted by array **/     Public Static voidBubblesort (int[] Array) {         for(inti=0;i<array.length-1;i++){            intCount=0;  for(intj=0;j<array.length-1-i;j++){                if(array[j]>array[j+1]){                    inttemp=Array[j]; ARRAY[J]=array[j+1]; Array[j+1]=temp; Count++; }} System.out.println ("First" + (i+1) + "iterations:"); print (array);//Output Array            if(count==0){                 Break; }        }    }

The result of the original output:

before sorting:11,41,48,3,73,12,40,48,70,37 1th iteration:11,41,3,48,12,40,48,70,37,73 2nd iteration: 11,3,41,12,40,48,48,37,70,73 3rd Iteration:3,11,12,40,41,48,37,48,70,73 4th iteration: 3,11,12,40,41,37,48,48,70,73 5th iteration:3,11,12,40,37,41,48,48,70,73 6th iteration: 3,11,12,37,40,41,48,48,70,73 7th iteration:3,11,12,37,40,41,48,48,70,73 8th iteration: 3,11,12,37,40,41,48,48,70,73 9th iteration:3,11,12,37,40,41,48,48,70,73 after sorting: 3,11,12,37,40,41,48,48,70,73

The result of the improved output:

before sorting:11,41,48,3,73,12,40,48,70,37 1th iteration:11,41,3,48,12,40,48,70,37,73 2nd iteration: 11,3,41,12,40,48,48,37,70,73 3rd Iteration:3,11,12,40,41,48,37,48,70,73 4th iteration: 3,11,12,40,41,37,48,48,70,73 5th iteration:3,11,12,40,37,41,48,48,70,73 6th iteration: 3,11,12,37,40,41,48,48,70,73 7th iteration:3,11,12,37,40,41,48,48,70,73 after sorting: 3,11,12,37,40,41,48,48,70,73
As you can see
, it is true that two iterations are missing when sorting the above array. But in fact, even in the elementary sorting method, bubble sort is not a good choice, and if it is not because it has a good name, we probably won't have any knowledge of it (this is not very cruel, the author of the book said).
Exercise 3.1

4. A. Design a brute force algorithm that calculates the values of the following polynomial for a given x0:

P (x) =anxn+an-1xn-1+...+a1x+a0

and determine the worst efficiency type of the algorithm.

B. If the algorithm you are designing belongs to Θ (N2), design a linear algorithm for the problem.

C. Can you design an algorithm that is better than linear efficiency for this problem?

Solution:

A. Code implementation:

/*** Exercise 4.a Computational polynomial P (x) *@paramconstant entry for array polynomial *@paramx Variable *@returnreturns the result of the calculation **/     Public Static intPolynomialevaluation (int[] Array,intx) {        intResult=0;  for(inti=0;i<array.length;i++){            inttemp=Array[i];  for(intj=0;j<i;j++) {Temp*=x; } result+=temp; }        returnresult; }

Efficiency analysis:

So the algorithm belongs to Θ (N2).

B. Code implementation:

/*** Exercise 4.b Computational polynomial P (x) *@paramconstant entry for array polynomial *@paramx Variable *@returnreturns the result of the calculation **/     Public Static intPolynomialevaluation (int[] Array,intx) {        intResult=0; intTemp=1;  for(inti=0;i<array.length;i++) {result+=array[i]*temp; Temp*=x; }        returnresult; }

C. Because the problem must solve the n+1 of the addend, so we can not design a better than linear efficiency algorithm.

Brute Force Method-select sort, bubble 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.