You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# python의 경우 list slicing을 활용해 더 깔끔한 코드가 가능하지만# slicing 때마다 List를 복사하므로 성능적인 부분과# 다른 언어의 코드와 유사한 형태로 작성하기 위해 index를 활용했습니다.defmerge(valueList, left, right, mid):
temp= []
i, j=left, mid+1whilei<=midandj<=right:
ifvalueList[i] <=valueList[j]:
temp.append(valueList[i])
i+=1else:
temp.append(valueList[j])
j+=1# 남은 값 채우기whilei<=mid:
temp.append(valueList[i])
i+=1whilej<=right:
temp.append(valueList[j])
j+=1# 원래 리스트 값 변경foridxinrange(len(temp)):
valueList[idx+left] =temp[idx]
defmergeSort(valueList, start, end):
ifstart<end :
mid= (start+end)//2# 분할mergeSort(valueList, start, mid)
mergeSort(valueList, mid+1, end)
# 병합merge(valueList, start, end, mid)
JavaScript(Node.js) Version
letmerge=(list,left,right,mid)=>{lettemp=newArray();leti=left,j=mid+1;while(i<=mid&&j<=right){if(list[i]<=list[j]){temp.push(list[i++]);}else{temp.push(list[j++]);}}// 남은 값 뒤에 채우기while(i<=mid){temp.push(list[i++]);}while(j<=right){temp.push(list[j++]);}// 원래 배열에 복사temp.forEach((value,idx)=>{list[left+idx]=value;});};letmergeSort=(list,start,end)=>{if(start<end){letmid=(start+end)>>1;// 분할mergeSort(list,start,mid);mergeSort(list,mid+1,end);// 병합merge(list,start,end,mid);}};