css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas id="canvas" width="800" height="400" ></canvas>

<div id="sort">
	<button id="selection" onclick="selectionSort(Arr)">Selection Sort</button>
	<button id="insertion" onclick="insertionSort(Arr)">Insertion Sort</button>
	<button id="bubbel" onclick="bubbelSort(Arr)">Bubbel Sort</button>

	<button id="quick" onclick="quickSort(Arr)">Quick Sort</button>
	<button id="merge" onclick="mergeSort(Arr)">Merge Sort</button>
	<button id="heap" onclick="heapSort(Arr)">Heap Sort</button>
	<button id="shaker" onclick="cocktailSort(Arr)">Cocktail Shaker Sort</button>
	<br>
	<button id="reverse" onclick="reverseArr(Arr)">Reverse Array</button>
	<button id="randomize" onclick="randomArr()">Randomize Array</button>
</div>

<div id="rgn">
		<p>Dealy Time &nbsp&nbsp&nbsp&nbsp:</p><input type="range" id="rangeTime" min="1" max="200" value="20"><span id="showTime">20</span><br>
		<p>Array Length :</p><input type="range" id="rangeArr" min="20" max="200" value="100"><span id="showLength">100</span>
</div>
            
          
!
            
              	@import url(https://fonts.googleapis.com/css?family=Patrick+Hand+SC);

	body,html{
		width: 100%;
		height: 100%;
		margin: 0 auto;
	}

	*{
		font-family: 'Patrick Hand SC', cursive;
	}

	#canvas{
		display: inline-block;
		position: relative;
		top: 10px;
		left:50px;
		border: solid 2px #000;
		outline: solid 10px #eeeeee;
		box-shadow: 0px 0px 35px 10px #000;
		background-color: #fff;
	}

	#sort{
		display: inline-block;
		position: relative;
		width: 300px;
		height: 500px;
		border: solid #000 10px;
		border-image: repeating-linear-gradient(45deg,gray,gray 1%,#2c2c2c, #2c2c2c 1%, #2c2c2c 8%)10;
		left: 100px;
		top: 10px;
	}

	#sort button{
		margin:10px;
		width: 120px;
		padding: 10px;
		background: rgba(0,0,0,0);
		border-style:offset;
		border-color: #424242;
		border-width: 5px;
		font-size: 20px;
		font-weight: 650;
		cursor: pointer;
		outline: none;
		user-select: none;
		transition: .2s;
	}

	#sort button:hover{
		transform: scale(1.1);
		transition: .2s;
	}


	#rgn{
		width: auto;
		position: relative;
		left:50px;
		
	}

	#rgn p{
		margin: 0px;
		padding: 8px;
		font-size: 25px;
		font-weight: 600;
	}

	#rgn p,#rgn input{
		display:inline-block;
	}

	#rgn input[type=range]{
		-webkit-appearance: none;  
    	appearance: none;
    	margin-left: 1em;
    	width: 200px;
    	height: 15px;
    	background: #d3d3d3;
    	border-radius: 30px;
    	outline: none;
	}

	#rgn input[type=range]::-webkit-slider-thumb {
    	-webkit-appearance: none; 
    	appearance: none;
    	width: 23px; 
    	height: 23px; 
    	border-radius: 50%;
    	background: #424242; 
    	cursor: pointer;
    	transition: .2s; 
		}
	#rgn input[type=range]::-webkit-slider-thumb:hover {
	    width: 27px; 
    	height: 27px;
    	transition: .2s;
	}

	#rgn input[type=range]::-moz-range-thumb {

    	width: 23px; 
    	height: 23px; 
    	border-radius: 50%; 
    	background: #424242; 
    	cursor: pointer; 
    	transition: .2s;
		
	}

@media screen and (max-width: 1200px){
	#canvas{
		width: 500px;
		height: 250px;
		position: static;
		margin: 0 auto;
		margin-top: 30px;
		margin-left:50%;
		transform: translateX(-50%);
	}
	
	#sort{
		margin-top:50px;
		margin-left:50%;
		width: 430px;
		height:auto;
		position:static;
		transform: translateX(-50%);	
	}
	
	#rgn{
		position:static;
		transform: translateX(-60%);
		margin-left:50%;
	}
}
            
          
!
            
              	var Arr = [];
	var len = Arr.length;
	var time = 20;

	var canvas = document.getElementById('canvas');
	var ctx = canvas.getContext("2d");

	var rangeTime = document.getElementById("rangeTime");
	var rangeArr = document.getElementById("rangeArr");	

	var showTime = document.getElementById("showTime");//use in range
	var showLength = document.getElementById("showLength");//use in range

	createArr(100)
	randomArr()
	drawArr(Arr);

	function createArr(number){
		Arr = [];
		for(var i=1; i<=number; i++){
			Arr.push(i);
		}
		len = Arr.length;
		return Arr;
	}
	function randomArr(){
		var index = len, random;
		while(0!==index){
			random = Math.floor(Math.random() * index);
			index -= 1;
			swapIndex(Arr,index,random);
			}
		drawArr(Arr);
		return Arr;
	}
	function swapIndex(arr,i,j){
		var temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
		return arr;
	}
	function drawArr(arr){
		var x = 0;
		var wid = canvas.width/len;
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		for (var i = 0; i < len; i++) {
			ctx.beginPath();
			ctx.fillStyle = "#424242";
			ctx.fillRect(x,(canvas.height)-arr[i]*(wid/2),wid, (canvas.height));
			ctx.closePath();
			x += wid;
		}
	}
	function sleep(ms){
		return new Promise(resolve =>setTimeout(resolve,ms));	
	}

	// using of "async" to use "await"
	async function bubbelSort(Arr){
			for (var i = len-1; i>=0; i--){
     			for(var j = 1; j<=i; j++){
       				if(Arr[j-1]>Arr[j]){
           			swapIndex(Arr,j,j-1);// Animation Performs when swaping is done in array;
         			await drawArr(Arr);	//drawArr() animate the array
        				await sleep(time);		//await sleep() pause the function (act as a dealy time)
        			}
     			}
   			}
	} 	

	async function selectionSort(arr){
		var minIdx;
  		for(var i = 0; i < len; i++){
    		minIdx = i;
    		for(var  j = i+1; j<len; j++){
      			if(arr[j]<arr[minIdx]){
       				minIdx = j;
       			}
    		}
    		await swapIndex(arr,i,minIdx);
    		await drawArr(arr);
    		await sleep(time)
  		}
  		return arr;
	}

	async function insertionSort(arr){
	  var i, el, j;

	  for(i = 1; i<len; i++){
		    el = arr[i];
		    j = i;

		    while(j>0 && arr[j-1]>el){
			      arr[j] = arr[j-1];
			      await drawArr(arr);
			      await sleep(10);
			      j--;
			   }
			   arr[j] = el;
			   	await drawArr(arr);
			    await sleep(time);
			  }

		  return arr;
	}

	async function quickSort(arr){
		await QS(0,len-1);
		async function partition(pivot,left,right){
			const pivotValue = arr[pivot];
			var	partitionIndex = left;
			var i = left;

			for(var i = left; i < right; i++){
			   	if(arr[i] < pivotValue){
			    	 	await swapIndex(arr, i, partitionIndex);
			    	 	await drawArr(arr);
						await sleep(time);
			      		partitionIndex++;
			    	}
			  }
			await swapIndex(arr, right, partitionIndex);
			await drawArr(arr);
			await sleep(time);
			return Promise.resolve(partitionIndex);
		}
		async function QS(left,right){
			if(left < right){
				const pivot = right;
				const partitionIndex =  await partition(pivot,left,right);
				await QS(left,partitionIndex-1);
				await QS(partitionIndex+1,right);
				}
			return Promise.resolve();	
		}
	}

	function mergeSort(Arr){
		inPlaceSort(Arr,0,len-1)

		async function inPlaceSort(x, first, last){

			var left, mid, right, temp;

			if(first>=last)	return Promise.resolve();

			mid = Math.floor((first+last)/2);

			await inPlaceSort(x,first,mid);
			await inPlaceSort(x,mid+1,last);

			left = first;
			right = mid+1;

				if(x[mid] <= x[right])	return;
				
				while(left <= mid && right <= last){
					if(x[left] <= x[right]){
						left++;
					}
					else{
						temp = x[right];
						await moveArr(x,left,right);
						x[left] = temp;
						await drawArr(x);
						await sleep(time);
	     				left++; right++; mid++;
						}
					}
					return Promise.resolve();
				}

		function moveArr(x,left,right){ 
			var tempArr = [];
			for(var i=0, j=left; i<(right-left); i++,j++){
					tempArr[i] = x[j];
			}

			for(var i=0, j=left+1; i<(right-left); i++,j++){
					x[j] = tempArr[i];
			}
			return Promise.resolve(x);
		}
	}

	function heapSort(arr){

		async function max_heapify(a, i, length) {
    		while (true) {
    		    var left = i*2 + 1;
    		    var right = i*2 + 2;
    		    var largest = i;

        		if (left < length && a[left] > a[largest]) {
           			largest = left;
        		}

        		if (right < length && a[right] > a[largest]) {
        	    	largest = right;
        		}

        		if (i == largest) {
        		    break;
        		}

        		await swapIndex(a, i, largest);
        		await drawArr(arr);
        		await sleep(time)
        		i = largest;
 		   	}
		}

		async function heapify(a, length) {
		    for (var i = Math.floor(length/2); i >= 0; i--) {
		        await max_heapify(a, i, length);
		    }
		}

		async function heapsort(a) {
    		await heapify(a, a.length-1);

    		for (var i = a.length - 1; i >= 0; i--) {
    	   	 	await swapIndex(a, i, 0);
        		await drawArr(arr);
        		await sleep(time)
        		await max_heapify(a, 0, i);
    		}	
		}

		heapsort(Arr);
	}

	async function cocktailSort(arr){
		var i, left=0, right = len-1, temp;
		while(left<right){
			for(i=left; i<right; i++){
				if(arr[i] > arr[i+1]){
					await swapIndex(arr,i,i+1);
					await drawArr(arr);
					await sleep(time)
				}
			}
			right--;
			for(i=right; i>left; i--){
				if(arr[i-1] > arr[i]){
					await swapIndex(arr,i-1,i);	
					await drawArr(arr);
					await sleep(time);
				}
			}
			left++;
		}
	}

	function reverseArr(arr){
			var maxIdx;
			const t = 0 ,
      			  len = arr.length;
  			for(var i = 0; i < len; i++){
    			maxIdx = i;
    			for(var  j = i+1; j<len; j++){
       				if(arr[j]>arr[maxIdx]){
          				maxIdx = j;
       				}
    			}
    			swapIndex(arr,i,maxIdx);
  			}
  			drawArr(arr);
  			return arr;
  	}

	rangeTime.oninput = function(){
		time = rangeTime.value;
		showTime.innerHTML = time;
	}

	rangeArr.oninput = function(){
		createArr(rangeArr.value);
		showLength.innerHTML = rangeArr.value;
		randomArr();
		drawArr(Arr);
	}


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console