Found on the Internet, the common ranking algorithm summary, you can review __ algorithm

Source: Internet
Author: User
Tags array length int size

Bubble Sort

Bubble sort is a simple sort algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence.

example of bubbling sort:

The bubble sort algorithm is implemented as follows: "After sorting, arrays are arranged from small to large"

/**
     * Bubble sort
     * Compare adjacent elements. If the first one is bigger than the second one, swap them both.  
     * Do the same work for each pair of adjacent elements, from the first pair to the end of the last pair. At this point, the final element should be the largest number.  
     * Repeat the above steps for all elements except the last one.
     * Continue to repeat the previous steps for less and fewer elements until no pair of digits need to be compared. 
     * @param numbers integer array that needs to be sorted *
    /public static void Bubblesort (int[] numbers)
    {
        int temp = 0;
        int size = Numbers.length;
        for (int i = 0; i < size-1 i + +)
        {for
        (int j = 0; J < Size-1-i; J +)
        {
            if (Numbers[j] > Numb ERS[J+1])  //Exchange two number position
            {
            temp = numbers[j];
            NUMBERS[J] = numbers[j+1];
            NUMBERS[J+1] = temp;}}}
    

Quick Sort

The basic idea of quick sorting :

By sorting the records to be sorted into two separate parts, where some of the records have less keywords than the other, the two sections are sorted until the entire sequence is ordered.

example of a quick sort:

(a) a process of sequencing:

(b) The whole process of sequencing

The whole sequence is regarded as an array, and the 0th position is regarded as the middle axis, and the last one, if smaller than it is exchanged, does not do any processing larger than it; exchanged later again and small that end ratio, than it small does not exchange, than he a large exchange. This cycle, a trip to sort out, the left is smaller than the central axis, the right is larger than the central axis, and then use the partition method, respectively, the two separate array of sorting.

The code implementation is as follows:

1. Find the center axis (lowest bit as the axis) location

/**
     * Find out the central axis (the default is the lowest bit low) in the numbers array after the sort of location
     * * 
     @param numbers with find array
     * @param low   start position
     * @param High  End Position
     * @return  Axis Position * *
     public
    static int getmiddle (int[] numbers, int low,int high)
    {
        int temp = Numbers[low];//The first of the array is the axis while
        (Low < High)
        {while (Low < high
        && numbers [High] > Temp)
        {
            high--;
        }
        Numbers[low] = numbers[high];//is moved to the low-end while
        (Low < high && Numbers[low] < temp)
        {low
            ++;
        }
        Numbers[high] = Numbers[low]; Higher than middle axis record moved to high-end
        }
        numbers[low] = temp;//Axis records to tail
        return low;//back to center axis position
    }

2, recursive form of split-rule sorting algorithm:

    /**
     * * 
     @param numbers with sorted array
     * @param low  start position
     * @param high end position/public
    static void quick Sort (int[] numbers,int low,int High)
    {
        if (Low < high)
        {
        int middle = getmiddle (Numbers,low,high); Divides the numbers array into a
        quickSort (numbers, low, middle-1);   Recursive ordering of low field tables
        QuickSort (numbers, middle+1, high);//recursive sorting of fields tables
        }
    
    

3. Quick Sort provides method calls

/**
     * Quick Sort
     * @param numbers with sorted array
     *
    /public static void quick (int[] numbers)
    {
        if ( Numbers.length > 0)   //See if the array is empty
        {
        quickSort (numbers, 0, numbers.length-1);
        }
    

Analysis:

A quick sort is generally considered to be the best in the ranking method of the same order of magnitude (O (nlog2n)) . But if the initial sequence is ordered or basically ordered by the key code, the fast sort is reduced to bubble sort. In order to improve it, the benchmark record is usually selected by the "three-way Method", which adjusts the center of the two endpoints of the sorted interval and the midpoint of three key codes to the Fulcrum record. A quick sort is an unstable sort of method.

method Test

Print function:

public static void Printarr (int[] numbers)
    {for
        (int i = 0; i < numbers.length; i + +)
        {
        SYSTEM.OUT.P Rint (Numbers[i] + ",");
        }
        System.out.println ("");
    }

Test:

public static void Main (string[] args) 
    {
        int[] numbers = {10,20,15,0,6,7,2,1,-5,55};
        System.out.print ("Sort before:");
        Printarr (numbers);
        
        Bubblesort (numbers);
        System.out.print ("After bubble sort:");
        Printarr (numbers);
        
        
        Quick (numbers);
        System.out.print ("Fast sort After:");
        Printarr (numbers);
    }

Results:

Sort before: 10,20,15,0,6,7,2,1,-5,55,
bubble sort: -5,0,1,2,6,7,10,15,20,55,
after a quick sort: -5,0,1,2,6,7,10,15,20,55,

One, select the sort       1, the basic idea : In the number of groups to be sorted, select the smallest number and the number of the first position of the exchange, and then in the remaining number to find the smallest and the second position of the number of exchanges, so that the second and last number of the countdown to compare. 2. Example   

  3, algorithm implementation

/**
     * Select sorting algorithm
     * Find the smallest element in the unordered sequence, hold it to the start of the sort sequence  
     * and then continue to look for the smallest element from the remaining unordered elements, and then place it at the end of the sort sequence. 
     * And so on until all the elements are sorted. 
     * @param numbers */public
    static void Selectsort (int[] numbers)
    {
    int size = numbers.length;// Array length
    int temp = 0;//intermediate variable for
    
    (int i = 0; i < size; i++)
    {
        int k = i;   Position to be determined
        //select number for
        (int j = size-1; j > i; j--)
        {
        if (NUMBERS[J) < Numbers[k])
        {
            k = j;
        }
        }
        Exchange two numbers
        temp = numbers[i];
        Numbers[i] = numbers[k];
        NUMBERS[K] = temp;
    }
    }

Second, insert sort

  1, the basic idea : Each step will be a record to be sorted, in its order code size inserted into the previous sorted sequence of words in the appropriate position (after the appropriate position from the forward), until the insertion of all the sorting finished.

  2. Example

  

  3, algorithm implementation

     /**  
     * Insert Sort * * 
     starting with the first element, the element can be thought to have been sorted
     * Take out the next element and scan backwards in the sorted sequence of elements 
     * If the element (sorted) is greater than the new element, move the element to the next position  
     * Repeat step 3 until you find the sorted element is less than or equal to the new element  
     * Insert the new element into the location  
     * Repeat steps 2  
     * @param numbers  array  
    to be sorted * * public static void Insertsort (int[] numbers)
    {
    int size = numbers.length;
    int temp = 0;
    Int J =  0;
    
    for (int i = 0; i < size; i++)
    {
        temp = numbers[i];
        If temp is smaller than the previous value, then move the previous value for
        (j = i; j > 0 && Temp < numbers[j-1]; J-)
        {
        Numbers[j] = Numbe RS[J-1];
        }
        NUMBERS[J] = temp;
    }
    }
Related Article

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.