Reader Level:
ARTICLE

Sorting in TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript November 28, 2012
In this article I will explain how to sort an array using a bubble sort in TypeScript with an example.
  • 0
  • 0
  • 2793

Bubble Sort

Bubble Sort is an algorithm for sorting (arranging in ascending order or descending order) numbers. The Bubble Sort algorithm is very easy to understand. A Bubble Sort uses iteration; an array to be sorted from the first element to the last, comparing each element to adjacent elements and swapping their positions if necessary. This process is repeated as many times as necessary, until the array is sorted.

Example

The following example describes how to sort an array using Bubble Sort in TypeScript. The following code implements Bubble Sort to sort an array in ascending order. The following is the procedure for using Bubble Sort in TypeScript.

Step 1

Open Visual Studio 2012 and click on "File" menu -> "New" -> "Project". A window is opened. Provide the name of  your application like "Bubble_Sort", then click on the Ok button.

Step 2

After Step 1 your project has been created. Solution Explorer, which is at the right side of your project, contains the js file, ts file, css file and html files.

Step 3

Bubble_Sort.ts

class Bubble_Sort
 {
     sorting()
     {
         var num, x, y, swap;
         var array: number[] = [100];
         num = parseInt(prompt("Enter number of elements"));
         for (x = 0; x < num;x++)
         array[x]=parseInt(prompt("Enter "+num+ " integers"));
         for (var i = 0; i < num; i++)
          {
             var span = document.createElement("span");
             span.style.color = "Green";
             span.innerText = "Enter "+ i +" Element -> " + array[i]+ "\n";
             document.body.appendChild(span);
         }
         for (x = 0 ; x < ( num - 1 ); x++)
         {
             for (y = 0 ; y < num - x - 1; y++)
             {
                 if (array[y] > array[y+1])
                 {
                 swap       = array[y];
                 array[y]   = array[y+1];
                 array[y+1] = swap;
                 }
             }
         }
         var span = document.createElement("span");
         span.style.color = "Red";
         span.innerText = "\nSorted list in ascending order \n";
         document.body.appendChild(span);
       
         for (x = 0 ; x < num ; x++) {
         var span = document.createElement("span");
         span.style.color = "Blue";
         span.innerText = "Sorted element "+x+" -> "+array[x]+"\n";
         document.body.appendChild(span);
         } 
     }
 }
 
window.onload = () =>
 {
     var sort = new Bubble_Sort();
     sort.sorting();
 };


Bubble_SortDemo
.html

<!DOCTYPE html>
 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 
<head>
 
    <meta charset="utf-8" />
 
    <title>Bubble Sort in TypeScript</title>
 
    <link rel="stylesheet" href="app.css" type="text/css" />
 
    <script src="app.js"></script>
 
</head>
 <
body>
 
    <h2>Bubble Sort in TypeScript</h2>
 
    <div id="content"/>
 
</body>
 </
html>

app.js

var Bubble_Sort = (function () {
    function Bubble_Sort() { }
    Bubble_Sort.prototype.sorting = function () {
        var num;
        var x;
        var y;
        var swap;
        var array = [
            100
        ];
        num = parseInt(prompt("Enter number of elements"));
        for (x = 0; x < num; x++) {
            array[x] = parseInt(prompt("Enter " + num + " integers"));
        }
        for (var i = 0; i < num; i++) {
            var span = document.createElement("span");
            span.style.color = "Green";
            span.innerText = "Enter " + i + " Element -> " + array[i] + "\n";
            document.body.appendChild(span);
        }
        for (x = 0; x < (num - 1); x++) {
            for (y = 0; y < num - x - 1; y++) {
                if (array[y] > array[y + 1]) {
                    swap = array[y];
                    array[y] = array[y + 1];
                    array[y + 1] = swap;
                }
            }
        }
        var span = document.createElement("span");
        span.style.color = "Red";
        span.innerText = "\nSorted list in ascending order \n";
        document.body.appendChild(span);
        for (x = 0; x < num; x++) {
            var span = document.createElement("span");
            span.style.color = "Blue";
            span.innerText = "Sorted element " + x + " -> " + array[x] + "\n";
            document.body.appendChild(span);
        }
    };
    return Bubble_Sort;
})();
window.onload = function () {
    var sort = new Bubble_Sort();
    sort.sorting();
};


Output 1

Enter how many elements are in the array that you want and then click on ok, as in:


enter-element.jpg


Output 2


Enter the elements as in:


element-0.jpg


element-1.jpg


element-2.jpg


element-3.jpg


Output 3

 

final-result.jpg

COMMENT USING