Sorting Algorithm (2): Sorting Algorithm

Source: Internet
Author: User

Sorting Algorithm (2): Sorting Algorithm
1. Insert Sort-insert Sort directly (Straight Insertion Sort)

Basic Idea:

    Insert a record to an ordered table that has been sorted to obtain a new ordered table with 1 Increase in the number of records. That is: first, the first record of the sequence is regarded as an ordered subsequence, and then the second record is inserted one by one until the whole sequence is ordered.

    Key points:Set up a sentry for temporary storage and array boundary judgment.

Example of directly inserted sorting:

 

If you encounter an element that is equal to the inserted element, the inserted element is placed behind the element that you want to insert. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting, so insertion sorting is stable.

Algorithm Implementation:

      

Package sort; public class InsertSort {public static void main (String [] args) {int a [] = {, 35, 2}; InsertSort. sort (a, 8); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void sort (int [] arr, int n) {for (int I = 1; I <n; I ++) {if (arr [I] <arr [I-1]) {// insert directly if the I-th element is greater than the I-1 element. Otherwise, enter the ordered table, judge the insert position and insert int j = I-1; // It is already the length of the sorting number array int x = arr [I]; // set up the Sentinel, store the sorted element arr [I] = arr [I-1]; // move one while to the back (x <arr [j]) {arr [j + 1] = arr [j]; // move a j --;} arr [j + 1] = x; // insert to the correct position }}}}

Efficiency:

  Time Complexity: O (n²)

Other insert sorting methods include binary insert sorting and two-way insert sorting.

 


For direct sorting algorithms

This is actually to insert the sorting directly. First, from the second element, that is, your value assignment operation, assign a [I] to temp. The first operation is actually a [2]. compare it with a [1]. If it is smaller than a [1,
So let a [2] be equal to a [1], and let a [1] be equal to temp. This temp is actually the [2] saved at the beginning.
In this case, a [1] and a [2] are ordered.
In the same way, when we encounter each element in turn, we will compare this element with each element in the previous step. If we encounter a bigger element, we will move it to the next step. For example
For example, 1, 2, 4, 3. when the fourth number is 3, let it compare with each previous number. Is 3 smaller than 4? The answer is small. So long, the fourth number equals 4, at this time, 3 is saved in the temp variable, so that the third book is equal to 3, and then compare temp = 3 and 2, which is bigger than it, so it does not move. The same principle is compared with 1, still. The entire array is ordered by 1, 2, 3, 4.

What is the idea of selecting a sorting algorithm?

Select the maximum value of the current sequence for each time:
For example, 1, 3, 6, 9, and 5 are sorted in ascending order!
Step 1
Step 2, 1
Step 3, 3, 5
Step 4, 1, 5, 6
Step 5, 1, 5, 6, 9

// Progressively select the implementation of sorting!

Void SelectSort (double * head, int amount)
{
Double temp;
Int m, n;
For (m = 0; m <amount-1; m ++)
{
For (n = m + 1; n <amount; n ++)
{
If (head [n] {
Temp = head [n];
Head [n] = head [m];
Head [m] = temp;
}
}
}
}

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.