Java 병합 정렬 알고리즘 구현? 자세한 설명 및 전체 자습서
게시 됨: 2021-01-20
Crunchify에서 우리는 지금까지 500+ Java and Spring MVC
기술 관련 튜토리얼을 작성했습니다. 새로운 것을 배우는 것은 결코 지루하지 않습니다. 저는 매일 새로운 것을 배우는 것을 좋아하고 제 독자들도 마찬가지라고 믿습니다. :).
버블정렬 알고리즘 이전에 보셨다시피 선택정렬 알고리즘과 삽입정렬 알고리즘은 다양한 면접에서 매우 인기가 있습니다.
이 튜토리얼에서는 Merge Sort Algorithm
에 대해 알아보겠습니다.
병합 정렬 알고리즘은 매우 간단합니다. 배열이 한 수준에 도달하면 반으로 나눈 다음 정렬합니다. 다음 단계는 순서대로 병합하는 것입니다. 기본적으로 divide and conquer
방식입니다.
다음은 요소를 분할하고 병합하는 방법에 대한 병합 정렬에 대한 간단한 설명입니다.

오늘 이 튜토리얼에서 아래의 모든 질문에 답해 보겠습니다.
- 병합 정렬 알고리즘이란 무엇입니까?
- 병합의 구현은 무엇입니까?
- Java의 병합 정렬 – 자습서
- 병합 정렬 자바 코드
우리는 아래 단계를 수행할 것입니다:
- 크기가 10
crunchifyArray
생성 - 배열에 임의의 정수 10개 채우기
- 초기 배열 인쇄
- 병합 정렬 수행
- 병합 정렬 후 최종 배열 인쇄
다음은 자바 코드입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
package crunchify . com . tutorial ; import java . util . * ; /** * @author Crunchify.com Merge Sort Algorithm in Java * */ public class CrunchifyMergeSortAlgorithm { public static Integer [ ] crunchifyArray = new Integer [ 10 ] ; ; public static int iteration = 1 ; // Divide and Merge public static void crunchifyMergeSort ( Integer [ ] crunchifyArray ) { Integer [ ] tempArray = new Integer [ crunchifyArray . length ] ; // recursively perform merge sort crunchifyMergeSort ( crunchifyArray , tempArray , 0 , crunchifyArray . length - 1 ) ; } // Idea is simple: // First divide whole array into 2 part // Divide each array into another 2 part // Once it reaches to only 1 elements in each side tree, just sort it // Merge backward the same way to get complete sorted array private static void crunchifyMergeSort ( Integer [ ] crunchifyArray , Integer [ ] tempArray , int myLeft , int myRight ) { if ( myLeft < myRight ) { int center = ( myLeft + myRight ) / 2 ; crunchifyMergeSort ( crunchifyArray , tempArray , myLeft , center ) ; crunchifyMergeSort ( crunchifyArray , tempArray , center + 1 , myRight ) ; crunchifyMerge ( crunchifyArray , tempArray , myLeft , center + 1 , myRight ) ; } } // Merge Sort Algorithm private static void crunchifyMerge ( Integer [ ] crunchifyArray , Integer [ ] tempArray , int left , int myRight , int rightMost ) { int leftEnd = myRight - 1 ; int k = left ; int num = rightMost - left + 1 ; while ( left < = leftEnd && myRight <= rightMost) if (crunchifyArray[left].compareTo(crunchifyArray[myRight]) <= 0) tempArray[k++] = crunchifyArray[left++]; else tempArray [ k ++ ] = crunchifyArray [ myRight ++ ] ; while ( left < = leftEnd ) tempArray [ k ++ ] = crunchifyArray [ left ++ ] ; while ( myRight < = rightMost ) tempArray [ k ++ ] = crunchifyArray [ myRight ++ ] ; for ( int i = 0 ; i < num ; i ++ , rightMost -- ) crunchifyArray [ rightMost ] = tempArray [ rightMost ] ; System . out . print ( "Iteration # " + iteration + " ===> " ) ; crunchifyPrint ( ) ; iteration ++ ; } // Get Random Integer in Java public static Integer getRandomIntegers ( ) { Random crunchifyRandom = new Random ( ) ; int myNumber = crunchifyRandom . nextInt ( 150 ) ; return myNumber ; } // Simply Print Arrays public static void crunchifyPrint ( ) { for ( int n : crunchifyArray ) { System . out . print ( " " + n + " " ) ; } System . out . print ( "\n" ) ; } // Main Method public static void main ( String [ ] args ) { for ( int i = 0 ; i < crunchifyArray . length ; i ++ ) { crunchifyArray [ i ] = getRandomIntegers ( ) ; } System . out . print ( "Here is our initial array: " ) ; crunchifyPrint ( ) ; System . out . println ( ) ; // Perform actual sorting crunchifyMergeSort ( crunchifyArray ) ; System . out . println ( ) ; System . out . print ( "Here is an array after Merge Sort: " ) ; crunchifyPrint ( ) ; } } |
Eclipse 콘솔 출력:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Here is our initial array : 45 53 10 50 48 8 106 77 44 108 Iteration # 1 ===> 45 53 10 50 48 8 106 77 44 108 Iteration # 2 ===> 10 45 53 50 48 8 106 77 44 108 Iteration # 3 ===> 10 45 53 48 50 8 106 77 44 108 Iteration # 4 ===> 10 45 48 50 53 8 106 77 44 108 Iteration # 5 ===> 10 45 48 50 53 8 106 77 44 108 Iteration # 6 ===> 10 45 48 50 53 8 77 106 44 108 Iteration # 7 ===> 10 45 48 50 53 8 77 106 44 108 Iteration # 8 ===> 10 45 48 50 53 8 44 77 106 108 Iteration # 9 ===> 8 10 44 45 48 50 53 77 106 108 Here is an array after Merge Sort : 8 10 44 45 48 50 53 77 106 108 |
crunchifyMergeSort
및 crunchifyMerge
두 가지 방법을 이해하기 위해 신중하게 디버깅 프로그램을 시도하십시오. 질문이 있거나 위의 코드를 실행하는 데 문제가 있으면 알려주십시오.

Big O 표기법 / 병합 정렬 알고리즘 복잡성이란 무엇입니까?
-
n*log(n)
병합 정렬 최상의 시나리오 복잡성?
- 이미 정렬된 입력의 경우
O(n)