๐Ÿ“š ์…ธ ์ •๋ ฌ

temprmnยท2023๋…„ 8์›” 4์ผ
0
post-thumbnail

์…ธ ์ •๋ ฌ

shell-sort-concepts.png

์ •๋ ฌ ๋ฐฉ๋ฒ•

  1. ๊ฐ„๊ฒฉ(gap)์„ ์„ค์ •ํ•œ๋‹ค.
  2. ๊ฐ ๊ฐ„๊ฒฉ ๋ณ„๋กœ ๋ถ„๋ฅ˜๋œ ์„œ๋ธŒ(๋ถ€๋ถ„) ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์‚ฝ์ž…์ •๋ ฌ์„ ํ•œ๋‹ค.
  3. ๊ฐ ์„œ๋ธŒ(๋ถ€๋ถ„) ๋ฆฌ์ŠคํŠธ์˜ ์ •๋ ฌ์ด ๋๋‚˜๋ฉด ๊ฐ„๊ฒฉ์„ ์ค„์ธ๋‹ค.
  4. ๊ฐ„๊ฒฉ์ด 1์ด ๋  ๋•Œ ๊นŒ์ง€ 2๋ฒˆ ๊ณผ์ •์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๋ฉฐ ๋ฐ˜๋ณตํ•œ๋‹ค.
๐Ÿค” ๊ทธ๋Ÿผ ๊ฐ„๊ฒฉ(gap)์€ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•ด์š”? ๐Ÿ˜ฒ ๋‹ต์€ ์—†๋‹ค.

์™œ๋ƒํ•˜๋ฉด ๊ฐ„๊ฒฉ(gap)์ด ๋„ˆ๋ฌด ์ ์œผ๋ฉด ๊ฑด๋„ˆ ๋›ฐ๋Š” ๊ฐ„๊ฒฉ์ด ์ข์•„์ง„๋‹ค. ์ฆ‰, pass ์†๋„๊ฐ€ ๋Š๋ ค์ง„๋‹ค๋Š” ๋œป์ด๋‹ค.

๋ฐ˜๋Œ€๋กœ ๊ฐ„๊ฒฉ์ด ํฌ๋ฉด ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ํ‰๊ท ์ ์œผ๋กœ ์ข‹์€ ๊ฐ„๊ฒฉ๋“ค์„ ๋‚ด๋†“์•˜๋Š”๋ฐ, ์ด๋ฅผ ๊ฐญ ์‹œํ€€์Šค(Gap Sequences)๋ผ๊ณ  ํ•œ๋‹ค.

์•„๋ž˜ ๊ธ€์„ ๋ณด๋ฉด ๋งŽ์€ ๊ฐญ ์‹œํ€€์Šค๋“ค์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
Shellsort

์…ธ ์ •๋ ฌ์˜ ํŠน์ง•

๐Ÿ“š ์‚ฝ์ž… ์ •๋ ฌ์€ ์ดˆ๊ธฐ๋ฆฌ์ŠคํŠธ๊ฐ€ "๊ฑฐ์˜ ์ •๋ ฌ"๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ ํšจ์œจ์ ์ด๋‹ค.

์—ฐ์†์ ์ด์ง€ ์•Š์€ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ์—์„œ ์ž๋ฃŒ์˜ ๊ตํ™˜์ด ์ผ์–ด๋‚˜๋ฉด ๋” ํฐ ๊ฑฐ๋ฆฌ๋ฅผ ์ด๋™ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ตํ™˜๋˜๋Š” ์š”์†Œ๋“ค์ด ์‚ฝ์ž… ์ •๋ ฌ๋ณด๋‹ค๋Š” ์ตœ์ข… ์œ„์น˜์— ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์•„์ง„๋‹ค.

๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ๋Š” ์–ด๋Š ์ •๋„ ์ •๋ ฌ์ด ๋œ ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1์ด ๋˜๊ฒŒ ๋˜๋ฉด ์…ธ ์ •๋ ฌ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฝ์ž… ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ ์‚ฝ์ž… ์ •๋ ฌ๋ณด๋‹ค ๋”์šฑ ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋œ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„

  • ํ‰๊ท : T(n) = O(n^1.5)
  • ์ตœ์•…์˜ ๊ฒฝ์šฐ: T(n) = O(n^2)

๊ตฌํ˜„

  • ์‚ฌ์šฉ ์‹œํ€€์Šค: Ciura ์‹œํ€€์Šค
    • gap Sequence = {1, 4, 10, 23, 57, 132, 301, 701, 1750}

    • 1750 ์ดํ›„๋กœ๋Š” Gn = (Gn - 1) * 2.25 ๋กœ ํ‘œํ˜„

    • ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

      private final static int[] gap = 
      		{ 1, 4, 10, 23, 57, 132, 301, 701, 1750, 3937, 	
      		8858, 19930, 44842, 100894, 227011, 510774,
      		1149241, 2585792, 5818032, 13090572, 29453787, 
      		66271020, 149109795, 335497038, 754868335, 1698453753};	// ๊ฐญ์„ ๋‹ด๊ณ ์žˆ๋Š” ๋ฐฐ์—ด
  • 1๏ธโƒฃ ์‚ฝ์ž… ์ •๋ ฌ์„ ํฌํ•จํ•œ ๊ตฌํ˜„
    public class Shell_Sort {
    	
    	private final static int[] gap = 
    		{ 1, 4, 10, 23, 57, 132, 301, 701, 1750, 3937, 	
    		8858, 19930, 44842, 100894, 227011, 510774,
    		1149241, 2585792, 5818032, 13090572, 29453787, 
    		66271020, 149109795, 335497038, 754868335, 1698453753};	// ๊ฐญ์„ ๋‹ด๊ณ ์žˆ๋Š” ๋ฐฐ์—ด	
     
    	
    	public static void shell_sort(int[] a) {
    		shell_sort(a, a.length);
    		
    	}
     
    	// ๋งจ ์ฒ˜์Œ gap์„ ์ฐธ์กฐํ•  ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ
    	private static int getGap(int length) {
    		int index = 0;
    		// ์ตœ์†Œํ•œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ 2๊ฐœ์”ฉ์€ ๋น„๊ต๋˜๋„๋ก ๋‚˜๋ˆ ์ค€๋‹ค.
    		int len = (int)(length / 2.25);	// โ†’ Gn = (Gn - 1) * 2.25
    		while (gap[index] < len) {
    				index++;
    		}
    		return index;
    	}
     
    	private static void shell_sort(int[] a, int size) {
    		int index = getGap(size);	// ์ฒซ gap์„ ์‚ฌ์šฉํ•  index
     
    		// gap[index] ๊ฐ’๋ถ€ํ„ฐ gap[0] ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
    		for (int i = index; i >= 0; i--) { 
    			for (int j = 0; j < gap[i]; j++) {
    				insertion_sort(a, j, size, gap[i]);	// ๊ฐ ๋ถ€๋ถ„ ๋ฆฌ์ŠคํŠธ์— ๋Œ€ํ•ด ์‚ฝ์ž…์ •๋ ฌ์„ ํ•œ๋‹ค.
    			}
    		}
    	}
     
    	/**
    	 * @param a		   ๋ฐฐ์—ด
    	 * @param start	 ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ ์ธ๋ฑ์Šค 
    	 * @param size	 ๋ฐฐ์—ด์˜ ์ „์ฒด ํฌ๊ธฐ
    	 * @param gap	   ํ˜„์žฌ gap
    	 */
    	private static void insertion_sort(int[] a, int start, int size, int gap) {
     
    		// ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ๋‘ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ size๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. (gap ๊ฐ’์”ฉ ๊ฑด๋„ˆ๋”) 
    		for (int i = start + gap; i < size; i += gap) {
     
    			int target = a[i];
    			int j = i - gap;
     
    			// ํƒ€๊ฒŸ ์›์†Œ๊ฐ€ ์ด์ „์˜ ์›์†Œ๋ณด๋‹ค ์ž‘์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต 
    			while (j >= start && target < a[j]) {
    					a[j + gap] = a[j];	// ์ด์ „ ์›์†Œ๋ฅผ ํ•œ ์นธ์”ฉ ๋’ค๋กœ ๋ฏธ๋ฃฌ๋‹ค.
    					j -= gap;
    			}
    			/*
    			 * ์œ„ ๋ฐ˜๋ณต๋ฌธ์—์„œ ํƒˆ์ถœ ํ•˜๋Š” ๊ฒฝ์šฐ ์•ž์˜ ์›์†Œ๊ฐ€ ํƒ€๊ฒŸ๋ณด๋‹ค ์ž‘๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ
    			 * ํƒ€๊ฒŸ ์›์†Œ๋Š” j๋ฒˆ์งธ ์›์†Œ ๋’ค์— ์™€์•ผํ•œ๋‹ค.
    			 * ๊ทธ๋Ÿฌ๋ฏ€๋กœ ํƒ€๊ฒŸ์€ j + gap ์— ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค.
    			 */
    			a[j + gap] = target;
     
    		}
    	}
    }
  • 2๏ธโƒฃ ์‚ฝ์ž… ์ •๋ ฌ์„ ์ œ์™ธํ•œ ๊ตฌํ˜„
    public class Shell_Sort {
    	
    	private final static int[] gap = 
    		{ 1, 4, 10, 23, 57, 132, 301, 701, 1750, 3937, 	
    		8858, 19930, 44842, 100894, 227011, 510774,
    		1149241, 2585792, 5818032, 13090572, 29453787, 
    		66271020, 149109795, 335497038, 754868335, 1698453753};	// ๊ฐญ์„ ๋‹ด๊ณ ์žˆ๋Š” ๋ฐฐ์—ด	
     
    	
    	public static void shell_sort(int[] a) {
    			shell_sort(a, a.length);
    	}
     
    	// ๋งจ ์ฒ˜์Œ gap์„ ์ฐธ์กฐ ํ•  ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    	private static int getGap(int length) {
    			int index = 0;
    			// ์ตœ์†Œํ•œ ๋ถ€๋ถ„ ๋ฐฐ์—ด์˜ ์›์†Œ๊ฐ€ 2๊ฐœ์”ฉ์€ ๋น„๊ต ๋˜๋„๋ก ๋‚˜๋ˆ ์ค€๋‹ค.
    			int len = (int)(length / 2.25);	
    			while (gap[index] < len) {
    				index++;
    			}
    			return index;
    	}
    	
     	private static void shell_sort(int[] a, int size) {
    			int gapIndex = getGap(size);
    		
    			// ๊ฐญ์ด 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    			while(gapIndex >= 0) {
    					int step = gap[gapIndex--];	// ํ˜„์žฌ gap(step)
    					/*
    					 * <--- ์‚ฝ์ž… ์ •๋ ฌ ๊ณผ์ • --->
    					 * 
    					 *  ๊ฐ ๋ถ€๋ถ„๋ฆฌ์ŠคํŠธ์˜ ๋‘ ๋ฒˆ์งธ ์›์†Œ์˜ ์ธ๋ฑ์Šค ๋ถ€ํ„ฐ ์ˆœํšŒํ•œ๋‹ค.
    					 *  ์˜ˆ๋ฅผ ๋“ค์–ด, step์ด 3์ผ ๋•Œ arr[0], arr[1], arr[2] ๋Š” 
    					 *  ์ด์ „ ์›์†Œ์™€ ๋น„๊ตํ•  ๊ฒƒ์ด ์—†๋‹ค.
    					 *  ๊ทธ๋Ÿฌ๋ฏ€๋กœ step๋ถ€ํ„ฐ ์ˆœํšŒํ•œ๋‹ค.   
    					 */
    					for(int i = step; i < size; i++) {				
    							/*
    							 *  j๋Š” target ์›์†Œ๊ฐ€ ๋˜๋ฉฐ ํ˜„์žฌ ์›์†Œ(target) a[j]๊ฐ€ ์ด์ „ ์›์†Œ a[j - step]๋ณด๋‹ค
    							 *  ์ž‘์„ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
    							 */
    							for (int j = i; j >= step && a[j] < a[j - step]; j -= step) {
    									/*
    									 *  ํ˜„์žฌ(target) ์›์†Œ์˜ ์ธ๋ฑ์Šค(j)์™€ ์ด์ „์˜ ์›์†Œ(j-step)์˜ ์ธ๋ฑ์Šค์— ์žˆ๋Š”
    									 *  ์›์†Œ์˜ ๊ฐ’์„ ๊ตํ™˜ํ•œ๋‹ค.
    									 */
    									swap(a, j, j - step);
    							}
    					}
    			}
    	}
     
    	private static void swap(int[] a, int i, int j) {
    			int swap = a[i];
    			a[i] = a[j];
    			a[j] = swap;
    	}
    }

์ฐธ๊ณ 
[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์…ธ ์ •๋ ฌ(shell sort)์ด๋ž€ - Heee's Development Blog
์ž๋ฐ” [JAVA] - ์…ธ ์ •๋ ฌ (Shell Sort)

profile
`ISFJ` T 49% F 51% /

0๊ฐœ์˜ ๋Œ“๊ธ€