C pointer programming path-ninth note

Source: Internet
Author: User

C pointer programming path-ninth note
// Here we will talk about the application of pointers in algorithms.
// Select sorting directly
// Each sorting algorithm is convenient for pointers to exchange with each element.
// The basic idea here is to make n-1 selections for sorting records.
// Select the I-large (small) record for the I operation and place it at the I (or n-I-1) position.
// Put a record at its final position each time,
// This is the so-called "various families"
# Include
# Include
Using namespace std;
Void SelectSort (int * Array, int n)
{
Int I, j, m,;
// Locate the minimum value from the unordered Sequence
For (I = 0; I <n-1; I ++)
{
M = 1;
For (j = I + 1; j <n; ++ j)
{
If (* (Array + j) <* (Array + m ))
M = j;
}
/*
* Record the location of the current minimum value
**/


If (m! = 1)
{
A = * (Array + m );
* (Array + m) = * (Array + 1 );
* (Array + I) =;
}
}


}






Int main ()
{
Int I = 0;
Int Array [10] = {12, 2, 37, 67, 90, 1, 78, 67, 2, 32 };
Printf ("the array to be sorted is \ n ");
For (I = 0; I <10; ++ I)
{
Printf ("% d \ t", * (Array + I ));
}
SelectSort (Array, 10 );
Printf ("\ n: \ n" after direct sorting ");
For (int j = 0; j <10; ++ j)
{
Printf ("% d", * (Array + j ));
}
Printf ("\ n ");
Return 0;

}

 

 

// Search, which involves the pointer offset
// Search in sequence
// Half-Lookup
// Search for ercha
// Multipart search
// The example here is binary search
# Include
# Include
Using namespace std;
Int BinarySearch (int * Array, int n, int x)
{
Int low, high, middle;
Low = 0, high = n-1;
While (low <= high)
{
Middle = (low + high)/2;
If (* (Array + middle) = x)
Return 1;
Else
{
If (* (Array + middle)> = x)
{
High = middle-1;
}
If (* (Array + middle) <= x)
{
Low = middle + 1;
}
}

}


}






Int main ()
{
Int Array [10] = {2, 4, 5, 13, 15, 20, 30, 35, 40, 50 };
Int x1, x2;
X1 = 20;
X2 = 33;
If (BinarySearch (Array, 10, x1 ))
Printf ("% d \ n", x1 );
Else
Printf ("% d \ n not found", x1 );
If (BinarySearch (Array, 10, x2 ))
Printf ("% d \ n", x2 );
Else
Printf ("% d \ n not found", x2 );
Return 0;
}
Previous
Http://www.bkjia.com/kf/201412/361323.html

Related Article

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.