A few common algorithms

Source: Internet
Author: User
Tags ming

Today mainly talk about the common algorithm, so I Baidu a little bit into that "cool" of the know to see a look, finished, this is some god horse??? How can I not, I want is the kind of very common algorithm ah, well, virtually and deeply hurt a knife, fortunately I quickly adjust the state. I'll follow my own low, I'll take care of him. To get to the point, I'm going to say several algorithms are binary search, bubble sort and select sort. Use arrays as an example to talk about their implementation in Java.

Binary search, for example, we are often used when we are playing a guessing game of numbers. Xiao Ming to the range, 0 ~ 100, we guess 50, Xiao Ming answered "big", then we will continue to guess 25, so we are approaching the truth again and again. This is the idea of the dichotomy. It can also be said that the idea of divided treatment, divide and conquer. We can also apply this idea to the lookup of an ordered array. We can compare the number of the middle of the array to the element to be found, and the process is similar to guessing the number and becoming the number of the "middle" bit. Finally we can find the target element. Method implementation:

Private Static intBinarySearch (int[] list,inttarget) {        intLow = 0; intHigh = List.length-1; //until Low>high, the keyword is not found, the end of the search, return-1         while(low<=High ) {        intMid = (Low+high)/2; if(Target <List[mid]) { High= Mid-1; }        Else if(Target >List[mid]) { Low= Mid + 1; }        Else if(target = =List[mid]) {            returnmid; }        }        return-1; }

Bubble sort, imagine the fish spit a bubble in the water, in the upward process, is not in the more and more big. In fact, the idea of bubble sort is this. Give you the number of n, we use the first number and the second number to compare, the big one put the second, then the second and the third, the big one in the third place, in turn, until and the last comparison, we can get this n number the largest one, and put in the last position. (If you don't understand it, go back and read it again.) so we get one of the biggest numbers in this round, and then we compare the second round, so we can get the largest number of the remaining n-1. and put it in the penultimate position, this is the reason for the inner for loop condition-1, because there is no need to compare it with the last number in the previous round. Method implementation:

 Public Static voidBubblesort (int[] arr) {     /** The For loop outside determines how many rounds a data of length n is to compare to complete the sort.     * The For loop inside determines how many times each cycle will be done to end. */      for(inti = 0; i < arr.length-1; i++) {          for(intj = 0; J < arr.length-1-I; J + +){             //From small to large, large values are placed in the back position.              if(Arr[j] > arr[j+1]){                inttemp =Arr[j] Arr[j]= Arr[j + 1] arr [j+ 1] =Temp}} } }

Ps. You can get a sequence from large to small just by changing one symbol.

Quick sorting, quick sorting is an improvement to the bubbling sort. The basic idea is that the data to be sorted is divided into two separate parts, and that part of it is smaller than all the data in the other part. Then, according to this method, the two parts of the data are sorted quickly, and finally an ordered sequence is obtained. It should be explained that the average time complexity of the quick Sort is O (NLOGN). But the complexity of time is still O (n^2), because the time complexity is calculated according to the worst result. To facilitate understanding, use a set of data to simulate the sequencing process.

Suppose the array to be lined up is:int[] A = {5 2 8 9 2 3 4 9}; Select Key= 5, starting at i = 0,j = 7 Subscript0 1 2 3 4 5 6 7Start5 2 8 9 2 3 4 9I J for the first time5 2 8 9 2 3 4 9I-J Exchange:5 2 4 9 2 3 8 9I J the second time to find5 2 4 9 2 3 8 9I-J Exchange:5 2 4 3 2 9 8 9I J third time find5 2 4 3 2 9 8 9ij Adjust key:2 5 4 3 5 9 8 9IJ

Method implementation:

Private Static voidQuickSort (int[] A,intLowintHigh ) {          //find the exit of the recursive algorithm        if(Low >High ) {            return; }        inti =Low ; intj =High ; //default Key        intKey =A[low]; //start a trip sort         while(I <j) {//First find the number less than key from right to left,//This is done because the number of exchanges is guaranteed to be less than the existing key value when exchanging with the key value//if it is greater then the J pointer will continue to move to the left.              while(I<j && A[j] >key) {J--; }            //find the first number greater than or equal to key from left to right             while(I<j && A[i] <=key) {i++; }            //Exchange to achieve the effect of "divide and conquer" with key            if(i<j) {inttemp =A[i]; A[i]=A[j]; A[J]=temp; }          }        //When i = j, the value of the adjustment key is a[i] = = A[j]        inttemp =A[i]; A[i]=A[low]; A[low]=temp; //Quick sort of number to the left of keyQuickSort (A, low, i-1 ); //Quick sort of number to right of keyQuickSort (A, i+1, high); }

PostScript: I have to say, these simple algorithms I have not very familiar with, can only be the kind of principle clear, but the realization is a stumbling state. Also, the above-mentioned implementation can be further optimized, remember before the interview was asked about the bubble sorting problem, the scene has been asked how to optimize.

A few common algorithms

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.