Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.67 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3.   <head>
  4.  
  5.     <meta charset="utf-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  8.     <style>
  9.         body {
  10.             background-color: darkgrey;
  11.             color: white;
  12.         }
  13.  
  14.         .bar {
  15.             background-color: lime;
  16.             border: 1px solid #000000;
  17.             margin-left: 5px;
  18.             margin-right: 5px;
  19.             padding: 0px;
  20.             width: 20px;
  21.             text-align: center;
  22.             font-family: courier;
  23.             font-size: 12px;
  24.             color: #000000;
  25.         }
  26.  
  27.         #canvas {
  28.             border-bottom: 1px solid #000000;
  29.         }
  30.     </style>
  31.  
  32.     <title>JavaScript Fun</title>
  33.  
  34.   </head>
  35.   <body>
  36.  
  37.  
  38.     <div class="container">
  39.         <h2 class="text-center p-3">Sorter</h2>
  40.         <div id="canvas" class="d-flex justify-content-center mx-auto" style="width: 600px; height: 410px">
  41.             <!-- JS goes here -->
  42.         </div>
  43.         <div class="p-3 mx-auto text-center">
  44.             <button id="toggle_btn" onclick="sort()">Sort</button>
  45.             <button id="shuffle_btn" onclick="shuffle()">Shuffle</button>
  46.         </div>
  47.     </div>
  48.  
  49.     <!-- Bootstrap stuff -->
  50.     <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  51.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  52.     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  53.  
  54.     <script>
  55.         var heights = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
  56.         var start = false;
  57.         var matched;
  58.         draw_bars();
  59.  
  60.         function sort() {
  61.             do {
  62.             setInterval(bubble_sort, 500);
  63.             } while (matched);
  64.         }
  65.  
  66.         function draw_bars() {
  67.             var html_string = '';
  68.  
  69.             for (let i = 0; i < heights.length; i++) {
  70.                let height = heights[i] * 20 + 10;
  71.                html_string = html_string + '<div class="bar" style="height: ' + height + 'px; margin-top: '
  72.                            + (410-height) + 'px"><span class="num">' + (heights[i]) + '</span></div>';
  73.             }
  74.             $("#canvas").html(html_string);
  75.         }
  76.  
  77.         function shuffle() {
  78.             let counter = heights.length;
  79.  
  80.             while (counter > 0) {
  81.                 let index = Math.floor(Math.random() * counter);
  82.                 counter--;
  83.  
  84.                 let temp = heights[counter];
  85.                 heights[counter] = heights[index];
  86.                 heights[index] = temp;
  87.             }
  88.             draw_bars(heights);
  89.         }
  90.  
  91.         function bubble_sort() {
  92.                 matched = false;
  93.                 for (var i = 0; i < (heights.length - 1); i++) {
  94.                    if (heights[i] > heights[i+1]) {
  95.                         let temp = heights[i+1];
  96.                         heights[i+1] = heights[i];
  97.                         heights[i] = temp;
  98.                         matched = true;
  99.                     }
  100.                 }
  101.                 draw_bars();
  102.         }
  103.     </script>
  104.   </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement