Posts

Link List

#include "bits/stdc++.h" using namespace std; class Node { public: int data; Node *next; Node(int d) : data(d), next(NULL) {} }; class LinkedList { private: Node *head; public: LinkedList() : head(nullptr) {} ~LinkedList() { Node *curr = head; while(curr != NULL) { Node *nextNode = curr->next; delete curr; curr = nextNode; } } void insertAtHead(int data) { Node *tempNode = new Node(data); tempNode->next = head; head = tempNode; } void printList() { Node *currNode = head; if (currNode == nullptr) { cout data "; currNode = currNode->next; } cout data == key) { Node *tempNode = head; head = head->next; delete tempNode; return; } // 3. Delete last node Node *curr = head; Node *prev = nu...

Quick Sort

Image
  Lomuto Hoare

Shell Sort

Image
Shell Sort: Compare elements at K distance.  

Selection Sort

Image
Selection Sort : Take out the current element and compare with all elements to the right to find the smallest element. Swap current and smallest element. arr[] = {9, 6, 7, 5, 2};  

Insertion Sort

Image
  Insertion Sort : Take out the element and compare it with all elements before it.

Bubble Sort

Image
Bubble Sort: Compare two consecutive elements. Swap if they are out of order. Highest number will bubble to end after each pass.   

Merge Sort

Image