Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function swap(items, leftIndex, rightIndex){
- var temp = items[leftIndex];
- items[leftIndex] = items[rightIndex];
- items[rightIndex] = temp;
- // addTable();
- console.log(items.toString())
- }
- function partition(items, left, right) {
- var pivot = items[Math.floor((right + left) / 2)], //middle elemen
- i = left, //left pointer
- j = right; //right pointer
- console.log(pivot)
- // addTable();
- console.log(items.toString());
- while (i <= j) {
- while (items[i] < pivot) {
- i++; //2
- }
- while (items[j] > pivot) {
- j--; //4
- }
- if (i <= j) {
- swap(items, i, j); //sawpping two elements
- i++;
- j--;
- }
- // console.log(items)
- }
- return i;
- }
- function addTable() {
- let body = document.querySelector("body"),
- tableWidth = document.getElementById("table-width"),
- tableHeight = document.getElementById("table-height"),
- width = tableWidth.value,
- height = tableHeight.value,
- numRows = document.getElementById("rows"),
- numColumns = document.getElementById("columns"),
- rows = numRows.value,
- columns = numColumns.value,
- tr = "",
- td = "",
- firstTable = document.querySelector("table");
- console.log("ширина: ",width);
- console.log("высота: ",height);
- console.log("кол-во строк: ",rows);
- console.log("кол-во стобцов: ",columns);
- table = document.createElement("table"),
- checkbox = document.getElementById("checkbox");
- if (checkbox.checked == true) {
- table.setAttribute("border", "2px");
- } else {
- table.setAttribute("border", "0");
- }
- table.setAttribute("width", width);
- table.setAttribute("height", height);
- let text;
- for (let i = 0; i < 15; i++) {
- tr = document.createElement("tr");
- for (let j = 0; j < 15; j++) {
- td = document.createElement("td");
- // text = document.createTextNode((i + 1) + "." + (j + 1));
- // let number = Math.random() * 100;
- text = document.createTextNode(items[j]);
- td.appendChild(text);
- tr.appendChild(td);
- }
- table.appendChild(tr);
- }
- console.log(tr);
- console.log(td);
- if (firstTable == null) {
- return body.appendChild(table);
- } else {
- var newTable = body.appendChild(table);
- return document.body.replaceChild(newTable, firstTable);
- }}
- function quickSort(items, left, right) {
- var index;
- if (items.length > 1) {
- index = partition(items, left, right); //index returned from partition
- if (left < index - 1) { //more elements on the left side of the pivot
- quickSort(items, left, index - 1);
- }
- if (index < right) { //more elements on the right side of the pivot
- quickSort(items, index, right);
- }
- // console.log("ee",items)
- }
- return items;
- }
- // first call to quick sort
- let items = [];
- for (let i = 0;i < 15;i++){
- items[i] = Math.floor(Math.random()*100);
- }
- var sortedArray = quickSort(items, 0, items.length - 1);
- console.log(sortedArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement