Detailed Linked List Searching Sorting Notes
Detailed Linked List Searching Sorting Notes
1. Linked Lists
Linked Lists are a linear data structure where elements are connected via pointers. Each element,
2. Pointer: A reference to the next (or previous, in doubly linked lists) node in the sequence.
Advantages:
Types:
1. Singly Linked List: Each node contains a pointer to the next node.
2. Doubly Linked List: Each node contains pointers to both the next and previous nodes.
Key Operations:
class Node {
public:
int data;
Node* next;
data = value;
next = nullptr;
};
class SinglyLinkedList {
private:
Node* head;
public:
newNode->next = head;
head = newNode;
temp->next = newNode;
}
void traverse() {
while (temp) {
temp = temp->next;
";
void deleteAtStart() {
if (!head) return;
head = head->next;
delete temp;
void deleteAtEnd() {
if (!head) return;
delete temp->next;
temp->next = nullptr;
}
};
2. Searching Algorithms
1. Linear Search:
Algorithm:
Example Code:
return -1;
2. Binary Search:
- Requires sorted data.
- Divides the search space in half and checks the middle element.
Algorithm:
1. Set pointers (low and high) at the start and end of the array.
Example Code:
return -1;
3. Sorting Algorithms
Sorting arranges data in ascending or descending order.
1. Bubble Sort:
Example Code:
2. Selection Sort:
Example Code:
int minIndex = i;
swap(arr[i], arr[minIndex]);
}
3. Quick Sort:
- Divides the array into two parts (based on a pivot) and recursively sorts them.
4. Merge Sort:
- Recursively divides the array, sorts each half, and merges them.
These algorithms are faster for larger data sets but require recursion and additional space.
4. Complete Example
Below is a complete implementation of a singly linked list in C++ with sorting and searching
functionalities.
Refer to the provided examples to implement insertion, deletion, bubble sort, linear search, and
binary search.