Find the maximum (minimum) MB number in array n (manual debugging is supported)

Source: Internet
Author: User

Method 2 can be used when n is very small,

When N is large, you can use method 1 to read data from the hard disk one by one;


/* Method 1 is suitable for a large amount of data
* Search for the first m elements of the n Array and output them.
* Use array n to create a m-large max heap based on the heap nature, and then output the heap content.
* Time Complexity Analysis: heap creation time O (m)
* Traverse numbers and compare time O (N-M) logm) in the heap)
* Total time complexity O (m) + O (N-M) logm)
*
* Space complexity O (m)
*/
 
 
/* Method 2
* Solve the problem by using the quick sorting method of classes, "suitable for less data"
*
* Time complexity O (N)

*

*/

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define ndebug # include <assert. h> int input (void); void print (int * P, int Len); void adjust (int * heap, int heap_size, int index); void swap (int *, int * B); void built_heap (int * heap, int heap_size, int * A); void find_max (int * heap, int heap_size, int * a, int Len ); // method 2 void quick_find (int * Arry, int Len, int m); int partation (int * Arry, int Len, int start, int end); Int main (void) {int heap_size; int * heap; // int A [] = }; int A [] = {1,231, 122,192,126,212, 232, 66,214,123, 66,662,552,225,226,226,226, 789,622, 25,262,551,789,781,895,895,951, 23,325,235,266,456, 7894,}; heap_size = inpu T (); heap = (int *) malloc (sizeof (INT) * heap_size); find_max (heap, heap_size, A, sizeof (a)/sizeof (INT )); print (heap, heap_size); free (HEAP); quick_find (A, sizeof (a)/sizeof (INT), heap_size); Return 0;} void print (int * P, int Len) {int I; for (I = 0; I <Len; I ++) printf ("% d", P [I]); printf ("\ n"); return;} int input (void) {int m; printf ("input the M:"); scanf ("% d ", & M); Return M ;}// create a heap void built_heap (int * heap, int H Eap_size, int * A) {int I; Assert (heap! = NULL &! = NULL); for (I = 0; I 

Program running result:

"The output two rows are the result of method 1 and method 2 respectively"

[[email protected] code_test]$ gcc   -o arryN_maxM arryN_maxM.c[[email protected] code_test]$ ./arryN_maxM input the M:102125  2262  2626  4755  2626  45678  9511  7894  15629  4548  45678  15629  9511  7894  4548  2626  2626  4755  2262  2125 







Find the maximum (minimum) MB number in array n (manual debugging is supported)

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.