Summary of methods for sorting array elements (using C + +)

Source: Internet
Author: User

First, the sorting method for an array element is summarized in the following three ways:

1. Insert Sort

2. Select Sort

3. swap sort, that is, bubble sort

Next, they are discussed separately:

1. Insert Sort

Algorithm idea: Each step inserts a pending element into the sorted sequence at the appropriate position by the size of its key value until the sorted element is inserted.

Algorithm implementation:

Test results:

2. Select sort

Algorithm idea: Select the smallest element of a keyword each time from the sequence to be sorted (when you need to sort by the keyword in ascending order), and the sequence is sorted at the end of the ordered sequence until all the rows are complete.

Algorithm implementation:

Test results:

3. Swap sort (bubble sort)

Algorithm idea: 22 compares the elements in the sequence to be sorted and swaps the pairs of elements that do not meet the order requirements until all the order requirements are met.

Algorithm implementation:

Test results:

Summary of methods for sorting array elements (using C + +)

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.