Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mini Project Report: Visual Applications of Sorting Algorithms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Mini Project Report

On

VISUAL APPLICATIONS OF
SORTING ALGORITHMS

AJAY KUMAR GARG ENGINEERING COLLEGE,


GHAZIABAD

(B. Tech Computer Science Sem III, 2019-20)


(KCS- 354 Mini project report or Internship Assessment Report)

SUBMITTED TO: SUBMITTED BY:


MS. ARTI PANDEY
MS. PRIYANKA GABA AMAN SRIVASTAVA
(1802710011)
AMISHA
(1802710012)
Acknowledgement

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

2. Problem Statement and Description

3. SRS with USE CASE Diagram

4. Code

5. Dataflow diagram

6. Minimum Software Required

7. Minimum Hardware Required

8. Module of Project and Snapshot

9. Conclusion

10. Bibliography
1. INTRODUCTION

Sorting is a very classic technique of reordering items (that can be


Compared, e.g. integers, floating -point numbers, strings, etc) of an
array (or a list) in a certain order.
Another way of looking at this project is an analytical tool to study how
sorting algorithm work .Beside generating visual representation, sorting
provides a walk- through that guides the reader step after step along the
processes of ordering a list of integer numbers.
Sorting was born to create visual representations of sorting algorithms
with the hope of finding visual patterns. It turned out that the visual
footprints of algorithms are unique and differ from each other.
In this project we are going to show how the pictorial visualisation of
the various sorting algorithms, their working and mechanism and their
comparison and swapping operations.
2. PROBLEM STATEMENT AND DESCRIPTION

1. Numbers of inputs are given user can sort them by different sorting
algorithms given in this project;

2. Basically it serves as an analytical tool for studying how sorting


algorithm works by generating their visual applications.

3. One can modify the number of inputs, and order in which they are
arranged.

4. User can modify the speed of algo presentation by modifying delay


time.

5. We have taken six basic sorting algorithms:-

a) Bubble sort
b) Selection sort
c) Insertion sort
d) Quick sort
e) Heap sort
f) Merge sort
3. CODES

3.1 Bubble Sort

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;

var mid = Math.floor((left + right) / 2);


if (right - left > 1) {
mergesort(aa, left, mid);
mergesort(aa, mid + 1, right);
}
// Merge, building up a permutation. This could probably be prettier.
var next_left = left;
var next_right = mid + 1;
var perm = [];
for (var i = left; i <= right; i++) {
var choice = null;
if (next_left <= mid && next_right <= right) {
if (aa[next_left] < aa[next_right]) {
choice = 'L';
} else {
choice = 'R';
}
} else if (next_left > mid) {
choice = 'R';
} else if (next_right > right) {
choice = 'L';
}
if (choice === 'L') {
perm.push(next_left - left);
next_left++;
} else if (choice === 'R') {
perm.push(next_right - left);
next_right++;
} else {
throw 'Should not get here'
}
}
var swaps = perm_to_swaps(perm);
for (var i = 0; i < swaps.length; i++) {
var j=swaps[i][0] + left;
var k= swaps[i][1] + left
var temp=aa[j];
aa[j]=aa[k];
aa[k]=temp;
console.log(j,k);
await draw(aa,j,k);
await sleep(delay);

}
}
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

Minimum Hardware Requirement

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.

8.2 To Enter the details


This module is used to select the sorting algorithm, the size of array
and the delay time that the user wants to perform.

8.3 Bubble Sort


This module shows how the bubble sort algorithm works when user enters size of aan
array it goes like above. Bubble sort, sometimes referred to as sinking sort, is a simple
sorting algorithm that repeatedly steps through the list, compares adjacent elements and
swaps them if they are in the wrong order. The pass through the list is repeated until the
list is sorted. The algorithm, which is a comparison sort, is named for the way smaller or
larger elements bubble to the top of the list.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps
since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
8.4 Quick Sort

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

8.7 Merge sort


Like Quick Sort, Merge Sort is a Divide and Conquer algorithm. It divides input
array in two halves, calls itself for the two halves and then merges the two sorted
halves. The merge () function is used for merging two halves. The merge (arr, l,
m, r) is key process that assumes that arr[l..m] and arr [m+1..r] are sorted and
merges the two sorted sub-arrays into one.
CONCLUSION
This project serves as an analytical tool for studying how sorting algorithm works by
generating their visual applications. Efficient sorting is important for optimizing
the efficiency of other algorithms such as merge sort algorithms that require input data to be
in sorted lists. Sorting is also often useful for canonical zing data and for producing human-
readable output.

FEEDBACK FORM
BIBLIOGRAPHY

1. BOOK : Web programming using HTML/CSS


Author: John Dean

2. Other References :
2.1 https://github.com/
2.2 https://www.geeksforgeeks.org/

You might also like