The maximum and minimum values are also found in the N number:
Example: n = 7
5 1 2 3 6 4 8
Method One: Independently find the maximum and minimum values, each with n-1 times comparison, a total of 2n-2 comparison.
Method two: To deal with the elements in pairs, first compare a pair of input elements, then compare the smaller with the current minimum, compare the larger with the current maximum, so each two elements need to be compared 3 times. (when n is odd, the maximum and minimum values are set to the value of the first element, and then the remaining elements are processed in pairs, a total of 3*[n/2] times are compared, [] is the lower bound. When n is even, a comparison is made to the first two elements to determine the initial value of the maximum and minimum values, and then the remaining elements are processed in pairs, with a total of (n-2)/2 + 2 = 3*n/2-2 comparisons, regardless of the case, the total number of comparisons is 3[n/2],[] is the lower bound)
/* ------------------------------------------------------------------------------------------------------------- --------------------*/
Proof: In the worst case, the second small element in n elements can be found using n + [Lg[n]]-2 comparisons. (Hint: Find the smallest element at the same time)
General search: First find the minimum value, using a n-1 comparison, and then find the second small value, using the n-2 times comparison. A total of 2n-3 times compared, can not meet the requirements of the topic.
We can use the structure of the tree to complete the search: paired comparisons, constantly take small values, so that the minimum value can be obtained. And in the process of comparison can generate a tree. Specific
Example: n = 7
5 1 2 3 6 4 8
Find the minimum (that is, the root node) need to compare the number of times is n-1 (in fact, the number of non-leaf nodes), and then from the root node toward the leaves along a path to find the minor value (a path refers to the child node value = The value of the current node of the path). Because in the spanning tree time small value must be compared with the minimum value, so in the course of walking along the path, the element to be compared is the child node value! = root node value of the value of the node, that is, the graph of red labeled nodes, the number of the maximum is LG (n)-1, that is, the tree height-1 No comparison required). finding the minimum value of the Red node is the final minor value. So the total number of times reached n + [LG (n)]-2 times.
It's a pity.
After testing, the normal lookup (comparison number = 2n-3) is more than 10 times faster than using a binary tree lookup (comparison = n + [LG (n)]-2), which may be the time consumption caused by the latter operation complexity. But the latter is really a good idea!
Paste the code:
/* Generate test Data */#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>int main () {freopen ("In.txt", "w", stdout), Srand (Unsigned (Time (NULL))), int n = 8000000;int i;printf ("%d\n", N); for (i = 0; i < n; i++) {printf ("%d\n", rand ()%n + 10);} return 0;}
/* Includes worst case comparison of N + LG (n)-2 for sub-small values in the array *//* operating environment: Windows platform */#include <stdio.h> #include <string.h> #include < ;stdlib.h> #include <windows.h> #define N 100010000#define INF (1<<30) #define MIN (x, y) ((x) < (y)? (x): (y)) typedef struct TREE{INT data; TREE *lchild, *rchild;} Tree; Tree *t[n];int a[n];bool Create (tree *&t) {t = NULL; t = (tree *) malloc (sizeof); if (NULL = = T) {printf ("error! Overflow!\n "); return 0;} return 1;} BOOL Init (Tree *t[], int n) {int i; Tree *t;for (i = 0; i < n; i++) {if (! Create (T)) return 0;scanf ("%d", &t->data); T->lchild = T->rchild = Null;t[i] = T;} return 1;} void Showinit (Tree *t[], int n) {int i;for (i = 0; i < n; i++) printf ("%d", T[i]->data);p rintf ("\ n");} BOOL Createtree (Tree *t[], int n) {int len = N;int u, i; Tree *t;while (len > 1) {u = 0;for (i = 0; i + 1 < len; i + = 2) {if (! Create (T)) return 0; T->data = MIN (T[i]->data, t[i+1]->data); T->lchild = T[i]; T->rchild = t[i+1];t[u++] = t;} if (Len & 1) t[u++] = T[len-1];len = u;} return 1;} void ShowTree (Tree *t) {if (NULL = = T) return;printf ("%d", t->data); ShowTree (T->lchild); ShowTree (t->rchild);} void Findsecmin (Tree *t, const int base, int &ans) {if (null = = T->lchild && NULL = = t->rchild) return;if ( base = = T->lchild->data) {ans = MIN (ans, t->rchild->data); Findsecmin (T->lchild, base, ans);} Else{ans = MIN (ans, t->lchild->data); Findsecmin (T->rchild, base, ans);}} int main () {freopen ("In.txt", "R", stdin); Tree *t;int N, Min2;while (~scanf ("%d", &n)) {#if 0for (i = 0; i < n; i++) scanf ("%d", &a[i]); int flag = 0;int Min1 = inf;/*---------------------------------------------------*/FILETIME beg,end;//<windows.h> The timer getsystemtimeasfiletime (&beg);/*---------------------------------------------------*/for (i = 0; i < n; i + +) {if (A[i] < min1) {min1 = A[i];flag = i;}} Min2 = inf;for (i = 0; i < n; i++) {if (A[i] < min2 && i! = flag) min2 = A[i];} /* ---------------------------------------------------*/getsystemtimeasfiletime (&end); Long time = 100* ( End.dwlowdatetime-beg.dwlowdatetime);/*---------------------------------------------------*/printf ("%d\n", Time ); if (INF = = min2) printf ("error!\n"); Only one data else printf ("min2 =%d\n", min2); #endif # if 1if (! Init (t, N)) return 1;//showinit (t, N); Show initial data/*---------------------------------------------------*/filetime beg,end;//<windows.h> The timer getsystemtimeasfiletime (&beg);/*---------------------------------------------------*/createtree (t, N); Create Tree min2 = INF; Findsecmin (T[0], t[0]->data, min2); Look for the second small value/*---------------------------------------------------*/getsystemtimeasfiletime (&end); Long time = 100* (end.dwlowdatetime-beg.dwlowdatetime);/*---------------------------------------------------*/printf ("%d\n" , time), if (INF = = min2) printf ("error!\n"); Only one data else printf ("min2 =%d\n", min2); #endif}return 0;}
Introduction to Algorithms (9th-median and sequential statistics) maximum and minimum values