Algorithm-face test more than 20,000 employees by age sorting algorithm

Source: Internet
Author: User

More than 20,000 employees by age ranking algorithm,

Algorithm thinking is the idea of choosing a sort algorithm, taking into account more than 20,000 employees, at a certain age when the value of a number of employees will have the actual situation.

Ideas
The first search to find, the minimum age of M minage, the maximum age of N MaxAge, so that the age of the range [Minage, MaxAge]; second, search step 1, all found, after the M minage, in turn ...

C # code implementation,

Personnel model for,

public class person
{public person
   (int staffno, int age)
   {
     this.staffno = staffno;
     This.age = age;
   }
   Work number public
   int staffno {get;set;}
   Age public
   int age{get;set;}
  }

Step to 1 sorting search algorithm:

        static void sort ()
        {
            if (list = = null) return;
            Find out who is the youngest of the list
            list<person> minagepersons = getagepersons (list. Min (per => per.age));

            Find out who is the oldest list
            list<person> maxagepersons = getagepersons (list. Max (per => per.age));

           Remove these values
            list = list. Except (minagepersons). Except (maxagepersons). ToList ();

            for (int age = minagepersons[0].age+1;age<maxagepersons[0].age;age++)
            {
                list<person> agepersons = Getagepersons (age);
                Minagepersons.addrange (agepersons);
                List. Except (agepersons);
            }

            Minagepersons.addrange (maxagepersons);
            list = minagepersons;
        }

        static list<person> getagepersons (int age)
        {return
            List. FindAll (person => person.age = = age);
        }

The age of the person does not necessarily step into 1, so improve the sort (),

        static void sort ()
        {
            if (list = = null) return;
            Find out who is the youngest of the list
            list<person> minagepersons = getagepersons (list. Min (per => per.age));
            List = list. Except (minagepersons). ToList ();
            Do
            {                
                list<person> tmpminagepersons = getagepersons (List. Min (per => per.age));
                Minagepersons.addrange (tmpminagepersons);
                List = list. Except (tmpminagepersons). ToList ();
            } while (list. Count > 0);

            list = minagepersons;
        }

Source Address:
After downloading, list = list. Except (minagepersons). ToList () moves outside the Do loop. (a small problem that was found after uploading the code)
http://download.csdn.net/detail/daigualu/9782742

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.