- Joined
- 6/6/08
- Messages
- 1,194
- Points
- 58
1) Ahhh...Radix sort. Just wiki'd that one up. Never saw it in class before.
2) Merge vs. Quick: depends on the data structure. Because merge sort is a stable sort and quick isn't, quick would go better on arrays of numbers, while merge would do better on more complex data structures, such as linked lists.
3) For one linked list in O(n): go down the list, creating a double link at each node, then delete the original.
4) If the two linked lists merge at the last node, then they're one big linked list, and thus can be manipulated as such.
5) Subset sum: ah, the Horowitz and Sani solution. Split the elements into two arrays, sort the arrays, then with one, start from the largest value, and from the second, start from the smallest. Keep adding them up simultaneously until you reach above the desired target, then subtract from the first while adding from the second. This runs in O(n sqrt(2^n)). I would wonder if it can be improved by sorting all the numbers initially, and then dividing them into a smaller and larger array at the median.
6) public vs. private pedals: hmmm...I know that a private method can only be accessed by instances of its own class. So an instance of mountainBike, I'd think, would have no problem accessing its pedal() method. Furthermore, since bike's pedal() is public, it can be accessed by any bike class. That said, I think that if you tried to do it in reverse, and made Bike.pedal() private and mountainBike.pedal() public, that mountainBike.pedal() wouldn't be able to access the original pedal() method and the compiler would give you an error. It's been a while since I remember the exact wording of the error, but basically that you can't access a private method from outside its class.
2) Merge vs. Quick: depends on the data structure. Because merge sort is a stable sort and quick isn't, quick would go better on arrays of numbers, while merge would do better on more complex data structures, such as linked lists.
3) For one linked list in O(n): go down the list, creating a double link at each node, then delete the original.
4) If the two linked lists merge at the last node, then they're one big linked list, and thus can be manipulated as such.
5) Subset sum: ah, the Horowitz and Sani solution. Split the elements into two arrays, sort the arrays, then with one, start from the largest value, and from the second, start from the smallest. Keep adding them up simultaneously until you reach above the desired target, then subtract from the first while adding from the second. This runs in O(n sqrt(2^n)). I would wonder if it can be improved by sorting all the numbers initially, and then dividing them into a smaller and larger array at the median.
6) public vs. private pedals: hmmm...I know that a private method can only be accessed by instances of its own class. So an instance of mountainBike, I'd think, would have no problem accessing its pedal() method. Furthermore, since bike's pedal() is public, it can be accessed by any bike class. That said, I think that if you tried to do it in reverse, and made Bike.pedal() private and mountainBike.pedal() public, that mountainBike.pedal() wouldn't be able to access the original pedal() method and the compiler would give you an error. It's been a while since I remember the exact wording of the error, but basically that you can't access a private method from outside its class.