Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,765 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,601 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Optimization: Selection and Order Statistics

 
Reply to this topicStart new topic

> Optimization: Selection and Order Statistics

KYA
Group Icon



post 18 Jul, 2008 - 04:16 PM
Post #1


Selection Algorithms

By this point we have a good grasp on various ways to sort and search through lists of data or classes. Intro computer science classes often have students write a program that needs to find the minimum, maximum, or median of the integers or other data present within the program. Selection Algorithms try to find the aforementioned data (Order Statistics) are really a subset of path finding algorithms (shortest path for example) in the earnest sense. Armed with this knowledge we will go over various selection algorithms and their widely varying abilities to do nearly anything within a list of data.

Selection Through Sorting

This is by far the easiest way to find the data you are looking for. Assuming we have a valid (but expensive processing) sort method, all we have to do to find the min and max is sort the list and retrieve the first and last elements of the list, respectively.

CODE

     //sort
     int min = arrayOne[0];
     int max = arrayOne[n-1];
  
     //easy and quick, no?


Linear Selection

This is the method employed by many of the first year programs. It iterates through the list and keeps track of which number is the maximum and minimum so far.

CODE

     int max = 0;
     while (!done)
          //user input
          if (userInput > max)
               max = userInput

//so on and so forth


Nonlinear general selection algorithm

This implementation is similar to the previous algorithm, but is quite inefficient. We simply find the most extreme value and move it to the beginning until we reach our desired index. This can be seen as an incomplete selection sort. One advantage includes its ability to be used on linked lists, whereas other methods can require random access.

Language Support

Very few languages have built-in support for general selection, although many provide facilities for finding the smallest or largest element of a list. One exception is C++, which provides a templated nth_element method with a guarantee of expected linear time. (read: hooray for us!)

In Conclusion

In short, there are many ways to arrive at the same destination (i.e. correctly found data). The only difference is the manner in which the data is sought. What dictates this portion of the project? The parameters. How fast does it need to be? How many resources can it take up? What about processing time? The question go on and on. This is especially true in embedded systems programming where resources (read: memory) are very tight and must be as efficient and optimized to the greatest possible extent. Always get your program to work before you try to optimize. Be aware of what your program takes up and how it uses those resources. As always, Happy Coding!

This post has been edited by KYA: 19 Jul, 2008 - 10:36 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/21/08 02:33PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month