C Language Enhancement (v) output the smallest k in a string of numbers

Source: Internet
Author: User

Sometimes the topic seems very simple, it appears to be very easy to achieve, but, have you considered efficiency?

With this problem, you can master

    • Simple Insert Sort
    • Techniques for optimizing algorithms

Title: Enter n integers to output the smallest of the K.
For example, enter 1,2,3,4,5,6,7 and 8 for these 8 numbers, then the smallest 4 digits are three-to-three and 4.


See this problem, the first reaction is to sort this string of numbers, and then traverse the angle of the 0~3 digital print out, very simple ~ ~

How can it be so simple, look carefully, the topic is only the smallest number of N, ah, this way to sort the entire array is necessary?

It only takes 4 minimum, which means that as long as a number is larger than the existing 4 minimum number, then we can always forget it forever, instead of inserting it into the array, inserting it into the array, waiting for the new value to be inserted, and to compare it with him, and to increase the number of comparisons!


Ideas


Write a function sorting (), can insert the value and sort the array, note that the original arrays are already sorted in order.
Create a new vector, traverse the given array,
For the previous 4 elements, just execute the sorting function
For the subsequent element, execute the sorting function and pop out the top value POPs


Source

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include < Vector>using namespace std;/** Find the smallest k element (array) title: Enter n integers, and output the smallest of the K. For example, enter 1,2,3,4,5,6,7 and 8 for these 8 numbers, then the smallest 4 digits are three-to-three and 4. Thought write a function sorting (), can insert values and sort the array, note that the original arrays have been sorted in order to create a new vector, traverse the given array, for the previous 4 elements, just execute the sorting function for the following elements, execute sorting function, the top value pop out *//**SORTINGVT: The given array value: The values to be inserted from the top to the end to traverse the vector if value is smaller than the node value traversed, the swap position otherwise, because the original array is already in the order, the subsequent number is smaller than the current number, so stop, jump out of the loop */void Sorting (vector<int> &vt,int value) {vt.push_back (value); for (int i = vt.size () -2;i>=0;i--) {if (value< Vt[i]) {if (I<vt.size ()-1) Vt[i+1]=vt[i];vt[i]=value;} Else{break;}}} /**FINDLASTN Find the minimum n number */void findlastn (vector<int> orivt,vector<int> &finalvt,int N) {// Put the first number directly into the Finalvt.push_back (orivt[0]);//From the second number, start traversing for (int i =1;i<orivt.size (); i++) {//For the previous 4 elements, Just execute the sorting function sorting (finalvt,orivt[i]), if (i>n-1) {//for the following element, execute the sorting function, go out the top value pop Finalvt.pop_back ();}} void Main () {vector<int> finalvt;vector<int> orivt;//Create the original array orivt.push_back (2); Orivt.push_back ( -1); Orivt.push_back (4); orivt.push_back (0); Orivt.push_back (1); O Rivt.push_back (n); Orivt.push_back (3);//Display original array cout<< "original array:" <<endl;for (int i = 0;i<orivt.size (); i++) { Cout<<orivt[i]<<endl;} Find the smallest n int n = 4;findlastn (orivt,finalvt,n);cout<< "Minimum n arrays:" <<endl;for (int i = 0;i<finalvt.size (); i++ ) {Cout<<finalvt[i]<<endl;} System ("Pause");}


Run Diagram


Algorithm optimization can be understood as a word: stingy

One more step wasted, no more than a penny! Can compare 10 times calculated, absolutely not compare 11 times!




C Language Enhancement (v) output the smallest k in a string of numbers

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.