Data Structure and Algorithm - Insert Sorting and Multiple Sort

tags: Data Structures and Algorithms

Common algorithm

Quick sorting, bubble sorting, Hill sort, dual sorting (two-way) (NLOGN), barrel sort, stack sorting, base sort
Insert, Hill, return to
Choice, bubbling, fast
Pile Sort:

## Analysis Algorithm
1, time efficiency
2, spatial complexity
3, comparison number and exchange
4, stability
1 9 3 5 3
First type: 1 3 3 5 9
Second: 1 3 3 5 9
Which is stable? After the same two number, the relative position is unchanged.
significance:
Order order in the e-commerce: First, press the amount to the large row, the amount is the same time. I have been in the order from the order center.
1 Insert Sort
Suppose there is such a problem: playing Poker. Divided into two parts: Part is part of your hand (already in order), part is the card (disorder). Insert a unordered number one by one in the ordered number column. A orderly array, how do we continue to keep your data or order? We have added a new data to the inside. As long as we traverse an array, we can insert it in the position that the data should be inserted.
The above inserting an element in an ordered collection is inserted, and the sequence is still orderly, this is the insertion sort algorithm. It is not difficult to understand it. So how is the insertion sorting?
Specific steps are as follows:
1. Divide an array into a sorted segment and an unslining section. Sort only one element when initialization
2. Go to the unsorted segment tap element to insert the sorted segment, and ensure that it is still orderly after insertion
3. Repeat the above operation until the unsorted segment element is completely added.
There are several data structures that are implemented in what data structure. Array, linked list, 2 arrays.

package edu.sort;

import java.util.Arrays;

/**
   * Multiple sort
   * First segment the data, divided into a segment of a segment, and then insert sorting
 *
 * @author: LHT
 * @date: 2020/12/14 15:24
 */
public class MergeSort {
    public static void main(String[] args) {
        int[] data = {1, 4, 234, 63, 123, 56234, 21, 213, 4543};
        mergeSort(data, 0, data.length - 1);
        System.out.print(Arrays.toString(data));

    }

    public static void mergeSort(int data[], int left, int right) {
        if (left < right) {
            // Split an array, if you want, there is only one number. No need to split,
            int mid = (left + right) / 2;
            mergeSort(data, left, mid);
            mergeSort(data, mid + 1, right);
            // After the next, the next step is to merge.
            merge(data, left, mid, right);
        }
    }

    /**
           * Merge data
     *
           * @PARAM DATA source data
           * @Param LEFT splits the subscript
           * @Param MID splitting the bid
           * @PARAM Right split a bit subscript
     */
    public static void merge(int[] data, int left, int mid, int right) {
        int temp[] = new int[data.length]; // Borrow the temporary array store merged data
        int point1 = left;/ / Try the rightmost number of the first data
        int point2 = mid + 1;/ / Tag location of the first number of the right
        int loc = left; // Mark our current location
        while (point1 <= mid && point2 <= right) {
            if (data[point1] < data[point2]) {
                temp[loc] = data[point1];
                point1++;
                loc++;
            } else {
                temp[loc] = data[point2];
                point2++;
                loc++;
            }
        }
        // Merge data
        while (point1 <= mid) {
            temp[loc++] = data[point1++];
        }
        while (point2 <= right) {
            temp[loc++] = data[point2++];
        }
        / / To assign the serial data to array
        for (int i = left; i <= right; i++) {
            data[i] = temp[i];
        }
    }
}

package edu.sort;

/**
 * Insert sort
 *
 * @author: LHT
 * @date: 2020/12/14 14:44
 */
public class InsertionSort {
    /**
           * * 1. Divide the array into sorted segments and unslining segments. Sort only one element when initialization
           * * 2. The until the unsorted segment takes element is inserted into the sorted segment, and it is still in order to ensure insertion.
           * * 3, repeat the above operation until the unsorted segment elements are all added.
           * Number of cycles: 2,
           * Time complexity: n ^ 2
           * The best situation O (N)
           * The worst case O (n ^ 2)
     * <p>
           * Optimization Solution: Segmentation
     *
     * @param a
     */
    static int[] insertSort(int a[]) {
        int n = a.length;
        for (int i = 1; i < n; i++) {
            int data = a[i];
            int j = i - 1;
            for (; j > 0; j--) {
                if (a[j] > data) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = data;
            for (j = 0; j < n; j++) {
                System.out.print(a[j] + " ");
            }
            System.out.println(" + i + "Subproduction results");
        }
        return a;
    }

    public static void main(String[] args) {
        int a[] = {1, 3, 6532, 235, 2135, 67, 2};
        /**
                   * Insert sort
                   * Optimization plan, Hill sort
                   * Re-optimization solution
                   * Multiple sort
         */
        insertSort(a);
    }

}

Intelligent Recommendation

Insert Sort Java Details and Implementation-Data Structure and Algorithm -09- Sorting

Insert sort The basic operation of Straight INSERTION SORT is to insert a record into an orderly order that has been arranged to get an orderly table with a new record of 1. Insert sort derivation No ...

Data Structure - Sorting - Multiple Sort

Multiple order Recursive algorithm Non-creation algorithm Multiple sorting is also divided into Sort by: The amount of data is relatively small, can be placed in memory External sort: Data amount, can...

[data structure and algorithm] insert sort

Insert sorting is the basic entry and bubble sorting in the algorithm, and sorting is necessary. They are all sorted and need to be sorted by comparing the size of the swap position. The implementatio...

Data Structure and Algorithm: Insert Sort

Insert sort Insertion Sort (English: Insertion Sort) is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence, for unsorted data, scanning backwards and forwards in th...

Data Structure and Algorithm - Insert Sort

Insert sort First, the concept Basic concept: Insert sort, starting with the second number A in the sequence, inserting A into the frontSorted sequenceIn, form a new sorted sequence, and so on to the ...

More Recommendation

Insert sort of data structure and algorithm

Bubble sort of data structure and algorithm Selection of data structure and algorithm 1. The core idea The basic idea of ​​Insert Sorting is to treat N elements to be sorted as an ordered list and an ...

[Data Structure and Algorithm]-Insert Sort

Original address:[Data Structure and Algorithm]-Insert Sort Insert sort (Insertion sort) is a simple, intuitive and stable sorting algorithm. If there is an already ordered data sequence, it is requir...

Data structure and algorithm ------ Insert Sort

Insert order Insert sort belongs to the internal ordering method,For elements that are sorted in insert, the appropriate position of this element is inserted to achieve the purpose of sorting. Sorting...

Data Structure and Algorithm --- Insert Sort

Insert order What is insertion sort: The insertion sort belongs to the internal sorting method, is to find the appropriate position of the element in inserted elements inserted to achieve the purpose ...

Algorithm and Data Structure - Insert Sort

Thought: Simple insertion Sort is inserting a record that is sorted, inserted into the appropriate location of the ordered queue according to the size of its keyword, if the sequence is the order, the...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top