Mini Project Report: Visual Applications of Sorting Algorithms
Mini Project Report: Visual Applications of Sorting Algorithms
Mini Project Report: Visual Applications of Sorting Algorithms
On
VISUAL APPLICATIONS OF
SORTING ALGORITHMS
We have taken efforts in this project. However, it would not have been possible
without the kind support and help of many individuals and references. We would
like to extend our sincere thanks to all of them.
We are highly indebted to Ms. Arti Pandey ma’am and Ms.Priyanka Gaba ma’am
for her guidance and constant supervision as well as for providing necessary
information regarding the project & also for support in completing the project.
We would like to express our gratitude towards ma’am for her kind co-operation
and encouragement which help us in completion of this project.
We would like to express our special gratitude and thanks to faculty for giving us
such attention and time.
Our thanks and appreciations also go to our colleague in developing the project
and people who have willingly helped us out with their abilities.
Table of contents
1. Introduction
4. Code
5. Dataflow diagram
9. Conclusion
10. Bibliography
1. INTRODUCTION
1. Numbers of inputs are given user can sort them by different sorting
algorithms given in this project;
3. One can modify the number of inputs, and order in which they are
arranged.
a) Bubble sort
b) Selection sort
c) Insertion sort
d) Quick sort
e) Heap sort
f) Merge sort
3. CODES
async function
bubblesort(aa)
{
var n = aa.length;
for (var i = 0; i < n; i++) {
for (var j = 0; j < n - i - 1; j++) {
if (aa[j+1]< aa[j]) {
var temp= aa[j];
aa[j]= aa[j+1];
aa[j+1]=temp;
await draw(aa,j,j+1);
await sleep(delay);
}
}
}await draw(aa,-1,-1);
}
async function bubblesortRev(aa) {
var n = aa.length;
for (var i = 0; i < n; i++) {
for (var j = 0; j < n - i - 1; j++) {
if (aa[j+1]> aa[j]) {
var temp= aa[j];
aa[j]= aa[j+1];
aa[j+1]=temp;
await draw(aa,j,j+1);
await sleep(delay);
}
}
}await draw(aa,-1,-1);
}
3.2 Selection Sort
async function
selectionsort(aa)
{
var n = aa.length;
for (var i = 0; i < n - 1; i++) {
var min_j = i;
for (var j = i; j < n; j++) {
if (aa[j] < aa[min_j])
min_j = j;
}
var temp= aa[i];
aa[i]= aa[min_j];
aa[min_j]=temp;
await draw(aa,i,min_j);
await sleep(delay);
}
await draw(aa,-1,-1);
}
async function selectionsortRev(aa) {
var n = aa.length;
for (var i = n-1; i > 0; i--) {
var min_j = i;
for (var j = 0; j<i; j++) {
if (aa[j] < aa[min_j])
min_j = j;
}
var temp= aa[i];
aa[i]= aa[min_j];
aa[min_j]=temp;
await draw(aa,i,min_j);
await sleep(delay);
}await draw(aa,-1,-1);
}
3.3 Insertion Sort
async function
insertionsort(aa)
{
var n = aa.length;
for (var i = 1; i < n; i++) {
for (var j = i; j > 0 && aa[j]< aa[j-1]; j--) {
var temp= aa[j];
aa[j]= aa[j-1];
aa[j-1]=temp;
await draw(aa,j,j-1);
await sleep(delay);
}
}
await draw(aa,-1,-1);
}
async function insertionsortRev(aa) {
var n = aa.length;
for (var i = 1; i < n; i++) {
for (var j = i; j > 0 && aa[j]> aa[j-1]; j--) {
var temp= aa[j];
aa[j]= aa[j-1];
aa[j-1]=temp;
await draw(aa,j,j-1);
await sleep(delay);
}
}
await draw(aa,-1,-1);
}
3.4 Merge Sort
async
function
mergesort(aa,
left, right)
{
if (typeof(left) === 'undefined') left = 0;
if (typeof(right) === 'undefined') right = aa.length - 1;
if (left >= right) return;
}
}
function perm_to_swaps(perm) {
/*
* Convert a permutation to a sequence of transpositions.
*
* We represent a general permutation as a list of length N
* where each element is an integer from 0 to N - 1, with the
* interpretation that the element at index i will move to index
* perm[i].
*
* In general any permutation can be written as a product of
* transpositions; we represent the transpostions as an array t of
* length-2 arrays, with the interpretation that we first swap
* t[0][0] with t[0][1], then swap t[1][0] with t[1][1], etc.
*
* Input: perm, a permutation
* Returns: transpositions: a list of transpositions.
*/
if (!check_perm(perm)) {
console.log(perm);
throw "Invalid permutation";
}
var n = perm.length;
var used = [];
for (var i = 0; i < n; i++) used.push(false);
var transpositions = [];
for (var i = 0; i < n; i++) {
if (used[i]) continue;
var cur = i;
if (perm[i] == i) used[i] = true;
while (!used[perm[cur]]) {
transpositions.push([cur, perm[cur]]);
used[cur] = true;
cur = perm[cur];
}
}
return transpositions;
}
function check_perm(perm) {
// Check to see if an array is a valid permutation.
var n = perm.length;
var used = {};
for (var i = 0; i < n; i++) {
if (used[perm[i]]) return false;
used[perm[i]] = true }
3.5 Quick Sort
async function
choose_pivot(aa,
pivot_type,
left, right) {
if (typeof(left) === 'undefined') left = 0;
if (typeof(right) === 'undefined') right = aa.length() - 1;
var pivot = null;
if (pivot_type === 'random') {
pivot = Math.floor(Math.random()*(right-left))+left;
}else {
throw 'Invalid pivot_type ' + pivot_type;
}
return pivot;
}
async function partition(aa, pivot_type, left, right) {
var pivot = choose_pivot(aa, pivot_type, left, right);
var temp= aa[pivot];
aa[pivot]= aa[right];
aa[right]=temp;
draw(aa,pivot,right);
sleep(delay);
// Partition the array around the pivot.
pivot = left;
for (var i = left; i < right; i++) {
if (aa.lessThan(i, right)) {
if (i != pivot) {
var temp= aa[pivot];
aa[pivot]= aa[i];
aa[i]=temp;
draw(aa,pivot,i);
sleep(delay);
}
pivot += 1;
}
}
var temp= aa[pivot];
aa[pivot]= aa[right];
aa[right]=temp;
await draw(aa,pivot,right);
await sleep(delay);
return pivot;
}
async function quicksort(aa, pivot_type, left, right) {
var n = aa.length;
if (typeof(left) === 'undefined') left = 0;
if (typeof(right) === 'undefined') right = n - 1;
if (left >= right) return;
var pivot = partition(aa, pivot_type, left, right);
quicksort(aa, pivot_type, left, pivot - 1);
quicksort(aa, pivot_type, pivot + 1, right);
}
3.6 Heap Sort
Async
function
heapsort(aa,
left, right)
{
if (typeof(left) === 'undefined') left = 0;
if (typeof(right) === 'undefined') right = aa.length - 1;
var n = right - left + 1;
async function sift_down(start, end) {
var root = start;
while (true) {
var left_child = 2 * (root - left) + 1 + left;
var right_child = 2 * (root - left) + 2 + left;
if (left_child > end) break;
var swap = root;
if (aa[swap]< aa[left_child]) {
swap = left_child;
}
if (right_child <= end && aa[swap]<aa[right_child]) {
swap = right_child;
}
if (swap === root) {
return;
}
var temp = aa[root];
aa[root]=aa[swap];
aa[swap]=temp;
// draw(aa,swap,root);
// sleep(delay);
root = swap;
}
}
// First build a heap
var start = Math.floor(n / 2) - 1 + left;
while (start >= left) {
sift_down(start, right);
start--;
}
// Now pop elements one by one, rebuilding the heap after each
var end = right;
while (end > left) {
var temp = aa[end];
aa[end]=aa[left];
aa[left]=temp;
await draw(aa,end,left);
await sleep(delay*2);
end--;
sift_down(left, end);
}
await draw(aa,-1,-1);
}
SRS and Use Case Diagram
Minimum Software Requirements:
1. Browser
2. Vs code
3. Github
1. Monitor
2. Hard disk
3. RAM
4. TFS
5.
Data Flow Diagram (DFD)
SRS dfds
USE
8. MODULES OF PROJECT
8.1 Introduction
This module basically tells about what we are going to show in this
project.
Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the
given array around the picked pivot. There are many different versions of quick Sort that
pick pivot in different ways.
Always pick first element as pivot.
Always pick last element as pivot (implemented below)
Pick a random element as pivot.
Pick median as pivot.
The key process in quick Sort is partition (). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x. All
this should be done in linear times.
8.5 Insertion sort
Insertion sort iterates, consuming one input element each repetition, and growing a sorted
output list. At each iteration, insertion sort removes one element from the input data, finds
the location it belongs within the sorted list, and inserts it there. It repeats until no input
elements remain.
Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it.
At each array-position, it checks the value there against the largest value in the sorted list
(which happens to be next to it, in the previous array-position checked). If larger, it leaves
the element in place and moves to the next. If smaller, it finds the correct position within the
sorted list, shifts all the larger values up to make a space, and inserts into that correct
position.
The resulting array after k iterations has the property where the first k + 1 entries are sorted
(+1 because the first entry is skipped). In each iteration the first remaining entry of the input
is removed, and inserted into the result at the correct position, thus extending the result.
8.6 Selection Sort
The algorithm divides the input list into two parts: the sub list of items already sorted, which is
built up from left to right at the front (left) of the list, and the sub list of items remaining to be
sorted that occupy the rest of the list. Initially, the sorted sub list is empty and the unsorted sub
list is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending
on sorting order) element in the unsorted sub list, exchanging (swapping) it with the leftmost
unsorted element (putting it in sorted order), and moving the sub list boundaries one element to
the right. Uniqueness of selection sort when compared to other sorting techniques
FEEDBACK FORM
BIBLIOGRAPHY
2. Other References :
2.1 https://github.com/
2.2 https://www.geeksforgeeks.org/