JS 100제 문제 51 merge sort를 만들어보자


<풀이 코드>

function mergeSort(arr){
		if (arr.length <= 1){
			return arr;
		}
		
	    const mid = Math.floor(arr.length / 2);
		const left = arr.slice(0,mid);
	    const right = arr.slice(mid);

	  	return merge(mergeSort(left), mergeSort(right));
	}

	function merge(left, right){
	  	let result = [];

	  	while (left.length && right.length){
	    	if (left[0]<right[0]){
	      		result.push(left.shift());
	    	} else {
	      		result.push(right.shift());
	    	}
	  	}
	  	
	  	while (left.length) {
	    	result.push(left.shift());
	  	}
	  	
	  	while (right.length) {
	  		result.push(right.shift());
	  	}

	  	return result;
	}

	const array = prompt('배열을 입력하세요').split(' ').map(n => parseInt(n, 10));

	console.log(mergeSort(array));

좋은 웹페이지 즐겨찾기