bubbleSort.js
/**
* Use the Bubble Sort algorithm to sort an array
* of integers in ascending order.
*
* // Pseudocode for the Bubble Sort Algorithm
* While the array is unsorted:
* 	For each pass from left to right through the array:
*   	// Compare each pair of adjacent array elements.
*   	If the current element is greater than the next element:
*   		Swap the elements.
*			Increment the number of swaps.
*  		End If.
* 	End For.
* 	Increment the number of passes.
*	If the array is sorted:
* 		Return the sorted array (and any relevant statistics).
* 	End if.
* End While.
*
* @param {Object[]} A an array of integers
* @return a tuple consisting of the sorted array,
* number of swaps made, and the number of passes made
* during sorting.
*/
Use the Bubble Sort algorithm to sort an array
of integers in ascending order.

Pseudocode for the Bubble Sort Algorithm
While the array is unsorted:
For each pass from left to right through the array:
Compare each pair of adjacent array elements.
If the current element is greater than the next element:
Swap the elements.
Increment the number of swaps.
End If.
End For.
Increment the number of passes.
If the array is sorted:
Return the sorted array (and any relevant statistics).
End if.
End While.
Use the Bubble Sort algorithm to sort an array of integers in ascending order . Pseudocode for the Bubble Sort Algorithm While the array is unsorted : For each pass from left to right through the array : Compare each pair of adjacent array elements . If the current element is greater than the next element : Swap the elements . Increment the number of swaps . End If . End For . Increment the number of passes . If the array is sorted : Return the sorted array ( and any relevant statistics ) . End if . End While .
function bubbleSort(A) {
	var numSwaps = 0, numPasses = 0;
	var arrayIsSorted = false, swapOccurredDuringThisPass = false;
	while (!arrayIsSorted) {
		for (var i = 0; i < A.length; i++) {
			if ((i < (A.length - 1)) && (A[i] > A[i+1])) {
				var temp = A[i];
				A[i] = A[i+1];
				A[i+1] = temp;
				numSwaps += 1;
				swapOccurredDuringThisPass = true;
			}
		}
		numPasses += 1;
		if (!swapOccurredDuringThisPass) {
			return [A, numSwaps, numPasses];
		}
		swapOccurredDuringThisPass = false;
	}
}
function bubbleSort ( A ) { var numSwaps = 0 , numPasses = 0 ; var arrayIsSorted = false , swapOccurredDuringThisPass = false ; while ( ! arrayIsSorted ) { for ( var i = 0 ; i < A . length ; i ++ ) { if ( ( i < ( A . length - 1 ) ) && ( A [ i ] > A [ i + 1 ] ) ) { var temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; numSwaps += 1 ; swapOccurredDuringThisPass = true ; } } numPasses += 1 ; if ( ! swapOccurredDuringThisPass ) { return [ A , numSwaps , numPasses ] ; } swapOccurredDuringThisPass = false ; } }
Use the Bubble Sort algorithm to sort an array
of integers in ascending order.
Use the Bubble Sort algorithm to sort an array of integers in ascending order .
// Initialize the statistics variables. // Initialize the Boolean flags needed for the sort. // Sort the array. Assume that it is initially unordered.  // For each pass from left to right through the array... // Compare each pair of adjacent array elements. // If the current element is greater than the next element... // Swap the elements. // Increment the number of swaps. // Increment the number of passes. // If the array is sorted... // Return the sorted array (and any relevant statistics). // Continue sifting through the array until it is all ordered.