Tuesday, 24 April 2018

Sorting

1. Selection sort:

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;   

function myFunction() {//Bubble sort works by repeatedly swapping the adjecent  elements if they are in wrong order.
let len=points.length;
for(let i=0; i<len-1; i++){
        for(let j=0; j<len-i-1; j++){
        if(points[j+1]<points[j]){
            let temp=points[j];
                points[j]=points[j+1];
                points[j+1]=temp;
            }
          }
      }   
    //points.sort(function(a, b){return a - b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

For Edit You can Use below Link:-

https://jsfiddle.net/OmPrakashPandey/Lpw43xyt/



2. Bubble Sort

Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items

We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is completely sorted in an ascending order. This may cause a few complexity issues like what if the array needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of the loop.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>Click the button to sort the array in ascending order.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points; 

function myFunction() {//Bubble sort works by repeatedly swapping the adjecent  elements if they are in wrong order.
let len=points.length;
for(let i=0; i<len; i++){
        for(let j=0; j<len-i; j++){
        if(points[j+1]<points[j]){
            let temp=points[j];
                points[j]=points[j+1];
                points[j+1]=temp;
            }
          }
      }   
    //points.sort(function(a, b){return a - b});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>




For Edit You can Use below Link:-

https://jsfiddle.net/OmPrakashPandey/cfgrt2of//



No comments:

Post a Comment