Array address received using pointer variables as function parameters

Source: Internet
Author: User

[Cpp]/** Copyright (c) 2012, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Fan Lulu * Completion Date: July 15, December 16, 2012 * version: v1.0 ** input Description: none * Problem description: sorting the Array Using pointers * program output: array * Problem Analysis: * Algorithm Design: omitted */www.2cto.com # include <iostream> using namespace std; int main () {void select_sort (int * p, int n ); // define the int a [10] = {5, 8, 6, 97, 13, 19}, I; select_sort (a, 10 ); // reference function cout <"the sorted array is:" <endl; for (I = 0; I <10; I ++) // output function cout <<A [I] <''; cout <endl; return 0;} // The sorting Function Code void select_sort (int * p, int n) // use the pointer variable as the shape parameter {int I, j, t, k; // The loop of I (the first loop) starts from 0. By the end of The N-2, because the n-2 is compared with n-1, there is no need to loop to n. For (I = 0; I <n-1; I ++) {// assign the I value to k, so that the following sorting starts from I, and the preceding I is no longer required. K = I; // the cycle of j (Layer 2 loop) starts from I + 1 to n-1. Sort the results one by one. For (j = I + 1; j <n; j ++) // if the last number is greater than the previous one, assign j to k if (* (p + j)> * (p + k) k = j ;. // Sort and use pointers to receive array addresses. T = * (p + k); * (p + k) = * (p + I); * (p + I) = t ;}}

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.