Posts

Showing posts with the label algorithms

Sorting - Bubble Sort

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AsscendingOrder { class Program { static void Main(string[] args) { int[] a = { 2,1,2,1,2,13,19,31,6}; for (int j = a.Length - 1; j > 0; j--) { for (int i = 0; i < j; i++) { if (a[i] > a[i + 1]) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } } for (int i = 0; i < a.Length; i++ ) Console.WriteLine(a[i]); Console.ReadLine(); } } }

Binary Search Tree Operations

#include "iostream" #include "cstdlib" using namespace std; class BinarySearchTree { private: struct tree_node { int data; tree_node* left; tree_node* right; }; tree_node* root; public: BinarySearchTree() { root = NULL; } void Print_PreOrder(); void Print_PostOrder(); void Print_InOrder(); void inorder(tree_node*); void preorder(tree_node*); void postorder(tree_node*); void insert(int); }; void BinarySearchTree::insert(int d) { tree_node* current = new tree_node; current->data = d; current->left = NULL; current->right = NULL; tree_node* parent; parent = root; tree_node* temp; if(root == NULL) { root = current; } else { while(parent) { temp = parent; if(current->data > parent->data ) { parent = parent->right; } else { parent = parent->left; } } if(current->data > temp->data) { temp->right = current; } else { temp->left = current; } } } void Bina...