hamecoded
2/23/2013 - 11:06 PM

QuickSort algorithm in JavaScript : Accompanies the YouTube video "Visual Demonstration of the QuickSort Algorithm" : htt

QuickSort algorithm in JavaScript : Accompanies the YouTube video "Visual Demonstration of the QuickSort Algorithm" : http://www.youtube.com/watch?v=Z5nSXTnD1I4

/**
 * Accompanies the video "Visual Demonstration of the QuickSort Algorithm"
 * http://www.youtube.com/watch?v=Z5nSXTnD1I4
 **/

/**
 * a recursive function, each time it positions the left most element in its correct index,
 * then it recurse its left and right sides
 **/
function quicksort(a){
	if(a.length < 2) return a;
	console.log("call: " + a.toLocaleString());
	//position first element
	var lf = 0,
		rt = a.length -1,
		lf_pivot = true;  //the pivot is on the left pointer

	while(lf != rt ){
		console.log(">> "  + a.join() + "=>  pointers: left@" + lf + " right@" + rt );
		if( a[lf] > a[rt] ){
			lf_pivot = !lf_pivot;
			//swap
			var tmp = a[lf];
			a[lf] = a[rt];
			a[rt] = tmp;

			lf_pivot ? rt-- : lf++;				

		}else{
			lf_pivot ? rt-- : lf++;				
		}
	}
	console.log(">> " + a.join() + "=>  pointers collision @ " + rt );
	if(a.length == 2) {
		return a;
	}else{
		return [].concat( quicksort(a.slice(0,rt)),
					a[rt],
					quicksort( a.slice(rt+1) )  //out of bound index will return an empty array
				 );
	}
}

console.log( "output: " + quicksort([4,8,1,6,3,7,2,5]) );


/**
call: 4,8,1,6,3,7,2,5 
>> 4,8,1,6,3,7,2,5=>  pointers: left@0 right@7 quicksort.js:18
>> 4,8,1,6,3,7,2,5=>  pointers: left@0 right@6 quicksort.js:18
>> 2,8,1,6,3,7,4,5=>  pointers: left@1 right@6 quicksort.js:18
>> 2,4,1,6,3,7,8,5=>  pointers: left@1 right@5 quicksort.js:18
>> 2,4,1,6,3,7,8,5=>  pointers: left@1 right@4 quicksort.js:18
>> 2,3,1,6,4,7,8,5=>  pointers: left@2 right@4 quicksort.js:18
>> 2,3,1,6,4,7,8,5=>  pointers: left@3 right@4 quicksort.js:18
>> 2,3,1,4,6,7,8,5=>  pointers collision @ 3 quicksort.js:32
call: 2,3,1 quicksort.js:11
>> 2,3,1=>  pointers: left@0 right@2 quicksort.js:18
>> 1,3,2=>  pointers: left@1 right@2 quicksort.js:18
>> 1,2,3=>  pointers collision @ 1 quicksort.js:32
call: 6,7,8,5 quicksort.js:11
>> 6,7,8,5=>  pointers: left@0 right@3 quicksort.js:18
>> 5,7,8,6=>  pointers: left@1 right@3 quicksort.js:18
>> 5,6,8,7=>  pointers: left@1 right@2 quicksort.js:18
>> 5,6,8,7=>  pointers collision @ 1 quicksort.js:32
call: 8,7 quicksort.js:11
>> 8,7=>  pointers: left@0 right@1 quicksort.js:18
>> 7,8=>  pointers collision @ 1 quicksort.js:32
output: 1,2,3,4,5,6,7,8 
**/
/**
 * Accompanies the video "Visual Demonstration of the QuickSort Algorithm"
 * http://www.youtube.com/watch?v=Z5nSXTnD1I4
 **/
function profileQuickSort(a){
	var comparisons = 0;
	var swaps = 0;
	var func_calls = 0;
	
	/**
	 * a recursive function, each time it positions the left most element in its correct index,
	 * then it recurse its left and right sides
	 **/
	function quicksort(a){
		++func_calls;
		if(a.length < 2) return a;
		console.log("call: " + a.toLocaleString());
		//position first element
		var lf = 0,
			rt = a.length -1,
			lf_pivot = true;  //the pivot is on the left pointer

		while(lf != rt ){
			console.log(">> "  + a.join() + "=>  pointers: left@" + lf + " right@" + rt );
			++comparisons;
			if( a[lf] > a[rt] ){
				lf_pivot = !lf_pivot;
				//swap
				var tmp = a[lf];
				a[lf] = a[rt];
				a[rt] = tmp;
				++swaps;
			}
			lf_pivot ? rt-- : lf++;				
		}
		console.log(">> " + a.join() + "=>  pointers collision @ " + rt );
		if(a.length == 2) {
			return a;
		}else{
			return [].concat( quicksort(a.slice(0,rt)),
						a[rt],
						quicksort( a.slice(rt+1) )  //out of bound index will return an empty array
					 );
		}
	}//end quicksort

	console.log( "output: " + quicksort(a) );

	console.log( "array length: " + a.length +
		", comparisons: " + comparisons +
		", swaps: " + swaps +
		", function calls: " + func_calls);
}

profileQuickSort([4,8,1,6,3,7,2,5]);

/**
call: 4,8,1,6,3,7,2,5
>> 4,8,1,6,3,7,2,5=>  pointers: left@0 right@7
>> 4,8,1,6,3,7,2,5=>  pointers: left@0 right@6
>> 2,8,1,6,3,7,4,5=>  pointers: left@1 right@6
>> 2,4,1,6,3,7,8,5=>  pointers: left@1 right@5
>> 2,4,1,6,3,7,8,5=>  pointers: left@1 right@4
>> 2,3,1,6,4,7,8,5=>  pointers: left@2 right@4
>> 2,3,1,6,4,7,8,5=>  pointers: left@3 right@4
>> 2,3,1,4,6,7,8,5=>  pointers collision @ 3
call: 2,3,1
>> 2,3,1=>  pointers: left@0 right@2
>> 1,3,2=>  pointers: left@1 right@2
>> 1,2,3=>  pointers collision @ 1
call: 6,7,8,5
>> 6,7,8,5=>  pointers: left@0 right@3
>> 5,7,8,6=>  pointers: left@1 right@3
>> 5,6,8,7=>  pointers: left@1 right@2
>> 5,6,8,7=>  pointers collision @ 1
call: 8,7
>> 8,7=>  pointers: left@0 right@1
>> 7,8=>  pointers collision @ 1
output: 1,2,3,4,5,6,7,8
array length: 8, comparisons: 13, swaps: 9, function calls: 7 
**/