Posts

Showing posts with the label Algorithm

Using Stack to reverse a word

Using a Stack structure (LIFO) can be handy for reversing a word. I'll show you how you can do that with python:

Simple Sorting : Insertion Sort

The insertion sort is better than the bubble sort and the selection sort. Process For a better understanding, we will describe the process from the middle. Outer loop : Record the current value (e.g. index 6) as temp (The values at the left are partially sorted). Inner loop : Start comparison to the left For each Inner pass : [If inner - 1 > temp] then [move inner - 1 to inner] else [move temp to inner] Outer loop : move to the right (e.g. index 7) continue above steps until end of outer loop Java: Python: The insertion sort runs in O(N 2 ) time. As a bonus, you can see below a version which removes duplicate:

Simple Sorting: Selection Sort

The selection sort is a little bit more involved than the bubble sort. This algorithm basically makes a pass through all the values for picking the shortest one. This shortest value is swapped with the leftmost value on each pass. The sorted values accumulate on the left (in the bubble sort they accumulated on the right). Process Start on the left (outer loop) Record first index as minimum value by default Compare each value on the right (inner loop) If a shortest value exists, replace the minimum index with the current one. Swap outer value with the minimum Repeat above steps until end of outer loop Java: Python The selection sort and bubble sort have the same number of comparisons [N * (N-1)/2]. However, the selection sort is faster because there are fewer swaps. The selection sort runs in O(N 2 ) time .

Simple Sorting : Bubble sort

Hi, Today, I'll begin a very long journey of exploring algorithms that you need to know as a software engineer. I'll do my best to describe each and every one simply by providing you sample code in java and python. Each subsequent post will describe one and only one algorithm. We start by the most simple of them : the Bubble Sort . The bubble sort is notoriously slow. Process We start at the left of the array, we compare values in positions 0 and 1 If the value at index 0 is bigger, swap them Move on the right When you reach the end of the first pass, you know that the last value is sorted, start over at the left of the array till N-x (the number of pass) Java: Python: The bubble sort runs in O(N 2 ) time.

Rounded Percentages up to 100% using Insertion-Sort Algorithm

When you need to represent percentages as whole numbers using   Math.round() ,  you end up with a total of 101%.  Value Percentage Rounded A 650 49.88% 50 B 230 17.65% 18 C 150 11.51% 12 D 273 20.95% 21 1303 100.00% 101 As you can see above the total in the rounded column is 101. Let's solve this problem using a well known algorithm. Step 1: We begin to sum the given values for calculating the percentages. While iterating, we copy all the properties to a destination object ( NVPPercentage ) which will be pushed to an array ( temp ) . Step 2: The insertion sort, is an efficient algorithm for sorting a small number of elements. We start for each element to calculate the percentage ( P ) and the rounded percentage ( RP ). We sum also the rounded percentages (e.g. 101). We also compare in an inner loop the current element ( CE ) with the left ones for sorting purpo...