Sort Array without sort function in javascript

–JS File

r =[3,4,5,1];

function isSorted(r){
	for (x=0;x<r.length-1;x++){ if (r[x] > r[x+1]){ return false;} 	
	}
	return true;
}


while (!isSorted(r)){
	for (x=0;x<r.length-1;x++){ if (r[x] > r[x+1]){
		  rx=r[x];
		  r[x]=r[x+1];
		  r[x+1]=rx;	
	  }	
	}
}


console.log(r);

Leave a Reply

Your email address will not be published. Required fields are marked *