Posts

Showing posts from June, 2018

Selection Sort algorithm in depth with C++

Image
To represent this sorting algorithm we can visualize an array of unsorted integers. From there we start by marking the first integer and calling it Small . All the other numbers in the array will then be to the right of Small . We will then compare Small with all the other integers to the right. Checking if they are less than Small , and if so, we will be replacing the variable Small with the lowest value found. Let's imagine we have a array of integers 7, 8, 5, 4, 9, 2. In that case Small will be equal to 7 (Being the blue arrow). Now let's compare Small with the numbers to the right (The yellow arrow). Checking if they are in fact less than Small . So with the example above, we will first be checking if 8 < 7 . If that's true, then we will have to switch Small = 7 with Small = 8 . In this case, that simply isn't true. So we will have to move on with comparing Small = 7 with the other integers in the list. For the next step we can see that 5 wil

Stack data structure with C++

A stack is a data structure based on the concept of LIFO (Last In, First Out), which is a simple yet potent idea in computing. With it's two operations pop and push we are given the opportunity to control our stack and the elements we want to place in it. For adding new elements we can say that they're being pushed onto the top of the stack. Whilst if we want to remove something, the first element is popped from the stack. Let's see some code for this in C++ with integers and try to break it up. We can start out by creating a class for our stack. class Stack { public :        void push( int n );        void pop();        void top();        void size();        bool is_empty(); private :        int stack[100];        int stack_size = 0; }; From there we can start breaking up each function and variable inside the class. Going into the private section of the class we can see that we have two variables, one being an array and the othe