Singly Linked list selection sort - sorting

Hey so I have a class SinglyLinkedList and it contains my Node class, iterator class, and implements the List ADT (I dont think i need to post my code but if I do let me know). My question is this:
If I want to make a selected sort method (bubble etc), would it be "best practice" to put the Selected Sort method in its own class, or should it be put within the singly linked list. I want to put it within the Singly Linked List so it can have access to directly manipulating the Nodes (as the node class within my singly linked list is private) as it would be more efficient. But for some reason, some part of me thinks it would be a good idea to have it within its own class...
Any ideas on what would be best practice? Should I have my selected sort method(for singly linked lists) within the SinglyLinkedList or in its own class?
(i am using java, but that doesn't matter to much I dont think).

Related

What is the difference between Arrays, Linked-lists and Stacks?

I'm currently learning the fundamentals of Algorithms and Data Structures and I am slightly confused about the concepts and the differences between arrays, linked-lists and stacks.
Please correct me if I am wrong: Is stack more like an abstract concept, and arrays and linked-lists are data structures? (Hence we can use either arrays or linked-lists to implement the concept of stack)
Update - 032221
Thank you everyone for helping me out for this question! Back when I asked this question, I had a hard time understanding the overall concept of primitive data types and fundamental data structure (in this case an array) that is offered by each languages.
For example, linked list or queues can be created and implemented using array, but then I thought such linked list and queues still should be called as array (because the foundational data structure that is used behind is technically an array). My thought process was there would be primitive data structures for linked lists or queues that does not use an array. Thus I did not understand quite properly linked-lists or stacks or such data structures are just different patterns and ways that data is organized and configured.
I hope this can help anyone who is having a hard time understanding data structure concept like I did!
Array
A book is an array. You can go to any page by index and quickly go forwards or backwards by any increment you like.
Linked List
A scavenger hunt is a linked list. You can only go from one item to the next, because each item contains the information where to find the next item.
Stack
A pile of letters on your desk is a stack. You can only see the letter lying on top. Removing the top letter reveals the next letter.
From Programming Theory perspective all the tree are Abstract Data Types The fundamental differences between them are their features.
Array - offers a random access to any element in constant time, but removing or adding an element from/into an array is done in linear time
Linked List - offers random access in linear time (that means sequential access). But adding or removing an element is done in constant time.
Stack is a little bit different. It offers you access only to a single element - the top of the stack. The same is valid to removing and adding an element. You can remove only the element that is on the top of the stack and vice versa, add new element on the top of the stack.
You are correct that the stack can be implemented using array or linked list but this is already an implementation point of view. From Theory perspective they are different Abstract Data Types.
In fact if you dig deep enough on the assembly language level you have only 2 options to locate an arbitrary element:
Indexing - Calculate the element location in memory
Indirect addressing - Using a pointer stored in memory to locate the element.
ARRAY - a container that can be called a fixed number of items and those items should be of the same type.
STACK - a list of elements in which an element may be added or deleted only at one end called the top of the stack.
LINKEDLIST - a linear collection of data elements called a node pointing to the next node by means of a pointer.

Singly Linked List (General)

To put things short, I am learning about data structures and started building my own library (for practice). While comparing my implementation to the existing STL one I saw quite some differences. That is, when I insert an element in my singly linked list I push elements to the back of the list, while C++11's forward_list only allows pushing to the front (which actually makes sense, because the insertion has to be of complexity O(1)). My question is, why the books that present the idea of a singly linked list push and pop from back and the STL (which is supposed to be right one) pushes and pops from the front? Is one of the two the correct one, or is it just a matter of preference?
Please don't be harsh on me if the question sounds silly, but I have literally spent days trying to understand this and I didn't manage to find and trustful help online. Thank you very much for taking your time!

List implementation in ruby?

I'm a bit struggling with the understanding of the Array class in Ruby. I have seen on Google
that an Array class is actually more of a list, but I can't seem to find how it actually works.
I am really concerned with performance issues as I have to deal with large sorted lists, and
I don't want to step over the whole array to add a single element to it.
So I was wondering if there were any real and clear implementation of a list (as in caml for instance), and I am also looking for a good documentation about how Array's method are implemented, regarding optimization matters.
Thanks!
A Ruby array offers a full list interface:
push/<< for adding element at the end
each provides an iterator for list traversal
sort lets you sort the items with an optional block for a custom comparator
...
So there's no apparent need to have a special List class or Module - take Java for example, we end up using ArrayList if we need a List all the time because it gives us good performance and the additional benefit of accessing elements by their index. So Ruby (similar to other languages such as Python, PHP or Lua) tries to keep things simple with regards to collection types by offering just three types - Array, Hash and Set - therefore with a rich interface that makes it easy to emulate other collection types such as a List, Queue or Deque etc.
If you'd like to know details about the implementation, I would recommend simply downloading the Ruby sources and investigating the corresponding files (for MRI it's array.c in the top-level directory) .

Looking for a good definition of a map, and if maps can be implemented using trees

I'm going through some potential interview questions, one of which being can you implement a map or a linked list using a tree.
But even after some time googling I don't have a clear idea exactly what a map is, how it differs from an array or a hash table for example. Could anybody provide a clear description.
Can it, and a linked list be written as a tree?
A Map, aka Dictionary or associative array, is a data structure that allows you to look up a value using a key.
A Java Map can be implemented as a HashMap or a TreeMap; that suggests that hash map is one possible implementation and yes, you can implement a Map as a tree.
Can it (a map), and a linked list be written as a tree?
Maps are usually represented with arrays. To determine where an entry goes in a map, you need to compute its key. Look here for a better explanation.
Trees (with an undetermined number of nodes) can be implemented using lists (see here for further discussion). Lists are not usually implemented as trees.
I'd encourage you to get this book which is a classic on data structures and will give you alot of really great information.

Search algorithm for a sorted double linked list

As a learning excercise, I've just had an attempt at implementing my own 'merge sort' algorithm. I did this on an std::list, which apparently already had the functions sort() and merge() built in. However, I'm planning on moving this over to a linked list of my own making, so the implementation is not particuarly important.
The problem lies with the fact that a std::list doesnt have facilities for accessing random nodes, only accessing the front/back and stepping through. I was originally planning on somehow performing a simple binary search through this list, and finding my answer in a few steps.
The fact that there are already built in functions in an std::list for performing these kinds of ordering leads me to believe that there is an equally easy way to access the list in the way I want.
Anyway, thanks for your help in advance!
The way a linked list works is that you step through the items in the list one at a time. By definition there is no way to access a "random" element in the list. The Sort method you refer to actually creates a brand new list by going through each node one at a time and placing items at the correct location.
You'll need to store the data differently if you want to access it randomly. Perhaps an array of the elements you're storing.
Further information on linked lists: http://en.wikipedia.org/wiki/Linked_list
A merge sort doesn't require access to random elements, only to elements from one end of the list.

Resources