Advertisement
ArmoredVortex

ds_lab

Apr 17th, 2025 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 KB | None | 0 0
  1. // Quicksort
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int partition(int arr[], int low, int high)
  6. {
  7.     int pivot = arr[high];
  8.     int i = low - 1;
  9.  
  10.     for (int j = low; j < high; j++)
  11.     {
  12.         if (arr[j] < pivot)
  13.         {
  14.             i++;
  15.             swap(arr[i], arr[j]);
  16.         }
  17.     }
  18.  
  19.     swap(arr[i + 1], arr[high]);
  20.     return i + 1;
  21. }
  22.  
  23. void quickSort(int arr[], int low, int high)
  24. {
  25.     if (low < high)
  26.     {
  27.         int pivotIndex = partition(arr, low, high);
  28.         quickSort(arr, low, pivotIndex - 1);
  29.         quickSort(arr, pivotIndex + 1, high);
  30.     }
  31. }
  32.  
  33. void printArray(int arr[], int size)
  34. {
  35.     for (int i = 0; i < size; i++)
  36.         cout << arr[i] << " ";
  37.     cout << endl;
  38. }
  39.  
  40. int main()
  41. {
  42.     int arr[] = {10, 7, 8, 9, 1, 5};
  43.     int size = sizeof(arr) / sizeof(arr[0]);
  44.  
  45.     cout << "Original array: ";
  46.     printArray(arr, size);
  47.  
  48.     quickSort(arr, 0, size - 1);
  49.  
  50.     cout << "Sorted array: ";
  51.     printArray(arr, size);
  52.  
  53.     return 0;
  54. }
  55.  
  56. //Mergesort
  57. #include <iostream>
  58. using namespace std;
  59.  
  60. void merge(int arr[], int left, int mid, int right)
  61. {
  62.     int n1 = mid - left + 1;
  63.     int n2 = right - mid;
  64.  
  65.     int *L = new int[n1];
  66.     int *R = new int[n2];
  67.  
  68.     for (int i = 0; i < n1; i++)
  69.         L[i] = arr[left + i];
  70.     for (int j = 0; j < n2; j++)
  71.         R[j] = arr[mid + 1 + j];
  72.  
  73.     int i = 0, j = 0, k = left;
  74.     while (i < n1 && j < n2)
  75.     {
  76.         arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
  77.     }
  78.  
  79.     while (i < n1)
  80.         arr[k++] = L[i++];
  81.     while (j < n2)
  82.         arr[k++] = R[j++];
  83. }
  84.  
  85. void mergeSort(int arr[], int left, int right)
  86. {
  87.     if (left < right)
  88.     {
  89.         int mid = left + (right - left) / 2;
  90.         mergeSort(arr, left, mid);
  91.         mergeSort(arr, mid + 1, right);
  92.  
  93.         merge(arr, left, mid, right);
  94.     }
  95. }
  96.  
  97. void printArray(int arr[], int size)
  98. {
  99.     for (int i = 0; i < size; i++)
  100.         cout << arr[i] << " ";
  101.     cout << endl;
  102. }
  103.  
  104. int main()
  105. {
  106.     int arr[] = {12, 11, 13, 5, 6, 7};
  107.     int size = sizeof(arr) / sizeof(arr[0]);
  108.  
  109.     cout << "Original array: ";
  110.     printArray(arr, size);
  111.  
  112.     mergeSort(arr, 0, size - 1);
  113.  
  114.     cout << "Sorted array: ";
  115.     printArray(arr, size);
  116.  
  117.     return 0;
  118. }
  119.  
  120. //Queue
  121. #include <bits/stdc++.h>
  122. using namespace std;
  123.  
  124. class Queue
  125. {
  126. private:
  127.     int *arr;
  128.     int frontIndex;
  129.     int rearIndex;
  130.     int capacity;
  131.     int count;
  132.  
  133. public:
  134.     Queue(int size)
  135.     {
  136.         capacity = size;
  137.         arr = new int[capacity];
  138.         frontIndex = 0;
  139.         rearIndex = -1;
  140.         count = 0;
  141.     }
  142.  
  143.     void enqueue(int item)
  144.     {
  145.         if (count == capacity)
  146.         {
  147.             cout << "Queue is full\n";
  148.             return;
  149.         }
  150.         rearIndex = (rearIndex + 1) % capacity;
  151.         arr[rearIndex] = item;
  152.         count++;
  153.     }
  154.  
  155.     void dequeue()
  156.     {
  157.         if (isEmpty())
  158.         {
  159.             cout << "Queue is empty\n";
  160.             return;
  161.         }
  162.         frontIndex = (frontIndex + 1) % capacity;
  163.         count--;
  164.     }
  165.  
  166.     int front()
  167.     {
  168.         if (isEmpty())
  169.         {
  170.             cout << "Queue is empty\n";
  171.             return -1;
  172.         }
  173.         return arr[frontIndex];
  174.     }
  175.  
  176.     bool isEmpty()
  177.     {
  178.         return (count == 0);
  179.     }
  180.  
  181.     int size()
  182.     {
  183.         return count;
  184.     }
  185. };
  186.  
  187. int main()
  188. {
  189.     Queue q(5);
  190.  
  191.     q.enqueue(10);
  192.     q.enqueue(20);
  193.     q.enqueue(30);
  194.  
  195.     cout << "Front: " << q.front() << endl; // Should print 10
  196.  
  197.     q.dequeue();
  198.     cout << "Front after dequeue: " << q.front() << endl; // Should print 20
  199.  
  200.     cout << "Size: " << q.size() << endl;
  201.  
  202.     return 0;
  203. }
  204.  
  205. //Linearprobe
  206. #include <iostream>
  207. using namespace std;
  208.  
  209. const int TABLE_SIZE = 10;
  210.  
  211. class HashTable
  212. {
  213. private:
  214.     int *table;
  215.     bool *occupied;
  216.  
  217. public:
  218.     HashTable()
  219.     {
  220.         table = new int[TABLE_SIZE];
  221.         occupied = new bool[TABLE_SIZE];
  222.  
  223.         for (int i = 0; i < TABLE_SIZE; i++)
  224.         {
  225.             table[i] = -1;
  226.             occupied[i] = false;
  227.         }
  228.     }
  229.  
  230.     int hash(int key)
  231.     {
  232.         return key % TABLE_SIZE;
  233.     }
  234.  
  235.     void insert(int key)
  236.     {
  237.         int index = hash(key);
  238.         int startIndex = index;
  239.  
  240.         while (occupied[index])
  241.         {
  242.             index = (index + 1) % TABLE_SIZE;
  243.             if (index == startIndex)
  244.             {
  245.                 cout << "Hash table is full!\n";
  246.                 return;
  247.             }
  248.         }
  249.  
  250.         table[index] = key;
  251.         occupied[index] = true;
  252.     }
  253.  
  254.     bool search(int key)
  255.     {
  256.         int index = hash(key);
  257.         int startIndex = index;
  258.  
  259.         while (occupied[index])
  260.         {
  261.             if (table[index] == key)
  262.                 return true;
  263.             index = (index + 1) % TABLE_SIZE;
  264.             if (index == startIndex)
  265.                 break;
  266.         }
  267.         return false;
  268.     }
  269.  
  270.     void display()
  271.     {
  272.         cout << "Hash Table:\n";
  273.         for (int i = 0; i < TABLE_SIZE; i++)
  274.         {
  275.             if (occupied[i])
  276.                 cout << i << " => " << table[i] << endl;
  277.             else
  278.                 cout << i << " => [empty]" << endl;
  279.         }
  280.     }
  281. };
  282.  
  283. int main()
  284. {
  285.     HashTable ht;
  286.     ht.insert(5);
  287.     ht.insert(15);
  288.     ht.insert(25);
  289.     ht.insert(35);
  290.     ht.insert(8);
  291.  
  292.     ht.display();
  293.  
  294.     cout << "\nSearching for 25: " << (ht.search(25) ? "Found" : "Not found") << endl;
  295.     cout << "Searching for 9: " << (ht.search(9) ? "Found" : "Not found") << endl;
  296.  
  297.     return 0;
  298. }
  299.  
  300. //ChainProbe
  301. #include <iostream>
  302. #include <string>
  303. using namespace std;
  304.  
  305. const int TABLE_SIZE = 10;
  306.  
  307. struct Node
  308. {
  309.     string key;
  310.     Node *next;
  311.     Node(string k) : key(k), next(nullptr) {}
  312. };
  313.  
  314. class HashTable
  315. {
  316. private:
  317.     Node *table[TABLE_SIZE];
  318.  
  319.     int hash(string key)
  320.     {
  321.         int hashValue = 0;
  322.         for (char c : key)
  323.             hashValue = (hashValue * 31 + c) % TABLE_SIZE;
  324.         return hashValue;
  325.     }
  326.  
  327. public:
  328.     HashTable()
  329.     {
  330.         for (int i = 0; i < TABLE_SIZE; i++)
  331.             table[i] = nullptr;
  332.     }
  333.  
  334.     void insert(string key)
  335.     {
  336.         int index = hash(key);
  337.         Node *newNode = new Node(key);
  338.  
  339.         newNode->next = table[index];
  340.         table[index] = newNode;
  341.     }
  342.  
  343.     bool search(string key)
  344.     {
  345.         int index = hash(key);
  346.         Node *curr = table[index];
  347.         while (curr)
  348.         {
  349.             if (curr->key == key)
  350.                 return true;
  351.             curr = curr->next;
  352.         }
  353.         return false;
  354.     }
  355.  
  356.     void display()
  357.     {
  358.         for (int i = 0; i < TABLE_SIZE; i++)
  359.         {
  360.             cout << i << " -> ";
  361.             Node *curr = table[i];
  362.             while (curr)
  363.             {
  364.                 cout << curr->key << " -> ";
  365.                 curr = curr->next;
  366.             }
  367.             cout << "NULL\n";
  368.         }
  369.     }
  370. };
  371.  
  372. int main()
  373. {
  374.     HashTable ht;
  375.  
  376.     ht.insert("apple");
  377.     ht.insert("banana");
  378.     ht.insert("grape");
  379.     ht.insert("orange");
  380.     ht.insert("mango");
  381.     ht.insert("lemon");
  382.  
  383.     ht.display();
  384.  
  385.     cout << "\nSearch 'banana': " << (ht.search("banana") ? "Found" : "Not Found") << endl;
  386.     cout << "Search 'peach': " << (ht.search("peach") ? "Found" : "Not Found") << endl;
  387.  
  388.     return 0;
  389. }
  390.  
  391. //isBST
  392. #include <bits/stdc++.h>
  393.  
  394. using namespace std;
  395.  
  396. struct NODE
  397. {
  398.     int data;
  399.     struct NODE *left;
  400.     struct NODE *right;
  401. };
  402.  
  403. int isBST(NODE *root)
  404. {
  405.     if (root == NULL)
  406.     {
  407.         return 1;
  408.     }
  409.     if (root->left != NULL && root->left->data > root->data)
  410.     {
  411.         return 0;
  412.     }
  413.     if (root->right != NULL && root->right->data < root->data)
  414.     {
  415.         return 0;
  416.     }
  417.     return isBST(root->left) * isBST(root->right);
  418. }
  419.  
  420. int main()
  421. {
  422.  
  423.     struct NODE *root = new NODE;
  424.     root->data = 10;
  425.     root->left = new NODE;
  426.     root->left->data = 20;
  427.     root->right = new NODE;
  428.     root->right->data = 15;
  429.  
  430.     if (isBST(root))
  431.     {
  432.         cout << "The tree is a BST" << endl;
  433.     }
  434.     else
  435.     {
  436.         cout << "The tree is not a BST" << endl;
  437.     }
  438.  
  439.     return 0;
  440. }
  441.  
  442. //AVL
  443. #include <iostream>
  444. using namespace std;
  445.  
  446. struct Node {
  447.     int key;
  448.     Node* left;
  449.     Node* right;
  450.     int height;
  451. };
  452.  
  453. int height(Node* node) {
  454.     return node ? node->height : 0;
  455. }
  456.  
  457. int getBalance(Node* node) {
  458.     return node ? height(node->left) - height(node->right) : 0;
  459. }
  460.  
  461. Node* createNode(int key) {
  462.     Node* node = new Node();
  463.     node->key = key;
  464.     node->left = node->right = nullptr;
  465.     node->height = 1;
  466.     return node;
  467. }
  468.  
  469. int max(int a, int b) {
  470.     return (a > b) ? a : b;
  471. }
  472.  
  473. Node* rightRotate(Node* y) {
  474.     Node* x = y->left;
  475.     Node* T2 = x->right;
  476.  
  477.     x->right = y;
  478.     y->left = T2;
  479.  
  480.     y->height = max(height(y->left), height(y->right)) + 1;
  481.     x->height = max(height(x->left), height(x->right)) + 1;
  482.  
  483.     return x;
  484. }
  485.  
  486. Node* leftRotate(Node* x) {
  487.     Node* y = x->right;
  488.     Node* T2 = y->left;
  489.  
  490.     y->left = x;
  491.     x->right = T2;
  492.  
  493.     x->height = max(height(x->left), height(x->right)) + 1;
  494.     y->height = max(height(y->left), height(y->right)) + 1;
  495.  
  496.     return y;
  497. }
  498.  
  499. Node* insert(Node* node, int key) {
  500.     if (!node)
  501.         return createNode(key);
  502.  
  503.     if (key < node->key)
  504.         node->left = insert(node->left, key);
  505.     else if (key > node->key)
  506.         node->right = insert(node->right, key);
  507.     else
  508.         return node; // Duplicates not allowed
  509.  
  510.     node->height = 1 + max(height(node->left), height(node->right));
  511.  
  512.     int balance = getBalance(node);
  513.  
  514.     if (balance > 1 && key < node->left->key)
  515.         return rightRotate(node);
  516.  
  517.     if (balance < -1 && key > node->right->key)
  518.         return leftRotate(node);
  519.  
  520.     if (balance > 1 && key > node->left->key) {
  521.         node->left = leftRotate(node->left);
  522.         return rightRotate(node);
  523.     }
  524.  
  525.     if (balance < -1 && key < node->right->key) {
  526.         node->right = rightRotate(node->right);
  527.         return leftRotate(node);
  528.     }
  529.  
  530.     return node;
  531. }
  532.  
  533. void inorder(Node* root) {
  534.     if (root != nullptr) {
  535.         inorder(root->left);
  536.         cout << root
  537.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement