Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                
const unsortedArray = [1, 4, 11, 2, 5, 55, 66, 21, 43, 0];
console.log(mergeSort(unsortedArray));

function mergeSort(array) {

	// If there is only one element in either of
	// the left/right arrays, return the array since it
	// it can't be split, and is, presumably, sorted!

	// I was confused as to why the array returned was the
	// result object from the merge() function and not this
	// array with a length of 1, and TBQH...
	// I still don't 100% get it. I think the return from merge()
	// overrides this one, somehow?
	if (array.length === 1) {
		return array;
	}

	// Split the array in half
	let splitHere = parseInt(array.length / 2),
			leftArray = array.slice(0, splitHere),
			rightArray = array.slice(splitHere, array.length);

	return merge(mergeSort(leftArray), mergeSort(rightArray));
}

function merge(leftArray, rightArray) {

	// Create an array that will hold our result
	let result = [];

	// While both of the arrays have items:
	while(leftArray.length && rightArray.length) {

		// Note that .shift() returns the first item in an array
		// and _removes_ it from that array

		// Compare the starting items of each array
		// If the left starting item is smaller, move it to
		// the array where we are storing our result
		// otherwise, move the right starting item to result
		if (leftArray[0] <= rightArray[0]) {
			result.push(leftArray.shift());
		} else {
			result.push(rightArray.shift());
		}
	}

	// These while loops will execute only when the
	// above loop breaks when either left/right arrays
	// run out of items.

	// If just the left array has items left, move
	// the first one to the result
	while(leftArray.length) {
		result.push(leftArray.shift());
	}

	// If just the left array has items left, move
	// the first one to the result
	while(rightArray.length) {
		result.push(rightArray.shift());
	}

	// Until the array is sorted i.e. when leftArray and rightArray
	// no longer have items, the program will be caught in the
	// while loops above, but when none of those while statments
	// are true, the original array has been sorted and it can be
	// returned.

	// So, even though we are calling this function several times,
	// it doesn't makes it to the below return until the last go-round.

	// Wowza!
	return result;
}



              
            
!
999px

Console