在2d js数组上执行排序时遇到问题,请帮我运行此代码并帮我纠正错误。
排序算法在一维数组中正确排序数据,但在二维数组中返回错误的结果。
如果你能提供帮助,我们会很高兴的!
由于
<script type="text/javascript">
var values = new Array(102,2,35,8,8,0);
var rows=3, columns=2, d=0;
var receipts = new Array();
for(var r=0; r<rows; r++){
receipts[r] = new Array();
for(var c=0; c<columns; c++){
receipts[r][c] = values[d++];
document.write(receipts[r][c] + ", ");
}
}
/*
SORT RECEIVED DATA
Using Bubble sort
*/
function bubbleSort(arrayData)
{
document.write("<h3> Bubble Sort</h3>");
document.write("At the end of first iteration highest element is sorted <br />");
//Determine execution time
var start_time = performance.now();
//Determine and store the number of elements in array
var arraySize = arrayData.length;
document.write(arraySize);
//Loop through to sort
for ( var count = arraySize - 1; count>=0; count--)// outer loop loops (number of elements- 1) times
{
for ( var counter = 1; counter <= count; counter++)
{
for(var c=0; c<=count; c++ ){
//Compare elements and swap incase
//if(arrayData[counter-1] > arrayData[counter])
if(arrayData[count][c] > arrayData[count][c+1])
{
//swap positions
var temp = arrayData[count][c];
arrayData[count][c] = arrayData[count][c+1];
arrayData[count][c+1] = temp;
}
}
}
}
var c = arrayData.length;
//diplay sorted list
document.write("<h5>Sorted array with bubble Sort: </h5>");
/* for(count=0; count<arraySize; ++count){
document.write(arrayData[count] + ", ");
c++;
}*/
//display sorted data
document.write(arrayData);
//Calculate execution time
var end_time = performance.now();
var execution_time = end_time - start_time;
document.write("<br><h3>" + c + " Sorted successfully using bubble sort. In "+ execution_time + " milliseconds </h3><hr>");
return arrayData;
}
//pass data to sort to bubble sort function
bubbleSort(receipts);
/*
HEAP SORT ALGORITHM
*/
document.write("<h3> Heap Sort</h3>");
document.write("Divide data into 3 heaps and sorts.<br/>");
function heap_sort(arrayToSort){
var array_size = arrayToSort.length, end = array_size-1;
heapify(arrayToSort, array_size);
while(end > 0){
swap(arrayToSort, end--, 0);
siftDown(arrayToSort, 0, end);
}
return arrayToSort;
}
function heapify(arrayToSort, arraySize){
//create 3 heaps: root and 2 sides
var middle = Math.floor((arraySize-2)/2);
while(middle >= 0){
siftDown(arrayToSort, middle--, arraySize-1);
}
}
function siftDown(arrayToSort,start, end){
var root = start, child = root*2+1, to_swap = root;
while(child <=end){
if(arrayToSort[to_swap]<arrayToSort[child]){
swap(arrayToSort, to_swap, child);
}
if(child+1 <= end && arrayToSort[to_swap] < arrayToSort[child+1]){
swap(arrayToSort, to_swap, child+1);
}
if(to_swap != root){
swap(arrayToSort, root, to_swap);
root = to_swap;
}else{
return;
}
to_swap = root;
child = root*2+1;
}
}
//swap function
function swap(arrayToSort,a,b){
var temp = arrayToSort[a];
arrayToSort[a] = arrayToSort[b];
arrayToSort[b] = temp;
}
//Execution time
var start_time = performance.now();
//call heap sort function
var heap_sorted_array = heap_sort(receipts.slice());
document.write(heap_sorted_array);
var end_time = performance.now();
var execution_time = end_time - start_time;
document.write("<br><h3>" + heap_sorted_array.length+ " Sorted successfully using heap sort. In "+ execution_time + " milliseconds </h3><hr>");
</script>