๋ฌธ์ œ 1: ์˜ค๋ฆ„์ฐจ์ˆœ ์„ ํƒ ์ •๋ ฌ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ๋‹ค.

[10, 3, 8, 15, 6]

๋ฌธ์ œ : ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ผ.

์ถœ๋ ฅ ์˜ˆ์‹œ :

[3, 6, 8, 10, 15]

 

<๋‚˜์˜ ๋‹ต์•ˆ>

import java.util.Arrays;

public static void selctionArrays (int[] arr){
   int n = arr.length;
   
   for(int i = o; i < n-1; i++) {
      int minIndex = i;
      for(int j = i+1; j < n; j++) {
         if(arr[j] < arr[minIndex]){
           minIndex = j
         }
      }
      int temp = arr[minIndex];
      arr[minIndex] = arr[i];
      arr[i] = temp;
   }
}

<์ฑ„์ >

๋ฌธ์ œ์  1. ๋ฉ”์„œ๋“œ ์ด๋ฆ„ ์ž˜๋ชป ์‚ฌ์šฉ

public static void selectionSort (int[] arr) {

๋ฉ”์„œ๋“œ ์ด๋ฆ„์ด selctionArrays ๋กœ ์ž‘์„ฑ๋˜๋‹ค.

selectionSort์ฒ˜๋Ÿผ ์ฒ ์ž๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ณ ์ณ์•ผ ํ•œ๋‹ค.

 

๋ฌธ์ œ์  2. for ๋ฃจํ”„์—์„œ ์†Œ๋ฌธ์ž o๋ฅผ ์‚ฌ์šฉ

for(int i = 0; i < n-1; i++) {

์ž๋ฐ”์—์„œ๋Š” ์†Œ๋ฌธ์ž o์™€ ์ˆซ์ž 0์ด ๋‹ค๋ฅด๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

๋ฌธ์ œ์  3. minIndex = j๋’ค์— ์„ธ๋ฏธ์ฝœ๋ก  ์—†์Œ

minIndex = j;

 

๋ชจ๋‘ ์‚ฌ์†Œํ•œ ์˜คํƒ€์™€ ;๋ฅผ ๋ถ™์ด์ง€ ์•Š์•„์„œ ํ‹€๋ ธ๋‹ค.

์‚ฌ์†Œํ•œ ์‹ค์ˆ˜๋ผ๋„ ๊ธฐ๋กํ•˜์—ฌ ์™„๋ฒฝํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌ์‚ฌํ•ด์•ผ์ง€!

 

<๋ง๋ถ™์ผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ>

public static void main(String[] args) {
   int[] arr = {10, 3, 8, 15, 6};
   selectionSort(arr);
   System.out.println(Arrays.toString(arr));

 


<์ •๋‹ต ์ฝ”๋“œ>

import java.util.Arrays;

public class SelectionSort {
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }

    public static void main(String[] args) {
        int[] arr = {10, 3, 8, 15, 6};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

์ถœ๋ ฅ ๋ฐฐ์—ด:

[3, 6, 8, 10, 15]

๋ฌธ์ œ 2: ๋‚ด๋ฆผ์ฐจ์ˆœ ์„ ํƒ ์ •๋ ฌ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ๋‹ค.

[12, 4, 9, 16, 7]

 ๋ฌธ์ œ: ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ผ.

  ์ถœ๋ ฅ ์˜ˆ์‹œ : 

[16, 12, 9, 7, 4]

< ๋‚˜์˜ ๋‹ต์•ˆ >

import java.util.Arrays;

public static void selectionSort(int[] arr) {
    int num = arr.length;
    for (int i = 0; i < n-1; i++) {
      int maxIndex = i;
      for (int j = i+1; j < n ; j++) {
         if (arr[j] > arr[maxIndex]) {
             maxIndex = j;
         }
      }
      int temp = arr[maxIndex];
      arr[maxIndex] = arr[j];
      arr[j] = arr[maxIndex]; 
      
    }
}

 

<์ฑ„์ >

๋ฌธ์ œ์  1: ๋ณ€์ˆ˜ ์ด๋ฆ„ ํ˜ผ๋™ (num๊ณผ n)

int num = arr.length;
for (int i = 0; i < num-1; i++) {

- ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•œ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด num์ธ๋ฐ, for ๋ฃจํ”„์—์„œ n์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

- n์€ ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

๋ฌธ์ œ์  2: arr[j]์™€ arr[maxIndex] ๊ตํ™˜ ์˜ค๋ฅ˜

int temp = arr[maxIndex];
arr[maxIndex] = arr[i];
arr[i] = temp;

<์ •๋‹ต ์ฝ”๋“œ>

import java.util.Arrays;

public class SelctionSort {
   public static void selectionSortDescending(int[] arr) {
        int num = arr.length; //๋ฐฐ์—ด ๊ธธ์ด ์ €์žฅ 
        
        for (int i = 0; i < num-1; i++) {
           int maxIndex = i; // ํ˜„์žฌ ์œ„์น˜๋ฅผ ์ตœ๋Œ€๊ฐ’ ์ธ๋ฑ์Šค๋กœ ๊ฐ€์ •
           
             for (int j = i+1 ; j < num; j++){
                 if (arr[j] > arr[maxIndex]) {
                      maxIndex = j;
                 }
             }
             int temp = arr[maxIndex];
             arr[maxIndex] = arr[i];
             arr[i] = temp;
           
        }
   }
   public static void main(String[] args) {
      int[] arr = {12, 4, 9, 16, 7};
      selectionSortDescending(arr);
      System.out.println(Arrays.toString(arr));
   }
}

 

<์ถœ๋ ฅ ๋ฐฐ์—ด>

[16, 12, 9, 7, 4]

 


๋ฌธ์ œ 3 : ๋ฌธ์ž์—ด ๋ฐฐ์—ด ์ •๋ ฌ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ด ์ฃผ์–ด์กŒ๋‹ค.

String[] arr = {"grape", "apple", "banana", "cherry", "fig"};

๋ฌธ์ œ : ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ(์˜ค๋ฆ„์ฐจ์ˆœ)๋กœ ์ •๋ ฌํ•˜์‹œ์˜ค

<์ถœ๋ ฅ์˜ˆ์‹œ>

["apple", "banana", "cherry", "fig", "grape"]

 

๋ฌธ์ œ ํ’€์ด ํžŒํŠธ

 ๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ๋Š” compareTo ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

   - a.compareTo(b):

         - a๊ฐ€ b๋ณด๋‹ค ์•ž์— ์žˆ์œผ๋ฉด ์Œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.

         - a๊ฐ€ b๋ณด๋‹ค ๋’ค์— ์žˆ์œผ๋ฉด ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜.

         - a์™€ b๊ฐ€ ๊ฐ™์œผ๋ฉด 0์„ ๋ฐ˜ํ™˜.

 

 

import java.util.Arrays;

public class SelctionSortStrings {
    public static void selectionSort(String[] arr) {
        int n = arr.length;
        
        for (int i = 0; i < n-1; i++) {
            int minIndex = i; // ํ˜„์žฌ ์œ„์น˜๋ฅผ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐ€์ •
            
            for (int j = i+1; j < n; j++) {
                if (arr[j].compareTo(arr[minIndex]) < 0) {
                    minIndex = j; // ๋” ์•ž์„œ๋Š” ๋ฌธ์ž์—ด์ด๋ฉด ์ตœ์†Œ๊ฐ’ ๊ฐฑ์‹  
                 }
            }
            //์ตœ์†Œ๊ฐ’๊ณผ ํ˜„์žฌ ์œ„์น˜ ๊ฐ’ ๊ตํ™˜
            String temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
    public static void main(String[] args) {
       String[] arr = {"grape", "apple", "banana", "cherry", "fig"};
       System.out.println("Before Sorting: " + Arrays.toString(arr));
        selectionSort(arr);
        System.out.println("After Sorting: " + Arrays.toString(arr));
    }

}

1. ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ๊ธฐ์ค€์œผ๋กœ ์žก๋Š”๋‹ค.

2. ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด ์ดํ›„์˜ ๋ฌธ์ž์—ด๋“ค์„ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์žฅ ์•ž์— ์˜ค๋Š”(์•ŒํŒŒ๋ฒณ ์ˆœ์„œ์ƒ ์ž‘์€) ๋ฌธ์ž์—ด์„ ์ฐพ๋Š”๋‹ค.

3. ์ฐพ์€ ๋ฌธ์ž์—ด๊ณผ ๊ธฐ์ค€ ๋ฌธ์ž์—ด์„ ๊ตํ™˜ํ•œ๋‹ค.

4. ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด๋กœ ์ด๋™ํ•˜์—ฌ ๋™์ผํ•œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค. 

5. ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ๊ฐ€๋ฉด ๋ชจ๋“  ๋ฌธ์ž์—ด์ด ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ์ •๋ ฌ๋œ๋‹ค. 

 

<์ถœ๋ ฅ>

Before Sorting: [grape, apple, banana, cherry, fig]
After Sorting: [apple, banana, cherry, fig, grape]

+ Recent posts