Simply select the C ++ Implementation of sorting and select sorting implementation

Source: Internet
Author: User

Simply select the C ++ Implementation of sorting and select sorting implementation

Simple selection and sorting use the simplest selection method, that is, the minimum (or maximum) keyword is selected in the residual sequence, and the position is exchanged with the first keyword of the residual sequence, until the entire sequence is ordered.

Directly run the Code:

# Include <iostream> # include <string> using namespace std; template <typename T> void selectionSort (T arr [], int n) {for (int I = 0; I <n; I ++) {// find the minimum int minIndex = I in the [I, n) interval; // mark the position of the minimum value for (int j = I + 1; j <n; j ++) if (arr [j] <arr [minIndex]) minIndex = j; swap (arr [I], arr [minIndex]) ;}} int main () {int array [10] =, 2, 1}; selectionSort (array, 10); for (int I = 0; I <10; I ++) cout <array [I] <""; cout <endl; float a [3] = {3.3f, 2.2f, 1.1f}; selectionSort (a, 3); for (int j = 0; j <3; j ++) cout <a [j] <"; cout <endl; string B [4] = {" D "," C "," B ", "A"}; selectionSort (B, 4); for (int k = 0; k <4; k ++) cout <B [k] <""; cout <endl; return 0 ;}

 

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.