Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function swap(items, leftIndex, rightIndex){
  3.     var temp = items[leftIndex];
  4.     items[leftIndex] = items[rightIndex];
  5.     items[rightIndex] = temp;
  6.     // addTable();
  7.     console.log(items.toString())
  8. }
  9. function partition(items, left, right) {
  10.     var pivot = items[Math.floor((right + left) / 2)], //middle elemen
  11.         i = left, //left pointer
  12.         j = right; //right pointer
  13.     console.log(pivot)
  14.     // addTable();
  15.     console.log(items.toString());
  16.     while (i <= j) {
  17.         while (items[i] < pivot) {
  18.             i++;       //2
  19.         }
  20.         while (items[j] > pivot) {
  21.             j--;  //4
  22.         }
  23.         if (i <= j) {
  24.             swap(items, i, j); //sawpping two elements
  25.             i++;
  26.             j--;
  27.         }
  28.         // console.log(items)
  29.     }
  30.     return i;
  31. }
  32.  
  33. function addTable() {
  34.     let body = document.querySelector("body"),
  35.         tableWidth = document.getElementById("table-width"),
  36.         tableHeight = document.getElementById("table-height"),
  37.         width = tableWidth.value,
  38.         height = tableHeight.value,
  39.         numRows = document.getElementById("rows"),
  40.         numColumns = document.getElementById("columns"),
  41.         rows = numRows.value,
  42.         columns = numColumns.value,
  43.         tr = "",
  44.         td = "",
  45.         firstTable = document.querySelector("table");
  46.     console.log("ширина: ",width);
  47.     console.log("высота: ",height);
  48.     console.log("кол-во строк: ",rows);
  49.     console.log("кол-во стобцов: ",columns);
  50.  
  51.     table = document.createElement("table"),
  52.         checkbox = document.getElementById("checkbox");
  53.     if (checkbox.checked == true) {
  54.         table.setAttribute("border", "2px");
  55.     } else {
  56.         table.setAttribute("border", "0");
  57.     }
  58.     table.setAttribute("width", width);
  59.     table.setAttribute("height", height);
  60.     let text;
  61.     for (let i = 0; i < 15; i++) {
  62.         tr = document.createElement("tr");
  63.         for (let j = 0; j < 15; j++) {
  64.             td = document.createElement("td");
  65.             // text = document.createTextNode((i + 1) + "." + (j + 1));
  66.             // let number = Math.random() * 100;
  67.             text = document.createTextNode(items[j]);
  68.             td.appendChild(text);
  69.             tr.appendChild(td);
  70.         }
  71.         table.appendChild(tr);
  72.     }
  73.     console.log(tr);
  74.     console.log(td);
  75.     if (firstTable == null) {
  76.         return body.appendChild(table);
  77.     } else {
  78.         var newTable = body.appendChild(table);
  79.         return document.body.replaceChild(newTable, firstTable);
  80.     }}
  81. function quickSort(items, left, right) {
  82.     var index;
  83.     if (items.length > 1) {
  84.         index = partition(items, left, right); //index returned from partition
  85.         if (left < index - 1) { //more elements on the left side of the pivot
  86.             quickSort(items, left, index - 1);
  87.         }
  88.         if (index < right) { //more elements on the right side of the pivot
  89.             quickSort(items, index, right);
  90.         }
  91.         // console.log("ee",items)
  92.     }
  93.     return items;
  94. }
  95. // first call to quick sort
  96. let items = [];
  97. for (let i = 0;i < 15;i++){
  98.     items[i] = Math.floor(Math.random()*100);
  99. }
  100. var sortedArray = quickSort(items, 0, items.length - 1);
  101. console.log(sortedArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement