Data structure and algorithm (26)--Sort (i) __ data structure

Source: Internet
Author: User
Bubble sort (Bubble sort)   is one of the simplest sorting algorithms.
Basic idea: Iterate over 22 comparisons of the first element in the input sequence to the last element, and swap two element positions when needed.
public static void Bubblesort (int[] array) {
    //Get the number of array elements
    int n = array.length;
    This for loop is designed to avoid comparing the already ordered data for
    (int pass = n-1 pass >= 0; pass--) {
        //compare unsorted part for
        (int i = 0; i < PA ss i++) {
            //MAX Move backward
            if (Array[i] > array[i + 1]) {
                int temp = array[i];
                Array[i] = array[i + 1];
                Array[i + 1] = temp;
            }}}

You can improve the bubbling algorithm by adding tags. In the sort process, no swap operation means the sort completes. If the sequence is already ordered, you can end the algorithm by judging the tag.

public static void Bubblesortimproved (int[] array) {
    //Get the number of array elements
    int n = array.length;
    Mark each trip sort for data exchange
    Boolean swapped = true;
    This for loop is designed to avoid comparing the already ordered data for
    (int pass = n-1, pass >= 0 && swapped; pass--) {
        swapped = false;
        Compare only unsorted parts for
        (int i = 0; i < pass; i++) {
            //maximum move backward
            if (Array[i] > array[i + 1]) {
                int temp = Array[i];
                Array[i] = array[i + 1];
                Array[i + 1] = temp;
                Data exchange
                swapped = true;
            }}
    }
Select sort (Selection sort) is an algorithm for in-place sorting (that is, no additional storage space is required) and is suitable for small files.
idea:
1. Find the minimum value of
2 in the sequence. Use the current position for the minimum value of
3. Repeat the above procedure for all elements until the sequence sort completes
public static void Selectionsort (int[] array) {
    //Get the number of array elements
    int n = array.length;
    subscript int min for minimum value
    ;
    Temporary variable
    int temp;
    Look for the minimum position and swap for the current position for
    (int i = 0; i < n; i++) {
        min = i;
        Find the minimum position for
        (int j = i + 1; j < N; j + +) {
            if (Array[j] < array[min]) {
                min = j;
            }
        }
        //Exchange element
        temp = Array[i];
        Array[i] = array[min];
        Array[min] = temp;
    }
}
The Insert sort (Selection sort) is a simple and efficient comparison sort algorithm. Typical in-situ sequencing
 : Every time you remove an element from the input data and insert it in the correct position of the sorted sequence, until all input input elements are inserted into an ordered sequence.
public static void Insertionsort (int[] array) {
    //Get the number of array elements
    int n = array.length;
    Temporary variable
    int temp;
    The cyclic variable int J of ordered sequence
    ;
    for (int i = 1; i < n; i++) {
        //Get input data
        temp = array[i];
        j = i;
        Sort while
        (J >= 1 && (array[j-1] > Temp)) {
            array[j] = array[j-1];
            j--;
        }
        Put data into an ordered sequence
        array[j] = temp; 
    }
}

Test code, please refer to the data structure and algorithm (Java version)

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.