CS 2420 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Heap Sort Algorithm

Heap sort is a comparison based sorting technique. It is where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining element.

Use the example tree in the picture to traverse the binary tree in order:

In order (Left, Root, Right) : 4 2 5 1 3

Insertion Sort Algorithm

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Why is an AVL tree usefull?

Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of O(Logn) for all these operations.

Bubble Sort Algorithm big O notation memory usage:

O(1)

Hash table algorithm big O notation average case:

O(1)

Heap Sort Algorithm big O notation memory usage:

O(1)

Insertion Sort Algorithm big O notation memory usage:

O(1)

Selection Sort Algorithm big O notation memory usage:

O(1)

Binary search tree big O notation average case:

O(log(n))

Quick Sort Algorithm big O notation memory usage:

O(log(n))

Heap Sort Algorithm big O notation average case:

O(n log(n))

Heap Sort Algorithm big O notation worst case:

O(n log(n))

Merge Sort Algorithm big O notation average case:

O(n log(n))

Merge Sort Algorithm big O notation worst case:

O(n log(n))

Quick Sort Algorithm big O notation average case:

O(n log(n))

Binary search tree big O notation worst case:

O(n)

Hash table algorithm big O notation worst case:

O(n)

Merge Sort Algorithm big O notation memory usage:

O(n)

sequential search algorithm big O notation average case:

O(n)

sequential search algorithm big O notation worst case:

O(n)

Bubble Sort Algorithm big O notation average case:

O(n^2)

Bubble Sort Algorithm big O notation worst case:

O(n^2)

Insertion Sort Algorithm big O notation average case:

O(n^2)

Insertion Sort Algorithm big O notation worst case:

O(n^2)

Quick Sort Algorithm big O notation worst case:

O(n^2)

Selection Sort Algorithm big O notation average case:

O(n^2)

Selection Sort Algorithm big O notation worst case:

O(n^2)

Use the example tree in the picture to traverse the binary tree in post-order:

Post-order (Left, Right, Root) : 4 5 2 3 1

Use the example tree in the picture to traverse the binary tree in pre-order:

Pre-order (Root, Left, Right) : 1 2 4 5 3

Quick Sort Algorithm

QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

Bubble Sort Algorithm example:

int temp; loop i from 0 to array.length-1 loop j from 0 to array.length-1 if array[i] > array[i+1] then temp = array[i] array[i] = array[i+1] array[i+1] = temp end if end loop end loop

sequential search algorithm:

loop for i from 0 to array.length if searchkey == array[i] then ouput searchkey, "has been found!" break; end loop

How to traverse a graph using depth/stack first traversal

1) Visit the adjacent unvisited vertex. Mark it as visited. Put it in the stack. 2) If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.) 3) Repeat steps 1 and 2 until the stack is empty.

Hash table algorithm example:

#include<bits/stdc++.h> using namespace std; class Hash { int BUCKET; // No. of buckets // Pointer to an array containing buckets list<int> *table; public: Hash(int V); // Constructor // inserts a key into hash table void insertItem(int x); // deletes a key from hash table void deleteItem(int key); // hash function to map values to key int hashFunction(int x) { return (x % BUCKET); } void displayHash(); }; Hash::Hash(int b) { this->BUCKET = b; table = new list<int>[BUCKET]; } void Hash::insertItem(int key) { int index = hashFunction(key); table[index].push_back(key); } void Hash::deleteItem(int key) { // get the hash index of key int index = hashFunction(key); // find the key in (inex)th list list <int> :: iterator i; for (i = table[index].begin(); i != table[index].end(); i++) { if (*i == key) break; } // if key is found in hash table, remove it if (i != table[index].end()) table[index].erase(i); } // function to display hash table void Hash::displayHash() { for (int i = 0; i < BUCKET; i++) { cout << i; for (auto x : table[i]) cout << " --> " << x; cout << endl; } } // Driver program int main() { // array that contains keys to be mapped int a[] = {15, 11, 27, 8, 12}; int n = sizeof(a)/sizeof(a[0]); // insert the keys into the hash table Hash h(7); // 7 is count of buckets in // hash table for (int i = 0; i < n; i++) h.insertItem(a[i]); // delete 12 from hash table h.deleteItem(12); // display the Hash table h.displayHash(); return 0; }

How to process data using the Dijkstra's shortest path algorithm.

1) Pick first node and calculate distances to adjacent nodes. 2) Pick next node with minimal distance; repeat adjacent node distance calculations. 3) Final result of shortest-path tree

Selection Sort Algorithm example:

// C++ program for implementation of selection sort #include <bits/stdc++.h> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element swap(&arr[min_idx], &arr[i]); } } /* Function to print an array */ void printArray(int arr[], int size) { int i; for (i=0; i < size; i++) cout << arr[i] << " "; cout << endl; } // Driver program to test above functions int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); selectionSort(arr, n); cout << "Sorted array: \n"; printArray(arr, n); return 0; }

Insertion Sort Algorithm example:

// C++ program for insertion sort #include <bits/stdc++.h> using namespace std; /* Function to sort an array using insertion sort*/ void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } // A utility function to print an array of size n void printArray(int arr[], int n) { int i; for (i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } /* Driver code */ int main() { int arr[] = { 12, 11, 13, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); insertionSort(arr, n); printArray(arr, n); return 0; }

What are some ways to store graphs in a data structure?

1) Node objects in an array 2) A 2D array 3) A flat file of Tuples 4) CSR or CSC

How does a hash table algorithm work?

A hash table is a data structure which is used to store key-value pairs. Hash function is used by hash table to compute an index into an array in which an element will be inserted or searched.

What is stability in sorting?

A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted.

Selection Sort Algorithm

A sorting algorithm that keeps moving larger items toward the back of the list.

Bubble Sort Algorithm

A sorting algorithm that makes multiple passes through the list from front to back, each time exchanging pairs of entries that are out of order

What is an AVL tree?

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.

What is a B tree?

B-Tree is a self-balancing search tree. In most of the other self-balancing search trees (like AVL and Red-Black Trees), it is assumed that everything is in main memory. To understand the use of B-Trees, we must think of the huge amount of data that cannot fit in main memory. The main idea of using B-Trees is to reduce the number of disk accesses. Most of the tree operations (search, insert, delete, max, min, ..etc ) require O(h) disk accesses where h is the height of the tree. B-tree is a fat tree. The height of B-Trees is kept low by putting maximum possible keys in a B-Tree node.

How to traverse a graph using breadth/queue

Breadth First Traversal of the following graph is 2, 0, 3, 1.

Merge Sort Algorithm

Breaks data into small groups, puts the groups in order, then combines groups until all are in order

Bucket Sort Algorithm

Bucket Sort is a sorting technique that sorts the elements by first dividing the elements into several groups called buckets. The elements inside each bucket are sorted using any of the suitable sorting algorithms or recursively calling the same algorithm.

How to delete items in a binary search tree:

struct node* deleteNode(struct node* root, int key) { // base case if (root == NULL) return root; // If the key to be deleted is smaller than the root's key, // then it lies in left subtree if (key < root->key) root->left = deleteNode(root->left, key); // If the key to be deleted is greater than the root's key, // then it lies in right subtree else if (key > root->key) root->right = deleteNode(root->right, key); // if key is same as root's key, then This is the node // to be deleted else { // node with only one child or no child if (root->left == NULL) { struct node *temp = root->right; free(root); return temp; } else if (root->right == NULL) { struct node *temp = root->left; free(root); return temp; } // node with two children: Get the inorder successor //(smallest in the right subtree) struct node* temp = minValueNode(root->right); // Copy the inorder successor's content to this node root->key = temp->key; // Delete the inorder successor root->right = deleteNode(root->right, temp->key); } return root; }

How to insert items into a binary tree:

struct node* insert(struct node* node, int key) { /* If the tree is empty, return a new node */ if (node == NULL) return newNode(key); /* Otherwise, recur down the tree */ if (key < node->key) node->left = insert(node->left, key); else if (key > node->key) node->right = insert(node->right, key); /* return the (unchanged) node pointer */ return node; }

How to search for items in a binary tree:

struct node* search(struct node* root, int key) { // Base Cases: root is null or key is present at root if (root == NULL || root->key == key) return root; // Key is greater than root's key if (root->key < key) return search(root->right, key); // Key is smaller than root's key return search(root->left, key); }

Binary search tree algorithm:

struct node* search(struct node* root, int key) { // Base Cases: root is null or key is present at root if (root == NULL || root->key == key) return root; // Key is greater than root's key if (root->key < key) return search(root->right, key); // Key is smaller than root's key return search(root->left, key); }


Ensembles d'études connexes

Chapter 1 review for test - interpersonal communications

View Set

French Revolution K Essential Questions

View Set

Ch. 30 - Digital Image Preprocessing and Processing (Rescaling)

View Set

mastering assignment chapter respiratory system

View Set

Microbiology Bacteriology 2: Flagella and Pili

View Set

Driver's Education True or False

View Set

Test Your Knowledge Questions Course 6, Module 1

View Set