Data Structures and Sorting Algorithms
3 Linked List Properties
1. Count 2. First 3. Last
Linked List (System.Collections.Generic)
-A Linked List is a linear data structure which consists of a group of nodes in a sequence -Each node contains two parts: 1. Data− Each node of a linked list can store a data. 2. Address − Each node of a linked list contains an address to the next node, called "Next". -Head: The first node of a Linked List is referenced by a pointer called Head. -Adding and removing nodes: You can add nodes anywhere in a linked list. -Insertion and deletion is easy to implement -Number of nodes: The number of nodes in a linked list can grow or shrink on demand -Sorting: A linked list is a sequential data structure and cannot be sorted in another order -Data Storage: Each node of a linked list can store data. -Effective Memory Management: They are dynamic in nature and allocate memory as and when required. -Performance: It has faster access time and can be expanded without memory overhead -Backtracking is possible in doubly linked lists. -Null: LinkedList accepts null as a valid Value for reference types and allows duplicate values.
Linked Lists
-A Linked List is a linear data structure which consists of a group of nodes in a sequence. -Each node contains two parts: Data− Each node of a linked list can store a data. Address − Each node of a linked list contains an address to the next node, called "Next". -Head: The first node of a Linked List is referenced by a pointer called Head. -Adding and removing nodes: You can add nodes anywhere in a linked list. -Insertion and deletion is easy to implement. -Number of nodes: The number of nodes in a linked list can grow or shrink on demand. -Sorting: A linked list is a sequential data structure and cannot easily be sorted in another order -Data Storage: Each node of a linked list can store data. -Effective Memory Management: They are dynamic in nature and allocate memory as and when required. Since there is no need to define an initial size for a linked list, memory utilization is effective -Performance: It has faster access time and can be expanded without memory overhead. -Backtracking is possible in doubly linked lists.
Stacks
-A Stack is a last-in, first-out collection of objects (LIFO). -Push adds and item to the Stack. -Pop removes the last item from the Stack.
12 Linked List Methods
1) AddAfter 2) AddBefore 3) AddFirst 4) AddLast 5) Clear 6) Contains 7) CopyTo 8) Find 9) FindLast 10) Remove 11) RemoveFirst 12) RemoveLast
Stack (Namespace: System.Collections)
-A Stack is a last-in, first-out collection of objects (LIFO). -Push adds and item to the stack. -Pop removes the last item from the stack. -Peek: The Peek operation allows you to look at the current item at the top of the stack without removing it from the stack. -The capacity of a stack refers to the number of items it can hold. However, as elements are added to a stack, the stack's capacity is automatically increased. -A stack is a heterogeneous data structure, meaning that the items within it can be of different data types. -Contains: The contains operation allows you to determine whether a specific item exists in the stack. -Stacks are useful structures in runtime memory management, expression evaluation, method-call tracking, etc.
Queues
-A first-in, first-out collection of objects (FIFO). -Enqueue adds an item to the back of the Queue. -Dequeue removes the item that was added to the Queue first -A queue's items are stored in the same order they were added -The capacity of a Queue is the number of elements the Queue can hold. -As elements are added to a Queue, the capacity is automatically increased as required -The capacity of the Queue can be decreased by calling TrimToSiz
Queue (Namespace: System.Collections)
-A queue is a first-in, first-out collection of objects (FIFO). -Enqueue adds an item to the back of the Queue. -Dequeue removes the item that was added to the Queue first -A queue's items are stored in the same order they were added. -The capacity of a Queue is the number of elements the Queue can hold. -As elements are added to a Queue, the capacity is automatically increased as required through reallocation. -The capacity of the Queue can be decreased by calling TrimToSize. -Queue.Peek() Method: This method returns the object at the beginning of the Queue without removing it. This method is like the Dequeue method, but Peek does not modify the Queue.
Namespace: System (System.Array class)
-An array is a data structure in which you can store multiple variables of the same type -Indexes: Each element in the array has an index. The indexes are zero-based, so it starts at 0 -Lower bound: The lower bound of an array is the index of its first element (0 by default). -Access elements: You can access the elements in the array by using the index. -Length: The length of an array is the total number of elements it can contain. -Nice to know: The array size is limited to a total of 4 billion elements. -Arrays are reference types, but the elements of an array can be either a reference or value type -An array can be single-dimensional, multidimensional or jagged
Arrays
-An array is a data structure in which you can store multiple variables of the same type. -Each element in the array has an index. -The indexes are zero-based, so it starts at 0. -You can access the elements in the array by using the index.
Bubblesort
-BubbleSort works by comparing two elements to check whether they are out of order; if they are, it swaps them. -The algorithm continues to do this until the entire list is in the desired order. -Bubble sort is named this way because, in this sorting method, the smaller elements gradually bubble up to the top of the list.
What is a data structure?
-Data structures are techniques for organizing and storing data in computer memory. -A data structure is a technique for data organization, management, and storage in computer memory that enables efficient access and modification of the data. -Types of data structures in the MTA 98-361 exam: Arrays, Queues, Stacks, Dictionaries, Hashtables, Linked lists.
Dictionary (System.Collections.Generic namespace)
-Dictionaries represent a collection of data consisting of key/value pairs -The key will be a unique value in the Dictionary, but the same value can be assigned to more than one key -You can iterate through the items in a Dictionary to retrieve the key-value pair. -The Dictionary class in C# represents a generic data structure that can contain keys and values of data. Hence, you can store data of any type in a Dictionary instance. -Add: Use the Add method to add a key and value pair to the Dictionary -Remove(TKey): Removes the value with the specified key from the Dictionary -ContainsKey: Can be used to check if a key exists before adding it. -Item[TKey]: Gets or sets the value associated with the specified key -TryGetValue(TKey, TValue): Gets the value that is associated with the specified key.
Dictionaries
-Dictionaries represent a collection of data consisting of key/value pairs. -The key will be a unique value in the Dictionary, but the same value can be assigned to more than one key. -You can iterate through the items in a Dictionary to retrieve the key-value pair. -The Dictionary class in C# represents a generic data structure that can contain keys and values of data. Hence, you can store data of any type in a Dictionary instance -The Dictionary class is contained inside the System.Collections.Generic namespace.
Hashtable (System.Collections namespace)
-Hashtable represents a data structure that can store objects as key value pairs -You can search for a value in an instance of Hashtable class using the corresponding key. -Note that both the key and the value that is stored in a Hashtable instance is of the object type -You can iterate through the keys in Hastable.keys to retrieve the keys and values of the items in a Hashtable. Hashtable searhces: A Hashtable is known for fast searches: Searching an item in a Hashtable is faster compared to other nongeneric collections - let's understand why -A record in a Hashtable is stored in buckets (each bucket can contain multiple records) using hash keys -The hash key is in turn generated automatically by using a hashing algorithm -The MSDN states: "When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. -Subsequent lookups of the key use the hash code of the key to search in only one bucket, thus substantially reducing the number of key comparisons required to find an element.
Quicksort
-The Quicksort algorithm selects an element from an array as pivot element (p). -The numbers in the array are partitioned and moved to the left and right side of the pivot element: Numbers that are p to the right of p. -Then, the left subarray(start to p-1) and right subarray(p+1 to end) are sorted recursively to get the final sorted array as output. Different versions of Quicksort pick pivots in different ways, for example: 1) Pick the first or last element as a pivot. 2) Pick a random element as pivot. 3) Pick median as pivot.
one-dimensional array
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves using a one-dimensional index.
Difference Between Dictionary and Hashtables
Dictionary: -Namespace: System.Collections.Generic Generic collection - type safe, need to specify the type of the key and value when declaring and initializing the Dictionary -Key and value can be of any type. -Provides better performance than hashtable for value types, no boxing and unboxing required. Hashtable: -Namespace: System.Collections Non-generic - not type safe. Declaration does not make provision for specifying type -Both key and value is of the object type. -Use boxing and unboxing when working with value types. -Known for fast searches, because of hash codes assigned to buckets in which elements are stored. Searches use these hash codes.
Generic collections vs Non-generic collection
Generic collections are type safe and require a type specification when declared and initialized. Non-generic collection elements are of type object and require casting to and from the object type.
4 List Node Properties
LinkedListNoderepresents a node in a LinkedList 1) Lsit 2) Next 3) Previous 4) Value
Hashtables
The Hashtable class: -The Hashtable class is contained in the System.Collections namespace. -Hashtable represents a data structure that can store objects as key value pairs -You can search for a value in an instance of Hashtable class using the corresponding key -Note that both the key and the value that is stored in a Hashtable instance is of the object type -You can iterate through the keys in Hastable.keys to retrieve the keys and values of the items in a Hashtable. Hashtable searches: A Hashtable is known for fast searches: Searching an item in a Hashtable is faster compared to other non-generic collections - let's understand why: -A record in a Hashtable is stored in buckets (each bucket can contain multiple records) using hash keys. -The hash key is in turn generated automatically by using a hashing algorithm -The MSDN states: "When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. -Subsequent lookups of the key use the hash code of the key to search in only one bucket, thus substantially reducing the number of key comparisons required to find an element.