Skip to content

Latest commit

Β 

History

History
734 lines (554 loc) Β· 20.9 KB

File metadata and controls

734 lines (554 loc) Β· 20.9 KB

버블 μ •λ ¬, μ‚½μž… μ •λ ¬, 선택 μ •λ ¬

μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜

μ°Έκ³  λ™μž‘ 방식 링크 : https://visualgo.net/en/sorting

πŸ‡ μ •μ˜

  • μ–΄λ–€ 데이터듀이 μ£Όμ–΄μ‘Œμ„λ•Œ, 이λ₯Ό μ •ν•΄μ§„ μˆœμ„œλŒ€λ‘œ λ‚˜μ—΄ν•˜λŠ” 것
  • λ­”κ°€λ₯Ό μ •λ ¬ν•˜λŠ” 것
    • AλΆ€ν„° ZκΉŒμ§€ κΈ°μ€€μœΌλ‘œ μ •λ ¬
    • 큰 μˆ˜μ—μ„œ μž‘μ€ 수 κΈ°μ€€μœΌλ‘œ μ •λ ¬
  • 데이터λ₯Ό νŠΉμ •ν•œ 기쀀에 λ”°λΌμ„œ μˆœμ„œλŒ€λ‘œ λ‚˜μ—΄ν•˜λŠ” 것
  • ν”„λ‘œκ·Έλž¨ μž‘μ„± μ‹œ κ°€μž₯ 많이 μ‚¬μš©
  • μ΄μ§„κ²€μƒ‰μ²˜λŸΌ λΉ λ₯Έ μ•Œκ³ λ¦¬μ¦˜ μ‚¬μš©μ„ μœ„ν•΄μ„œ
  • μ•Œκ³ λ¦¬μ¦˜μ˜ νš¨μœ¨μ„±μ„ μ‰½κ²Œ 이해 κ°€λŠ₯
  • λ‚΄λ¦Όμ°¨μˆœμ€ μ˜€λ¦„μ°¨μˆœμ€ reverseλ₯Ό μ΄μš©ν•΄μ„œ λ’€μ§‘μœΌλ©΄ 됨

πŸ‡ 버블 μ •λ ¬

  • 두 μΈμ ‘ν•œ 데이터λ₯Ό λΉ„κ΅ν•΄μ„œ , μ•žμ— μžˆλŠ” 데이터가 뒀에 μžˆλŠ” 데이터보닀 크면, 자리λ₯Ό λ°”κΎΈλŠ” μ•Œκ³ λ¦¬μ¦˜
  • 자주 μ‚¬μš©λ˜μ§€λŠ” μ•ŠμŒ 더 λΉ λ₯Έ μ•Œκ³ λ¦¬μ¦˜μ΄ 많기 λ•Œλ¬Έμ΄λ‹€.

정렬방법

  • λ°°μ—΄μ˜ 2개의 μ•„μ΄ν…œμ„ 선택

  • 비ꡐ(comparisons) : μ™Όμͺ½μ΄ 였λ₯Έμͺ½λ³΄λ‹€ 크면 κ΅ν™˜(swap)

  • 였λ₯Έμͺ½μœΌλ‘œ μ΄λ™ν•΄μ„œ, ν•΄λ‹Ή ν”„λ‘œμ„ΈμŠ€ 반볡 , 예λ₯Ό λ“€μ–΄ 5와 6을 비ꡐ

    β‡’ μ™Όμͺ½μ΄ 였λ₯Έμͺ½λ³΄λ‹€ μž‘μœΌλ―€λ‘œ κ΅ν™˜ν•˜μ§€ μ•ŠμŒ

  1. κ·Έ λ‹€μŒμ€ 6κ³Ό 3을 비ꡐ β‡’ κ΅ν™˜ κ²°κ³Ό : 2 5 3 6 1 4
  2. 6κ³Ό 1을 비ꡐ β‡’ κ΅ν™˜ κ²°κ³Ό : 2 5 3 1 6 4
  3. 6κ³Ό 4λ₯Ό 비ꡐ β‡’ κ΅ν™˜ κ²°κ³Ό : 2 5 3 1 4 6 β‡’ 첫번째 사이클 끝 λ‘λ²ˆμ§Έλ„ μ™Όμͺ½μ—μ„œ μ²˜μŒλΆ€ν„° λ°”κΏˆ

✨ λ²„λΈ”μ •λ ¬μ˜ μ‹œκ°„λ³΅μž‘λ„

  1. comparisons(비ꡐ) : N-1

    N(item의 λ§ˆμ§€λ§‰ 번호), λ°°μ—΄μ˜ N-1의 μ•„μ΄ν…œμ„ 비ꡐ

    EX. μ•„μ΄ν…œμ΄ 6개면, 5번 비ꡐ

  2. swap(κ΅ν™˜): μ΅œμ•…μ˜ 경우, λͺ¨λ“  μ•„μ΄ν…œμ„ κ΅ν™˜ν•΄μ•Ό 함

  3. κ·Έλž˜ν”„

✨ 버블정렬 예제 1

버블 정렬은 κ°„λ‹¨ν•˜μ§€λ§Œ 큰 λ¦¬μŠ€νŠΈμ— λŒ€ν•΄μ„œλŠ” λΉ„νš¨μœ¨μ μΌ 수 μžˆμœΌλ‹ˆ μ°Έκ³ 

import java.util.ArrayList;
import java.util.Collections;

public class BubbleSorting {

    public static void main(String[] args){
        ArrayList<Integer> dataList = new ArrayList<Integer>();
        dataList.add(9);
        dataList.add(2);
        dataList.add(4);
        dataList.add(8);
        dataList.add(1);

        // 9, 7, 1
        for(int index=0; index < dataList.size()-1; index++) {
            boolean swap = false;
            // 7, 9, 1
            for(int index2 = 0; index2 < dataList.size() - 1 - index; index2++) {
                if (dataList.get(index2) > dataList.get(index2 + 1)) {
                    Collections.swap(dataList, index2, index2 + 1);
                    // 1, 7, 9
                    swap = true;
                }
            }
        }
        System.out.println(dataList);
    }
}

✨ 버블정렬 예제 2

  • κΈ°μ‘΄ μ½”λ“œμ˜ 클래슀λ₯Ό μ΄μš©ν•΄μ„œ λ§Œλ“œλŠ” 방법
  1. BubbleSorting2
package sorting;

import java.util.ArrayList;
import java.util.Collections;

public class BubbleSorting2
{

	public void sort(ArrayList arr) {
		ArrayList<Integer> dataList = new ArrayList<Integer>();
		dataList = arr;
		
		for (int index = 0; index < dataList.size() - 1; index++)
		{
			boolean swap = false;

			for (int index2 = 0; index2 < dataList.size() - 1 - index; index2++)
			{
				if (dataList.get(index2) > dataList.get(index2 + 1))
				{
					Collections.swap(dataList, index2, index2 + 1);
					swap = true;
				}
			}
			System.out.println(dataList);
		}
	}

}
  1. BublleSorting
import java.util.ArrayList;

public class BubbleSorting
{

    public static void main(String[] args)
    {
        ArrayList<Integer> testList = new ArrayList<Integer>();

        for (int index = 0; index < 10; index++)
        {
            testList.add((int)(Math.random() * 100));

        }
        System.out.println(testList);

        BubbleSorting2 bSort = new BubbleSorting2();

        bSort.sort(testList);

    }

}

✨ 버블정렬 예제 3

  1. 문제링크 : https://jungol.co.kr/problem/1157?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjJ9
  2. μ½”λ“œ
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class BubbleSorting3 {

    private ArrayList<Integer> sort(ArrayList<Integer> dataList){

        for(int index=0; index < dataList.size()-1; index++) {
            boolean swap = false;

            for(int index2 = 0; index2 < dataList.size() - 1 - index; index2++) {

                if (dataList.get(index2) > dataList.get(index2 + 1)) {

                    Collections.swap(dataList, index2, index2 + 1);
                    swap = true;
                }

            }

            for(int i = 0; i<dataList.size(); i++) {
                System.out.print(dataList.get(i)+ " ");
            }
            System.out.println();

        }
        return dataList;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        ArrayList<Integer> dataList = new ArrayList<Integer>();

        int n = in.nextInt();

        for(int i=0; i<n; i++){
            dataList.add(in.nextInt());
        }

        BubbleSorting3 bubbleSort = new BubbleSorting3();
        bubbleSort.sort(dataList);

    }
}
  1. λ‹€λ₯Έ μ‚¬λžŒ μ½”λ“œ
import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
        int numlength = sc.nextInt();
        int [] arr = new int [numlength];
       
       for (int i = 0; i < numlength; i++) {
           arr[i] = sc.nextInt();
       }
       
        
       for (int i = 0; i < numlength-1; i++) {
    	   
    	   
           for (int j = 0; j < numlength-1; j++) {
        	   
               if (arr[j] > arr[j+1])
               {
                   int temp = arr[j];
                   arr[j]=arr[j+1];
                   arr[j+1]=temp;
                   
               }
               
           }
              
           for (int j = 0; j < numlength; j++) {
               System.out.printf("%d ", arr[j]);
           }
           System.out.println();
           
       
       
       
           }
       
       }
       
}
  1. 파이썬 μ½”λ“œ
def bubble_sort(arr):
    end = len(arr) - 1
    while end > 0:
        last_swap = 0
        for i in range(end):
            if arr[i] > arr[i + 1]:
                arr[i], arr[i + 1] = arr[i + 1], arr[i]
                last_swap = i
        end = last_swap
        print(arr)

πŸ‡ 선택정렬

  • 전체 λͺ¨λ“  μ•„μ΄ν…œ μŠ€μΊ”
  • 데이터가 λ¬΄μž‘μœ„λ‘œ μ—¬λŸ¬ 개 μžˆμ„ λ•Œ, μ΄μ€‘μ—μ„œ κ°€μž₯ μž‘μ€ 데이터λ₯Ό 선택해 맨 μ•žμ— μžˆλŠ” 데이터와 λ°”κΎΈκ³ , κ·Έλ‹€μŒ μž‘μ€ 데이터λ₯Ό 선택해 μ•žμ—μ„œ 두 번째 데이터와 λ°”κΎΈλŠ” κ³Όμ • 반볡
  • ν˜„μž¬ λ°μ΄ν„°μ˜ μƒνƒœ 상관없이 무쑰건 λͺ¨λ“  μ›μ†Œλ₯Ό λΉ„κ΅ν•˜κ³  μœ„μΉ˜λ₯Ό λ°”κΏˆ
  • κ°€μž₯ μž‘μ€ 것을 선택, λΉ„νš¨μœ¨μ 

✨ 정렬방법

  1. 전체 μ•„μ΄ν…œ 쀑 κ°€μž₯ μž‘μ€ μ•„μ΄ν…œμ˜ μœ„μΉ˜λ₯Ό κ·Έ μœ„μΉ˜λ₯Ό λ³€μˆ˜μ— μ €μž₯

  2. 5λΆ€ν„° μ°¨λ‘€λ‘œ 확인 ν›„ 맨 μ²˜μŒμ€ 5κ°€ 제일 μž‘κΈ°λ•Œλ¬Έμ— μœ„μΉ˜λ₯Ό λ³€μˆ˜μ— μ €μž₯

  3. κ·Έ λ‹€μŒ 2μ—μ„œ 2κ°€ 5보닀 μž‘μœΌλ‹ˆκΉŒ μœ„μΉ˜λ₯Ό λ³€μˆ˜μ— μ €μž₯

  4. μ­‰ ν•˜λ‹€κ°€ 1이 2보닀 μž‘μœΌλ‹ˆκΉŒ μœ„μΉ˜λ₯Ό λ³€μˆ˜ μ €μž₯

  5. 4κΉŒμ§€ 확인 ν›„ 사이클을 끝냄

    β‡’ λ°°μ—΄μ—μ„œ κ°€μž₯ μž‘μ€ μˆ«μžκ°€ 어디에 μžˆλŠ”μ§€ μ•ˆλ‹€

  6. κ·Έ λ‹€μŒ swaps(λ°”κΎΈκΈ°) : κ°€μž₯ μž‘μ€ 숫자(μœ„μΉ˜λ₯Ό μ•Œμ§€!) 그것을 첫 번째 μ•„μ΄ν…œκ³Ό λ°”κΏˆ

  7. κ·Έ λ‹€μŒ 사이클을 μ§„ν–‰μ‹œ 1λΆ€ν„° μ‹œμž‘ν•˜μ§€ μ•ŠμŒ. 1은 μ •λ ¬λœ 숫자, μ •λ ¬λ˜μ§€ μ•Šμ€ λΆ€λΆ„ μ€‘μ—μ„œ κ°€μž₯ μž‘μ€ 숫자λ₯Ό 찾음

  8. 이것을 반볡

✨ 선택정렬 예제1

import java.util.ArrayList;
import java.util.Collections;

public class SelectionSortingEx {

    private ArrayList<Integer> sort(ArrayList<Integer> dataList){
        int lowest;
        for(int stand = 0; stand < dataList.size()-1; stand++){
            lowest = stand;
            for(int index = stand+1; index < dataList.size(); index++){
                if(dataList.get(lowest) > dataList.get(index)) {
                    lowest = index;
                }
            }
            Collections.swap(dataList, lowest, stand);
        }

        return dataList;
    }

    public static void main(String[] args){
        ArrayList<Integer> testData = new ArrayList<Integer>();

        for(int i=0; i<10; i++){
            testData.add((int)(Math.random()*100));
        }
        SelectionSortingEx selection = new SelectionSortingEx();
        System.out.println(selection.sort(testData));
    }
}

✨ 선택정렬 예제 2

  1. μ •μ˜¬ 문제 : https://jungol.co.kr/problem/1146/submission?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjB9
  2. μ½”λ“œ
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class SelectionSortingEx2 {

    private ArrayList<Integer> sort(ArrayList<Integer> dataList){
        int lowest;
        for(int stand = 0; stand < dataList.size()-1; stand++){
            lowest = stand;
            for(int index = stand+1; index < dataList.size(); index++){
                if(dataList.get(lowest) > dataList.get(index)) {
                    lowest = index;
                }
            }
            Collections.swap(dataList, lowest, stand);
            for(int i = 0; i<dataList.size(); i++) {
                System.out.print(dataList.get(i)+ " ");
            }
            System.out.println();
        }

        return dataList;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        ArrayList<Integer> testData = new ArrayList<Integer>();

        int n = in.nextInt();

        for(int i=0; i<n; i++){
            testData.add(in.nextInt());
        }
        SelectionSortingEx2 selection = new SelectionSortingEx2();
        selection.sort(testData);
    }
}
  1. 선택 μ •λ ¬ λ‹€λ₯Έ μ‚¬λžŒ μ½”λ“œ
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i, j, n;
        int arr[] = new int [101];
        int big = 0;
         
        n = scan.nextInt();
         
        for (i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
         
        for (i = 0; i < n-1; i++) {
            int small = arr[i];
            int pos = i;
            for (j = i; j < n; j++) {
                if (small > arr[j]) {
                    small = arr[j];
                    pos = j;
                }
            }
            int c = arr[i];
            arr[i] = arr[pos];
            arr[pos] = c;
                     
            for (j = 0; j < n; j++) {
                System.out.printf("%d ", arr[j]);
            }
            System.out.printf("\n");
         
        }
    }
 
}
  1. 파이썬 μ½”λ“œ
def selection_sort(arr):
    for i in range(len(arr) - 1):
        min_idx = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
        print(arr)

πŸ‡ μ‚½μž… μ •λ ¬

  • ν•„μš”ν•œ μ•„μ΄ν…œλ§Œ μŠ€μΊ”
  • 선택정렬보닀 빠름, 효율적
  • 데이터λ₯Ό ν•˜λ‚˜μ”© ν™•μΈν•˜λ©°, 각 데이터λ₯Ό μ μ ˆν•œ μœ„μΉ˜μ— μ‚½μž…

β‡’ ν•„μš”ν•œ λ•Œλ§Œ μœ„μΉ˜λ₯Ό λ°”κΎΈλ―€λ‘œ 데이터가 거의 μ •λ ¬λ˜μ–΄ μžˆμ„ λ•Œ 훨씬 효율적

  • νŠΉμ •ν•œ 데이터λ₯Ό μ μ ˆν•œ μœ„μΉ˜μ— β€˜μ‚½μž…β€™ν•œλ‹€λŠ” 의미

    β‡’ νŠΉμ •ν•œ 데이터가 μ μ ˆν•œ μœ„μΉ˜μ— λ“€μ–΄κ°€κΈ° 이전에, κ·Έ μ•žκΉŒμ§€μ˜ λ°μ΄ν„°λŠ” 이미 μ •λ ¬λ˜μ–΄ μžˆλ‹€κ³  κ°€μ •

정렬방법

  1. 인덱슀 1λΆ€ν„° μ‹œμž‘μ‹œ, μ™Όμͺ½μ— 숫자 β€˜2’보닀 큰 μˆ«μžκ°€ μžˆλŠ”μ§€ 확인

    β‡’ 두 번째 데이터 λΆ€ν„° μ‹œμž‘ν•˜λŠ” μ΄μœ λŠ” 첫 λ²ˆμ§ΈλŠ” κ·Έ 자체둜 μ •λ ¬λ˜μ–΄ μžˆλ‹€κ³  νŒλ‹¨

    Left       >   Right
    
  2. 2와 5 자리 λ°”κΎΈκΈ° swap

  3. 2번째 사이클 : 6을 선택, μ™Όμͺ½μ— 더 큰 μˆ«μžκ°€ μžˆλŠ”μ§€ 확인, 였λ₯Έμͺ½μ΄ 더 크기 λ•Œλ¬Έμ— 계속 μ§„ν–‰

                        Left       <   Right
    
  4. 3번째 사이클 : 3을 선택, μ™Όμͺ½μ— 더 큰 μˆ«μžκ°€ μžˆλŠ”μ§€ 확인,

                                            Left      >   Right
    
  5. 6κ³Ό 3을 swap

  6. 3 μ™Όμͺ½μ— μžˆλŠ” 5λ₯Ό 비ꡐ(comparisons)

                         Left      >   Right
    
  7. 5κ³Ό 3을 swap

  8. 3에 μ™Όμͺ½μ— μžˆλŠ” 2λŠ” 3보닀 μž‘κΈ° λ•Œλ¬Έμ— μ•ˆλ°”κΏˆ

  9. 반볡

❗ μ‚½μž… 정렬은, 정렬이 이루어진 μ›μ†ŒλŠ” 항상 μ˜€λ¦„μ°¨μˆœμ„ μœ μ§€ν•˜κΈ° λ•Œλ¬Έμ—, νŠΉμ •ν•œ 데이터가 μ‚½μž…λ  μœ„μΉ˜λ₯Ό μ„ μ •ν•  λ•Œ(μ‚½μž…λ  μœ„μΉ˜λ₯Ό μ°ΎκΈ° μœ„ν•˜μ—¬ μ™Όμͺ½μœΌλ‘œ ν•œ μΉΈμ”© 이동할 λ•Œ) μ‚½μž…λ  데이터보닀 μž‘μ€ 데이터λ₯Ό λ§Œλ‚˜λ©΄ κ·Έ μœ„μΉ˜μ—μ„œ λ©ˆμΆ”λ©΄ λœλ‹€.

πŸ’‘ νŠΉμ •ν•œ λ°μ΄ν„°μ˜ μ™Όμͺ½μ— μžˆλŠ” 데이터듀은 이미 정렬이 된 μƒνƒœμ΄λ―€λ‘œ μžκΈ°λ³΄λ‹€ μž‘μ€ 데이터λ₯Ό λ§Œλ‚¬λ‹€λ©΄, 더 이상 데이터 μ‚΄νŽ΄λ³Ό ν•„μš” 없이 μ‚½μž…ν•˜λ©΄ 됨

✨ μ‚½μž…μ •λ ¬ μ½”λ“œ

import java.util.ArrayList;
import java.util.Collections;

public class InsertionSort {

    public ArrayList<Integer> sort(ArrayList<Integer> dataList){
        for(int index=0; index<dataList.size()-1; index++){
            for(int index2 = index + 1; index2 > 0; index2--){
                if(dataList.get(index2) < dataList.get(index2-1)){
                    Collections.swap(dataList, index2, index2-1);
                }
                else{
                    break;
                }
            }
        }
        return dataList;
    }

    public static void main(String[] args) {
        ArrayList<Integer> dataList = new ArrayList<Integer>();

        for (int i = 0; i < 10; i++) {
            dataList.add((int) (Math.random() * 100));
        }
        InsertionSort insertion = new InsertionSort();
        System.out.println(insertion.sort(dataList));
    }
}

✨ μ‚½μž…μ •λ ¬ μ •μ˜¬ 예제

  1. 문제링크 : https://jungol.co.kr/problem/1158?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjF9
  2. μ½”λ“œ
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public ArrayList<Integer> sort(ArrayList<Integer> dataList){
        for(int index=0; index<dataList.size()-1; index++){
            for(int index2 = index + 1; index2 > 0; index2--){
                if(dataList.get(index2) < dataList.get(index2-1)){
                    Collections.swap(dataList, index2, index2-1);
                }
                else{
                    break;
                }
            }
            for(int i = 0; i<dataList.size(); i++) {
                System.out.print(dataList.get(i)+ " ");
            }
            System.out.println();
        }
        return dataList;
    }

    public static void main(String[] args) {
        ArrayList<Integer> dataList = new ArrayList<Integer>();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        for (int i = 0; i < n; i++) {
            dataList.add(in.nextInt());
        }
        Main insertion = new Main();
        insertion.sort(dataList);
    }
}
  1. λ‹€λ₯Έ μ‚¬λžŒ μ½”λ“œ
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
         
        int n, i, j;
        int arr[] = new int[101];
         
        n = scan.nextInt();
         
        for (i = 0; i < n; i++) {
            arr[i] = scan.nextInt();
        }
         
        for (i = 1; i < n; i++) {
            for (j = i; j > 0; j--) {
                if (arr[j] < arr[j - 1])
                {
                    int c = arr[j];
                    arr[j]=arr[j-1];
                    arr[j-1]=c;
                }
                else break;
            }
            for (j = 0; j < n; j++) {
                System.out.printf("%d ", arr[j]);
            }
            System.out.printf("\n");
        } 
    }
}

✨ 예제

array = [7, 5, 9, 0, 3, 1, 6, 2, 4, 8]

for i in range(1, len(array)):
    for j in range(i, 0, -1): # jλ³€μˆ˜κ°€ 인덱슀 iλΆ€ν„° 1κΉŒμ§€ 1μ”© κ°μ†Œν•˜λ©΄μ„œ λ°˜λ³΅ν•˜λŠ” 문법
        if array[j] < array[j-1]: # ν•œ μΉΈ μ”© μ™Όμͺ½μœΌλ‘œ 이동
            array[j], array[j-1] = array[j-1], array[j] 
        else: # μžκΈ°λ³΄λ‹€ μž‘μ€ 데이터λ₯Ό λ§Œλ‚˜λ©΄ κ·Έ μœ„μΉ˜μ—μ„œ 멈좀
            break
print(array)

πŸ’‘ range의 μ„Έ 번째 λ³€μˆ˜ : range의 λ§€κ°œλ³€μˆ˜λŠ” 3개(start, end, step)이닀. μ„Έ 번째 λ§€κ°œλ³€μˆ˜μΈ step에 -1이 λ“€μ–΄κ°€λ©΄ startμΈλ±μŠ€λΆ€ν„° μ‹œμž‘ν•΄μ„œ end+1μΈλ±μŠ€κΉŒμ§€ 1μ”© κ°μ†Œν•œλ‹€.

✨ μ‚½μž…μ •λ ¬μ˜ μ‹œκ°„λ³΅μž‘λ„

  • μ‹œκ°„λ³΅μž‘λ„ : $O(N^2)$

    β‡’ 2쀑 λ°˜λ³΅λ¬Έμ„ μ΄μš©ν•˜κΈ° λ•Œλ¬Έ

β‡’ 선택정렬보닀 빨라도 μ‹œκ°„λ³΅μž‘λ„λŠ” κ°™μŒ

β‡’ μ‚½μž… 정렬은 ν˜„μž¬ 리슀트의 데이터가 거의 μ •λ ¬λ˜μ–΄ μžˆλŠ” μƒνƒœλΌλ©΄ 맀우 λΉ λ₯΄κ²Œ λ™μž‘

✨ μ‹œκ°„λ³΅μž‘λ„κ°€ λ™μΌν•œ μ΄μœ λŠ” μ΅œμ•…μ˜ μ‹œλ‚˜λ¦¬μ˜€λ₯Ό 보지말고, 평균 μ‹œλ‚˜λ¦¬μ˜€λ₯Ό 봐야 ν•˜κΈ° λ•Œλ¬Έ

μ‚½μž… μ •λ ¬, 버블 μ •λ ¬, 선택 μ •λ ¬ μ„Έ κ°€μ§€ 비ꡐ

  1. 버블 μ •λ ¬
  • 맨 μ²˜μŒμ— μžˆλŠ” 것을 μ €μž₯ ν›„ λ‹€μŒ μžˆλŠ” 것을 비ꡐ ν•΄μ„œ λ‹€μŒ 것이 μ§€κΈˆ μžˆλŠ” 것보닀 적닀면 κ·Έ 수λ₯Ό swap함 β‡’ μ΄λŸ°μ‹μœΌλ‘œ 반볡
public static void bubbleSort(int[] array) {
    int n = array.length;
    for (int i = 0; i < n - 1; i++) {    // λͺ¨λ“  μš”μ†Œλ₯Ό 순회
        for (int j = 0; j < n - i - 1; j++) {  // iκΉŒμ§€λŠ” 이미 μ •λ ¬λ˜μ—ˆμœΌλ―€λ‘œ 비ꡐ할 ν•„μš”κ°€ μ—†μŒ
            if (array[j] > array[j + 1]) {    // λ‹€μŒ μš”μ†Œκ°€ ν˜„μž¬ μš”μ†Œλ³΄λ‹€ μž‘μœΌλ©΄ κ΅ν™˜
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
}
  • 파이썬 μ½”λ“œ
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]

# μ‚¬μš© μ˜ˆμ‹œ
arr = [5, 3, 8, 2, 1, 4]
bubble_sort(arr)
print("버블 μ •λ ¬ κ²°κ³Ό:", arr)
  1. μ‚½μž… μ •λ ¬
  • λͺ¨λ“  데이터λ₯Ό ν›‘μ–΄λ΄„ 일단, κ°€μž₯ μž‘μ€ 데이터λ₯Ό 선택해 맨 μ•žμ— μžˆλŠ” 데이터와 λ°”κΎΈκ³ , κ·Έλ‹€μŒ μž‘μ€ 데이터λ₯Ό 선택해 μ•žμ—μ„œ 두 번째 데이터와 λ°”κΎΈλŠ” κ³Όμ • 반볡
public static void insertionSort(int[] array) {
    int n = array.length;
    for (int i = 1; i < n; i++) {   // 두 번째 μš”μ†ŒλΆ€ν„° μ‹œμž‘
        int key = array[i];         // ν˜„μž¬ μš”μ†Œλ₯Ό μ €μž₯
        int j = i - 1;
        while (j >= 0 && array[j] > key) {   // μ™Όμͺ½μ˜ λͺ¨λ“  μš”μ†Œμ™€ λΉ„κ΅ν•˜μ—¬ key보닀 큰 값은 였λ₯Έμͺ½μœΌλ‘œ 이동
            array[j + 1] = array[j];
            j--;
        }
        array[j + 1] = key;     // key 값을 μ μ ˆν•œ μœ„μΉ˜μ— μ‚½μž…
    }
}
  • 파이썬 μ½”λ“œ
def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key

# μ‚¬μš© μ˜ˆμ‹œ
arr = [5, 3, 8, 2, 1, 4]
insertion_sort(arr)
print("μ‚½μž… μ •λ ¬ κ²°κ³Ό:", arr)
  1. 선택 μ •λ ¬
  • ν•„μš”ν•  λ•Œλ§Œ λ°”κΏ”μ„œ 효율적
  • 두 번째 즉, 1μΈλ±μŠ€λΆ€ν„° ν›‘μ–΄λ΄„ μ™œλƒ 첫 λ²ˆμ§ΈκΊΌλŠ” μ •λ ¬λ˜μ–΄ μžˆλ‹€κ³  μƒκ°ν•˜λ‹ˆκΉŒ
  • 그리고 λ‘λ²ˆμ§Έκ°€ μ™Όμͺ½μ„ 바라보고 μ™Όμͺ½μ΄ 더 크닀면 λ°”κΏˆ
  • μ΄λŸ°μ‹μœΌλ‘œ μ μ ˆν•œ μœ„μΉ˜μ— λ„£κΈ° λ•Œλ¬Έμ— 거의 μ •λ ¬λ˜μ–΄ μžˆλ‹€κ³  νŒλ‹¨λ˜λŠ” 것에 μ’‹μŒ
public static void selectionSort(int[] array) {
    int n = array.length;
    for (int i = 0; i < n - 1; i++) {    // 맨 λ§ˆμ§€λ§‰ μš”μ†ŒλŠ” μžλ™μœΌλ‘œ μ •λ ¬λ˜λ―€λ‘œ n-1κΉŒμ§€λ§Œ 순회
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {    // i μ΄ν›„μ˜ μš”μ†Œλ“€κ³Ό λΉ„κ΅ν•˜μ—¬ μ΅œμ†Ÿκ°’μ˜ μœ„μΉ˜λ₯Ό 찾음
            if (array[j] < array[minIndex]) {
                minIndex = j;
            }
        }
        int temp = array[minIndex];     // μ΅œμ†Ÿκ°’μ„ ν˜„μž¬ μœ„μΉ˜λ‘œ 이동
        array[minIndex] = array[i];
        array[i] = temp;
    }
}
  • 파이썬 μ½”λ“œ
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_index = i
        for j in range(i+1, n):
            if arr[j] < arr[min_index]:
                min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]

# μ‚¬μš© μ˜ˆμ‹œ
arr = [5, 3, 8, 2, 1, 4]
selection_sort(arr)
print("선택 μ •λ ¬ κ²°κ³Ό:", arr)