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