Professional Documents
Culture Documents
(SESSION 2022-23)
PROJECT BASED LEARNING
ON
Sorting Visualizer
SUBMITTED BY :
Deepanshu Gaur 2000290110057
PROJECT GUIDE :
MR. Praveer Dubey
INDEX
S.NO. TOPIC
1. INTRODUCTION
2. THEORY
3. CODE
4. OUTPUTS
5. CONCLUSION
INTRODUCTION
Sorting algorithms are used to sort a data structure
according to a specific order relationship, such as
numerical order or lexicographical order.
This operation is one of the most important and
widespread in computer science. For a long time, new
methods have been developed to make this procedure
faster and faster.
There are currently hundreds of different sorting
algorithms, each with its own specific characteristics.
They are classified according to two metrics: space
complexity and time complexity.
THEORY
BUBBLE SORT
In this tutorial, you will learn about the bubble sort algorithm and its
implementation in Python, Java, C, and C++.
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps
them until they are in the intended order.
Just like the movement of air bubbles in the water that rise up to the surface, each
element of the array move to the end in each iteration. Therefore, it is
called a bubble sort.
2. If the first element is greater than the second element, they are
swapped.
3. Now, compare the second and the third elements. Swap them if they are not in
order.
4. The above process goes on until the last element.
After each iteration, the largest element among the unsorted elements is placed
at the end.
Put the largest element at the end
In each iteration, the comparison takes place up to the last unsorted element.
The array is sorted when all the unsorted elements are placed at their correct
positions.
Selection Sort
In this tutorial, you will learn about the selection sort algorithm and its
implementation in Python, Java, C, and C++.
Selection sort is a sorting algorithm that selects the smallest element from an
unsorted list in each iteration and places that element at the beginning of the
unsorted list.
Working of Selection Sort
Compare minimum with the third element. Again, if the third element is
smaller, then assign minimum to the third element otherwise do nothing.
The process goes on until the last element.
Compare minimum with the
remaining elements
3. After each iteration, minimum is placed in the front of the unsorted list.
T
h
e
Insertion Sort f
o
In this tutorial, you will learn about insertion sort algorithm and its u
r
implementation in C, C++, Java and Python.
t
h
Insertion sort is a sorting algorithm that places an unsorted element at its
iteration
suitable place in each iteration.
Insertion sort works similarly as we sort cards in our hand in a card game.
We assume that the first card is already sorted then, we select an unsorted card. If
the unsorted card is greater than the card in hand, it is placed on the
right otherwise, to the left. In the same way, other unsorted cards are taken
and put in their right place.
Initial array
1. The first element in the array is assumed to be sorted. Take the second
element and store it separately in key.
Compare key with the first element. If the first element is greater
than key, then key is placed in front of the first element.
Take the third element and compare it with the elements on the left of it.
Placed it just behind the element smaller than it. If there is no element smaller
than it, then place it at the beginning of the array.
Place 1 at the
beginning
3. Similarly, place every unsorted element at its correct position.
Place 4 behind 1
Place 3 behind 1 and
the array is sorted
Quick Sort
In this tutorial, you will learn about the quick sort algorithm and its
implementation in Python, Java, C, and C++.
While dividing the array, the pivot element should be positioned in such
a way that elements less than pivot are kept on the left side and
elements greater than pivot are on the right side of the pivot.
2. The left and right subarrays are also divided using the same approach. This
process continues until each subarray contains a single element.
2. If the element is greater than the pivot element, a second pointer is set for that
element.
If the element
is greater than the pivot element, a second pointer is set for that element.
3. Now, pivot is compared with other elements. If an element smaller than the
pivot element is reached, the smaller element is swapped with the greater
element found earlier.
Pivot is
compared with other elements.
4. Again, the process is repeated to set the next greater element as the second
pointer. And, swap it with another smaller element.
The process is
repeated to set the next greater element as the second pointer.
The process
goes on until the second last element is reached.
Finally, the
pivot element is swapped with the second pointer.
3. Divide Subarrays
Pivot elements are again chosen for the left and the right sub-parts separately. And,
step 2 is repeated.
Select pivot element of in
each half and put at correct place using recursion
The subarrays are divided until each subarray is formed of a single element. At this
point, the array is already sorted.
Merge Sort
In this tutorial, you will learn about merge sort algorithm and its implementation in C,
C++, Java and Python.
Merge Sort is one of the most popular sorting algorithms that is based on the
principle of Divide and Conquer Algorithm.
Here, a problem is divided into multiple sub-problems. Each sub-problem is solved
individually. Finally, sub-problems are combined to form the final solution.
Divide
If q is the half-way point between p and r, then we can split the
subarray A[p..r] into two arrays A[p..q] and A[q+1, r] .
Conquer
In the conquer step, we try to sort both the subarrays A[p..q] and
A[q+1, r]. If
we haven't yet reached the base case, we again divide both these subarrays and try
to sort them.
Combine
When the conquer step reaches the base step and we get two sorted subarrays
A[p..q] and A[q+1, r] for array A[p..r], we combine the results by
creating a sorted array A[p..r] from two sorted subarrays A[p..q] and A[q+1, r] .
MergeSort Algorithm
The MergeSort function repeatedly divides the array into two halves until we reach a
stage where we try to perform MergeSort on a subarray of size 1 i.e. p
== r .
After that, the merge function comes into play and combines the sorted arrays into
larger arrays until the whole array is merged.
MergeSort(A, p, r):
if p > r
return
q = (p+r)/2
mergeSort(A, p,
q) mergeSort(A,
q+1, r) merge(A, p,
q, r)
To sort an entire array, we need to call MergeSort(A, 0, length(A)-1) .
As shown in the image below, the merge sort algorithm recursively divides the array
into halves until we reach the base case of array with 1 element. After
that, the merge function picks up the sorted sub-arrays and merges them to
gradually sort the entire array.
The algorithm maintains three pointers, one for each of the two arrays and one for
maintaining the current index of the final sorted array.
No:
Yes:
Code
<!DOCTYPE
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sorting Visualizer</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-
beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<header>
<h1 align="center">Sorting Visualizer</h1>
<nav>
<div class="row">
<div class="col gap-2 d-sm-flex" id="newArray">
<button type="button" class="btn btn-outline-success btn-dark
newArray">New Array</button>
</div>
<div class="col" id="input">
<span id="size">Size
<input id="arr_sz" type="range" min="5" max="100" step=1
value=60>
</span>
<span id="speed">Speed
<input id="speed_input" type="range" min="20" max="300"
stepDown=10 value=60>
</span>
</div>
<div class="col gap-2 d-sm-flex justify-content-end">
<button type="button" class="btn btn-outline-primary btn-dark
bubbleSort">Bubble Sort</button>
<button type="button" class="btn btn-outline-primary btn-dark
selectionSort">Selection Sort</button>
<button type="button" class="btn btn-outline-primary btn-dark
insertionSort">Insertion Sort</button>
<button type="button" class="btn btn-outline-primary btn-dark
quickSort">Quick Sort</button>
<button type="button" class="btn btn-outline-primary btn-dark
mergeSort">Merge Sort</button>
</div>
</div>
</nav>
</header>
<body class="p-3 mb-2 bg-dark text-white">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-
beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-
b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0"
crossorigin="anonymous"></script>
<script src="js_files/sorting.js"></script>
<script src="js_files/bubble.js"></script>
<script src="js_files/insertion.js"></script>
<script src="js_files/merge.js"></script>
<script src="js_files/quick.js"></script>
<script src="js_files/selection.js"></script>
</body>
</html>
Style.css
body
{
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
padding: 0 20px 30px 0;
line-height: 1.4;
}
.flex-container{
margin-top: 20px;
display: flex;
flex-wrap: nowrap;
width: 100%;
height: 500px;
justify-content: center;
transition: 2s all ease;
}
.flex-item{
background: cyan;
border: 1pt solid black;
width: 10px;
transition: 0.1s all ease;
}
.row{
display: grid;
grid-template-columns: 1fr 1fr 2fr;
}
#input{
display: flex;
padding: 10px;
justify-content: space-around;
}
Main code
Bubble.js
async
function
bubble()
{
console.log('In bubbe()');
const ele = document.querySelectorAll(".bar");
for(let i = 0; i < ele.length-1; i++){
console.log('In ith loop');
for(let j = 0; j < ele.length-i-1; j++){
console.log('In jth loop');
ele[j].style.background = 'blue';
ele[j+1].style.background = 'blue';
if(parseInt(ele[j].style.height) > parseInt(ele[j+1].style.height)){
console.log('In if condition');
await waitforme(delay);
swap(ele[j], ele[j+1]);
}
ele[j].style.background = 'cyan';
ele[j+1].style.background = 'cyan';
}
ele[ele.length-1-i].style.background = 'green';
}
ele[0].style.background = 'green';
}
Insertion.js});
async
function
insertion(){
console.log('In insertion()');
const ele = document.querySelectorAll(".bar");
// color
ele[0].style.background = 'green';
for(let i = 1; i < ele.length; i++){
console.log('In ith loop');
let j = i - 1;
let key = ele[i].style.height;
// color
ele[i].style.background = 'blue';
await waitforme(delay);
await waitforme(delay);
// color
for(let k = i; k >= 0; k--){
ele[k].style.background = 'green';
}
}
ele[j + 1].style.height = key;
// color
ele[i].style.background = 'green';
}
}
console.log('In merge()');
console.log(`low=${low}, mid=${mid}, high=${high}`);
const n1 = mid - low + 1;
const n2 = high - mid;
console.log(`n1=${n1}, n2=${n2}`);
let left = new Array(n1);
let right = new Array(n2);
ele[k].style.height = left[i];
i++;
k++;
}
else{
console.log('In merge while loop else');
// color
if((n1 + n2) === ele.length){
ele[k].style.background = 'green';
}
else{
ele[k].style.background = 'lightgreen';
}
ele[k].style.height = right[j];
j++;
k++;
}
}
while(i < n1){
await waitforme(delay);
console.log("In while if n1 is left");
// color
if((n1 + n2) === ele.length){
ele[k].style.background = 'green';
}
else{
ele[k].style.background = 'lightgreen';
}
ele[k].style.height = left[i];
i++;
k++;
}
while(j < n2){
await waitforme(delay);
console.log("In while if n2 is left");
// color
if((n1 + n2) === ele.length){
ele[k].style.background = 'green';
}
else{
ele[k].style.background = 'lightgreen';
}
ele[k].style.height = right[j];
j++;
k++;
}
}
Selection.js
async
function
selection(){
console.log('In selection()');
const ele = document.querySelectorAll(".bar");
for(let i = 0; i < ele.length; i++){
console.log('In ith loop');
let min_index = i;
// Change color of the position to swap with the next min
ele[i].style.background = 'blue';
for(let j = i+1; j < ele.length; j++){
console.log('In jth loop');
// Change color for the current comparision (in consideration
for min_index)
ele[j].style.background = 'red';
await waitforme(delay);
if(parseInt(ele[j].style.height) <
parseInt(ele[min_index].style.height)){
console.log('In if condition height comparision');
if(min_index !== i){
// new min_index is found so change prev min_index color
back to normal
ele[min_index].style.background = 'cyan';
}
min_index = j;
}
else{
// if the currnent comparision is more than min_index change
is back to normal
ele[j].style.background = 'cyan';
}
}
await waitforme(delay);
swap(ele[min_index], ele[i]);
// change the min element index back to normal as it is swapped
ele[min_index].style.background = 'cyan';
// change the sorted elements color to green
ele[i].style.background = 'green';
}
}
console.log('In partitionLomuto()');
let i = l - 1;
// color pivot element
ele[r].style.background = 'red';
for(let j = l; j <= r - 1; j++){
console.log('In partitionLomuto for j');
// color current element
ele[j].style.background = 'yellow';
// pauseChamp
await waitforme(delay);
if(parseInt(ele[j].style.height) <
parseInt(ele[r].style.height)){
// pauseChamp
await waitforme(delay);
// color
for(let k = 0; k < ele.length; k++){
if(ele[k].style.background != 'green')
ele[k].style.background = 'cyan';
}
return i;
}
function disableSortingBtn(){
document.querySelector(".bubbleSort").disabled = true;
document.querySelector(".insertionSort").disabled = true;
document.querySelector(".mergeSort").disabled = true;
document.querySelector(".quickSort").disabled = true;
document.querySelector(".selectionSort").disabled = true;
}
function disableSizeSlider(){
document.querySelector("#arr_sz").disabled = true;
}
function disableNewArrayBtn(){
document.querySelector(".newArray").disabled = true;
}
function waitforme(milisec) {
return new Promise(resolve => {
setTimeout(() => { resolve('') }, milisec);
})
}
// create multiple element div using loop and adding class 'bar col'
for (let i = 0; i < noOfBars; i++) {
const bar = document.createElement("div");
bar.style.height = `${array[i]*2}px`;
bar.classList.add('bar');
bar.classList.add('flex-item');
bar.classList.add(`barNo${i}`);
bars.appendChild(bar);
}
}
// Helper function to delete all the previous bars so that new can be added
function deleteChild() {
const bar = document.querySelector("#bars");
bar.innerHTML = '';
}
This project will guide you step by step to complete this project and at the end of
this project you will have an immense grip on some core concepts of JavaScript
as well. Adding this project on your resume will showcase your skills and add a
great value to your profile.
This project is a good start for beginners and a refresher for professionals who
have dabbled in data structures and algorithms using JavaScript before and
also web developers. The methodology can be applied to showcase any
algorithm of one's choosing, so feel free to innovate!