C Pointer Programming Path---nineth notes

Source: Internet
Author: User

This is about the application of pointers in algorithms.
Direct Select sort
Each sorting algorithm is a handy feature of pointers that point to each element for exchange, etc.
The basic idea here is to treat the sorting records for n-1 times.
I operation select I Large (small) records placed in the first (or n-i-1) position.
That is, each time a record is placed in its final position,
It's called "all the families."
#include <iostream>
#include <cstdio>
using namespace Std;
void Selectsort (int *array, int n)
{
int I, J, M, A;
Find the location of the smallest 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 position of the current minimum value
* */


if (M! = 1)
{
A = * (Array + m);
* (array + m) = * (array + 1);
* (Array + i) = A;
}
}


}






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 <; ++i)
{
printf ("%d\t", * (Array + i));
}
Selectsort (Array, 10);
printf ("\ n" after direct sorting results are: \ n ");
for (int j = 0; J <10; ++j)
{
printf ("%d", * (Array + j));
}
printf ("\ n");
return 0;

}



Lookup, which involves the offset of the pointer
Find divided into sequential lookups
Binary find
Catechins Search
Block Lookup
Here's an example of two-point lookup
#include <iostream>
#include <cstdio>
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, ten, X1))
printf ("has found%d\n", X1);
Else
printf ("%d\n not Found", X1);
if (BinarySearch (Array, ten, x2))
printf ("has found%d\n", x2);
Else
printf ("%d\n not Found", x2);
return 0;
}

C Pointer Programming Path---nineth notes

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.