Simple Sorting Algorithm for data structure and algorithm analysis

Source: Internet
Author: User

In the Sorting Algorithm, there are three types of simple sorting algorithms: Bubble sorting, select sorting, and insert sorting. learning and understanding these three sorting algorithms helps to further study the data structure and algorithm analysis. Next, let's briefly talk about the principles and differences of Bubble sorting, selection sorting, and insertion sorting.


Bubble sorting principle:1. Compare adjacent elements. If the first one is larger than the last one, they will be exchanged. 2. perform the same action on each pair of elements, and compare them from the back to the front. 3. Each trip will determine an element of a position. Therefore, n elements must be measured by n-1 to determine the position of each element. For example, Bubble Sorting is performed for the numbers 23,4, 56,11. The first trip to, 11, 56, the second trip to, 56, the third trip to, 23, 56java program implementation is as follows: Select sorting principle:1. Compared with the other two types of sorting, a virtual pointer is added, which always points to the position subscript of the minimum value in each comparison. 2. Each comparison is compared with the element corresponding to the virtual pointer. If the element is smaller than the element corresponding to the pointer, the virtual Pointer Points to the new address for the next element comparison. 3. Each trip will determine an element of a position. Therefore, n elements must be measured by n-1 to determine the position of each element. For example, you can select and sort the numbers 23,4, 56,11. The implementation of the Java program is as follows:

Insert sorting principle:1. Element areas are divided into ordered areas and disordered areas. 2. Start from the second element and compare it with the previous element to determine the insert position. 3. If the element is greater than the comparison element, the modified element needs to be moved back. For example, sort the numbers 23,4, 56,11 by insert. The implementation of the Java program is as follows:

Similarities and differences between the three types of simple sorting:1. Bubble sorting and selection sorting each trip will determine the elements at a position, and the sorting efficiency of the two is relatively low. 2. When sorting is selected, the minimum value pointer is implied. Each comparison is compared with the elements of the pointer. 3. Insert sorting has obvious partitions in the sorting process. One side is an ordered element and the other side is an unordered element. 4. Insert sorting is more efficient than Bubble sorting and select sorting.

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.