cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              
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
🕑 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