javascript: Sorting Algorithms

javascript 复制代码
//  Sorting Algorithms int JavaScript https://www.geeksforgeeks.org/sorting-algorithms/
/**
* file Sort.js
* 1. Bubble Sort冒泡排序法
* @param arry
* @param nszie
*/
function BubbleSort(arry, nszie)
{
    var i, j, temp;
    var swapped;
    for (i = 0; i < nszie - 1; i++)
    {
        swapped = false;
        for (j = 0; j < nszie - i - 1; j++)
        {
            if (arry[j] > arry[j + 1])
            {
                // Swap arry[j] and arry[j+1]
                temp = arry[j];
                arry[j] = arry[j + 1];
                arry[j + 1] = temp;
                swapped = true;
            }
        }
 
        // IF no two elements were
        // swapped by inner loop, then break
        if (swapped == false)
        break;
    }
	
	return arry
}
/**
*
*/
function swap(arry,xp, yp)
{
    var temp = arry[xp];
    arry[xp] = arry[yp];
    arry[yp] = temp;
}
 /**
* 2 选择排序 Selection Sort
* @param arry
* @param nszie
*/
function SelectionSort(arry,  nsize)
{
    var i, j, min_idx;
 
    // One by one move boundary of unsorted subarray
    for (i = 0; i < nsize-1; i++)
    {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i + 1; j < nsize; j++)
        if (arry[j] < arry[min_idx])
            min_idx = j;
 
        // Swap the found minimum element with the first element
        swap(arry,min_idx, i);
    }
	return arry;
}
 /**
* 3 插入排序 Insertion Sort
* @param arry
* @param nszie
*/
function InsertionSort(arry, nsize) 
{ 
    let i, key, j; 
    for (i = 1; i < nsize; i++)
    { 
        key = arry[i]; 
        j = i - 1; 
   
        /* Move elements of arr[0..i-1], that are 
        greater than key, to one position ahead 
        of their current position */
        while (j >= 0 && arry[j] > key)
        { 
            arry[j + 1] = arry[j]; 
            j = j - 1; 
        } 
        arry[j + 1] = key; 
    }
	return arry;
} 
/**
*
*/
function stringArray(arry, size)
{
	
	var myStr=new Array();
  var i;
  for (i = 0; i < size; i++)
  {
       //getstr=getstr+arry[i].toString() + "<br/>";
	  myStr[i]=arry[i].toString();
	  console.log(arry[i].toString());	 
  }
   console.log(myStr.join("<br/>"));
	return myStr.join(" <br/>");
  //console.log(arry);
 /* var myStr=new Array();
  var getstr="";
  */
	/*
  var i;
  for (i = 0; i < size; i++)
  {
      getstr=getstr+arry[i] + " ";
	  myStr[i]=arry[i].toString();
	  console.log(myStr[i]);
  }	
  return getstr;//myStr.join(" ");*/
}


/**

*/
function printArray(arry, size)
{
  var getstr="";
  var i;
  for (i = 0; i < size; i++)
  {
      console.log(arry[i] + " ");
	  getstr=getstr+arry[i]+" ";
  }
  return getstr;
}

调用:

html 复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">	
<title>成长开始,geovindu,涂聚文,Geovin Du</title>
		<meta name="Description" content="geovindu"/>
<meta name="Keywords" content="geovindu"/>
<meta name="author" content="geovindu"/>
	<script  src="js/jquery-3.6.0.js"></script>
	<script src="js/SortAlgorithm/Sort.js"></script>
	<script type="text/javascript">

 $(document).ready(function () {
// 1. Bubble Sort冒泡排序法
var arry = [ 64, 34, 25, 112, 220, 11, 90 ];
var  nzie= arry.length;
var duselect=SelectionSort(arry,nzie);
console.log(duselect)
var geovindu=BubbleSort(arry, nzie);
console.log(geovindu);
var inserttdu=InsertionSort(arry,nzie);
var myStr=new Array();
var i;
  for (i = 0; i < nzie; i++)
  {
       //getstr=getstr+arry[i].toString() + "<br/>";
	  myStr[i]=geovindu[i].toString();
	  console.log(geovindu[i].toString());	 
  }
console.log(myStr.join("<br/>"));
var du=stringArray(arry,nzie);
var du2=stringArray(duselect,nzie);
var du3=stringArray(inserttdu,nzie);
	 
console.log(du);
console.log("Bubble Sorted array: ");
 var getstr=printArray(arry, nzie);
console.log("str:"+getstr)	 
$("#txtgeovindu").html(getstr);
txtgeovindu.innerHTML = getstr;//stringArray(geovindu,nsize);	 
$("#geovindu").html("1.泡冒泡排序Bubble Sorted:<br/>"+myStr.join("<br/>"));
$("#geovindu2").html(du);
$("#geovindu3").html("2.选择排序Selection Sorted:<br/>"+du2);
$("#geovindu4").html("3.插入排序Insertion Sorted:<br/>"+du3);
 });
	</script>
</head>

<body>
<textarea id="txtgeovindu" class="geovindu" name="" cols="30" rows="10"></textarea>
<div id="geovindu"></div>
<div id="geovindu2"></div>
<div id="geovindu3"></div>
<div id="geovindu4"></div>	
</body>
</html>
相关推荐
_Legend_King几秒前
vue3 + elementPlus 日期时间选择器禁用未来及过去时间
javascript·vue.js·elementui
余生H2 分钟前
transformer.js(三):底层架构及性能优化指南
javascript·深度学习·架构·transformer
凡人的AI工具箱5 分钟前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
景天科技苑15 分钟前
【vue3+vite】新一代vue脚手架工具vite,助力前端开发更快捷更高效
前端·javascript·vue.js·vite·vue项目·脚手架工具
chnming198721 分钟前
STL关联式容器之map
开发语言·c++
进击的六角龙22 分钟前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂23 分钟前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python
石小石Orz24 分钟前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
小行星12526 分钟前
前端预览pdf文件流
前端·javascript·vue.js
join828 分钟前
解决vue-pdf的签章不显示问题
javascript·vue.js·pdf