So today we're going to cover up how we can visualize the each steps of the algorithms we use .
Today I'm going to cover bubble sort algorithm visualizations via html boxes or you can say cards
each card is going to change it's values from the one as per the algorithm condition, so before doing anything let's understand about the bubble sort algorithm and the required things we need for this task.
Definition of Bubble Sort?
Bubble sort is one of the fundamental forms of sorting in programming. Bubble sort algorithms move through a sequence of data (typically integers) and rearrange them into ascending or descending order one number at a time. To do this, the algorithm compares number X to the adjacent number Y. If X is higher than Y, the two are swapped and the algorithm starts over.
What Does Bubble Sort Mean?
The name bubble sort comes from the fact that smaller or larger elements "bubble" to the top of a dataset. In the previous example of [3, 1, 4, 2], the 3 and 4 are bubbling up the dataset to find their proper positions.
This algorithm is alternatively called the sinking sort for the opposite reason; some of the elements are sinking to the bottom of the dataset. In our example, the 1 and the 2 are sinking elements.
So now we knows about bubble sort algorithm then let's checkout code one by one.
First we needed html structure as we have in video.
So let's start creating it, well I'm not so much good at frontend so the structure code can be messy,
but you guys can create your own structure and the only thing you need is just javascript code and you'll gonna get it at the end of the article.
In this article we are going to learn about how we can sort strings in golang the order we are going to implement is ascending order. If you are not familiar what ascending and descending order is let me explain you about it or if you knows then you can skip that part.
What is Ascending Order?
Ascending order is a method of arranging numbers from smallest value to largest value. The order goes from left to right. Ascending order is also sometimes named as increasing order. For example, a set of natural numbers are in ascending order, such as 1, 2, 3, 4, 5, 6, 7, 8… and same for the alphabets as well. The inverse method of increasing order isdescending order, where the numbers are arranged in decreasing order of values.
What is Descending Order?
If the information is sorted from highest to lowest, it is said to be in descending order. For example 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 are arranged in descending order. In other words, if the numbers are arranged from the largest to the smallest number, it is said to be in descending order.
In simple words, descending order is defined as an arrangement in the highest to lowest format. These concepts are related to decimals, numbers, fractions or amount of money.
Example of Descending Order
24, 20, 18, 12, 7 are arranged in descending order.
This is also known as decreasing order of numbers.
So now we knows what is ascending and descending order then let's move forward main part which is runes.
What is Rune in Golang?
Rune literals are just 32-bit integer values (however they're untyped constants, so their type can change). They represent unicode codepoints. For example, the rune literal 'a' is actually the number 97.
We have covered all reading part which was important while we were going to code, So let's start writing code.
1. Converting String To Slice of Rune
str :="fedcba"
runes := []rune(str)
So we've a str variable which have string type and then to get the indexes of unicodes we've used slice of rune.
2. Sorting The Rune Values
funcalphasort(str []rune, depth int) {
for x :=range str {
y := x +1
for y =range str {
if str[x] < str[y] {
str[x], str[y] = str[y], str[x]
}
}
}
}
In alphasort function we are using bubble sort and sorting the slice of rune via numbers because we can't directly compare characters which is bigger or not.
3. Converting Rune Values Back To String
funcruneToCharacter(r []rune) string {
str :=""
for _, c :=range r {
str += fmt.Sprintf("%c", c)
}
return str
}
After getting our sorted slice of rune we're getting a properly ready string and also converting rune value back to string characters with the help of loop, if you're trying to convert it without help of loop you'll gonna get the error.
[Golang] How To Sort Array | Slice Using Golang: Bubble Sort Algorithm Implementation.
Bubble Sort Algorithm
In this Article we are going to work on the bubble sort algorithm in practice in multiple ways to get the sort array using bubble sort algorithm.
What is Bubble Sort Algorithm?
Bubble sort is a sorting algorithm that works by repeatedly stepping through lists that need to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. This passing procedure is repeated until no swaps are required, indicating that the list is sorted. Bubble sort gets its name because smaller elements bubble toward the top of the list.
Bubble sort is also referred to as sinking sort or comparison sort.
Explaination of Bubble Sort
Bubble sort has a worst-case and average complexity of O(n2), where n is the number of items sorted. Unlike the other sorting algorithms, bubble sort detects whether the sorted list is efficiently built into the algorithm. Bubble sort performance over an already sorted list is O(n).
The position of elements in bubble sort plays an important role in determining performance. Large elements at the beginning do not pose a problem as they are easily swapped. The small elements toward the end move to the beginning slowly. As such, these elements are called rabbits and turtles.
The bubble sort algorithm can be optimized by placing larger elements in the final position. After every pass, all elements after the last swap are sorted and do not need to be checked again, thereby skipping the tracking of swapped variables.
so now we've understand the what bubble sort is then start implementing it one by one in all possible ways
1. Incremental Linear Pointer Way
what does it mean by linear pointer way, For first method, we are going to compare current index value with the next indexes values one by one.
Let's See the Code to have better understanding
// Way 1 next index checking with current index
// 1. x pointer is on index 0
// 2. y pointer in on index 1
// each time whenever y execution finishes it increment values for both
// whenever x starts again for next index
for x :=range arr {
var swapped bool
y := x +1
for y =range arr {
if arr[x] < arr[y] {
arr[x], arr[y] = arr[y], arr[x]
swapped =true
}
}
if!swapped {
break
}
}
2. Backward Pointer Way
So in this we're moving our first loop from index 1 instead of 0 and for second loop we're running it from 0 index
Let's See the Code
// Way 2 previous index checking with current index
// 1. x pointer is on index 1
// 2. y pointer is on index 0
// whenever y finishes indexes increments for both
x :=1
for x =range arr {
var swapped bool
y := x -1
for y =range arr {
if arr[x] < arr[y] {
arr[x], arr[y] = arr[y], arr[x]
swapped =true
}
}
if!swapped {
break
}
}
3. Left and Right Comparison
In this we're running our from both ends one is starting from index 0 and another one is starting from last index and both are incrementing and decrementing
Let's See the Code
// Way 3 comparing from both sides
// In this bubble sort solution we're trying to read values from both sides
// from left and at the same time from right and
// comparing both and swapping both values
for x :=range arr {
var swapped bool
y :=len(arr) - x -1
for y =range arr {
if arr[x] < arr[y] {
arr[x], arr[y] = arr[y], arr[x]
swapped =true
}
}
if!swapped {
break
}
}
4. Reverse Method End To Start Comparison
We are running loop from end to start for this example.
Let's Checkout the Code
// Way 4 opposite of way 1
// In this bubble sort we're sorting array via right side
for x :=len(arr) -1; x >=0; x-- {
var swapped bool
for y := x -1; y >=0; y-- {
if arr[x] < arr[y] {
arr[x], arr[y] = arr[y], arr[x]
swapped =true
}
}
if!swapped {
break
}
}
So Guys You have one task as we have example 2 I want you all to perform reverse method for that as well as and comment it below.