Tuesday, July 28, 2009

C++ time/space difference between sorting an array and using 2 seperate arrays.?

I'm a new student to cs, trying to figure out the most efficient way to do a c++ assignment. My assignment is to remove duplicates in an array (no vectors allowed).





I've coded and compiled this program currently to take in user's input of integers, selection sort the array, then step through a for loop removing all the duplicates(which seems to work well) . Is this the best way to do this assignment or would it be better to have 2 array's and cross reference each element in Array1 with Array 2?





I would appreciate any advice that anyone could give me.

C++ time/space difference between sorting an array and using 2 seperate arrays.?
Unless the assignment specifically says you have to make it as efficient (in terms of processing time or memory use) as possible., I would just keep it simple and use what you have. It sounds like you have a method that works, so you shouldn't need to worry much.





Does the assignment specifically say you must use arrays? If not, there are different data structures that could be used to sort and detect duplicates as the integers are inserted. But these would probably make the code more complex. The simplest example of this would be a linked list or a double linked list.





In most cases, I find the best advice is K.I.S.S. (Keep it simple, stupid). When dealing with large amounts of data, then you must prioritize what is most important (speed, memory, etc) and optimize for it. But in most cases I have come across, simple algorithms work just fine on modern computers when dealing with small to moderate amounts of data.


No comments:

Post a Comment