μ°Έκ³ λμ λ°©μ λ§ν¬ : https://visualgo.net/en/sorting
- μ΄λ€ λ°μ΄ν°λ€μ΄ μ£Όμ΄μ‘μλ, μ΄λ₯Ό μ ν΄μ§ μμλλ‘ λμ΄νλ κ²
- λκ°λ₯Ό
μ λ ¬νλ κ²- AλΆν° ZκΉμ§ κΈ°μ€μΌλ‘ μ λ ¬
- ν° μμμ μμ μ κΈ°μ€μΌλ‘ μ λ ¬
- λ°μ΄ν°λ₯Ό νΉμ ν κΈ°μ€μ λ°λΌμ μμλλ‘ λμ΄νλ κ²
- νλ‘κ·Έλ¨ μμ± μ κ°μ₯ λ§μ΄ μ¬μ©
μ΄μ§κ²μμ²λΌ λΉ λ₯Έ μκ³ λ¦¬μ¦ μ¬μ©μ μν΄μ- μκ³ λ¦¬μ¦μ
ν¨μ¨μ±μ μ½κ² μ΄ν΄ κ°λ₯ - λ΄λ¦Όμ°¨μμ μ€λ¦μ°¨μμ reverseλ₯Ό μ΄μ©ν΄μ λ€μ§μΌλ©΄ λ¨
- λ μΈμ ν λ°μ΄ν°λ₯Ό λΉκ΅ν΄μ , μμ μλ λ°μ΄ν°κ° λ€μ μλ λ°μ΄ν°λ³΄λ€ ν¬λ©΄, μ리λ₯Ό λ°κΎΈλ μκ³ λ¦¬μ¦
- μμ£Ό μ¬μ©λμ§λ μμ λ λΉ λ₯Έ μκ³ λ¦¬μ¦μ΄ λ§κΈ° λλ¬Έμ΄λ€.
-
λ°°μ΄μ 2κ°μ μμ΄ν μ
μ ν -
λΉκ΅(comparisons): μΌμͺ½μ΄ μ€λ₯Έμͺ½λ³΄λ€ ν¬λ©΄κ΅ν(swap) -
μ€λ₯Έμͺ½μΌλ‘
μ΄λν΄μ, ν΄λΉ νλ‘μΈμ€λ°λ³΅, μλ₯Ό λ€μ΄ 5μ 6μ λΉκ΅β μΌμͺ½μ΄ μ€λ₯Έμͺ½λ³΄λ€ μμΌλ―λ‘ κ΅ννμ§ μμ
- κ·Έ λ€μμ 6κ³Ό 3μ λΉκ΅ β κ΅ν κ²°κ³Ό : 2 5 3 6 1 4
- 6κ³Ό 1μ λΉκ΅ β κ΅ν κ²°κ³Ό : 2 5 3 1 6 4
- 6κ³Ό 4λ₯Ό λΉκ΅ β κ΅ν κ²°κ³Ό : 2 5 3 1 4 6 β 첫λ²μ§Έ μ¬μ΄ν΄ λ λλ²μ§Έλ μΌμͺ½μμ μ²μλΆν° λ°κΏ
-
comparisons(λΉκ΅) :
N-1N(itemμ λ§μ§λ§ λ²νΈ), λ°°μ΄μ N-1μ μμ΄ν μ λΉκ΅
EX. μμ΄ν μ΄ 6κ°λ©΄, 5λ² λΉκ΅
-
swap(κ΅ν): μ΅μ μ κ²½μ°, λͺ¨λ μμ΄ν μ κ΅νν΄μΌ ν¨
-
κ·Έλν
λ²λΈ μ λ ¬μ κ°λ¨νμ§λ§ ν° λ¦¬μ€νΈμ λν΄μλ λΉν¨μ¨μ μΌ μ μμΌλ μ°Έκ³
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);
}
}- κΈ°μ‘΄ μ½λμ ν΄λμ€λ₯Ό μ΄μ©ν΄μ λ§λλ λ°©λ²
- 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);
}
}
}- 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);
}
}- λ¬Έμ λ§ν¬ : https://jungol.co.kr/problem/1157?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjJ9
- μ½λ
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);
}
}- λ€λ₯Έ μ¬λ μ½λ
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();
}
}
}- νμ΄μ¬ μ½λ
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)- μ 체 λͺ¨λ μμ΄ν μ€μΊ
- λ°μ΄ν°κ° 무μμλ‘ μ¬λ¬ κ° μμ λ, μ΄μ€μμ κ°μ₯ μμ λ°μ΄ν°λ₯Ό μ νν΄ λ§¨ μμ μλ λ°μ΄ν°μ λ°κΎΈκ³ , κ·Έλ€μ μμ λ°μ΄ν°λ₯Ό μ νν΄ μμμ λ λ²μ§Έ λ°μ΄ν°μ λ°κΎΈλ κ³Όμ λ°λ³΅
- νμ¬ λ°μ΄ν°μ μν μκ΄μμ΄ λ¬΄μ‘°κ±΄ λͺ¨λ μμλ₯Ό λΉκ΅νκ³ μμΉλ₯Ό λ°κΏ
κ°μ₯ μμ κ²μ μ ν,λΉν¨μ¨μ
-
μ 체 μμ΄ν μ€ κ°μ₯ μμ μμ΄ν μ μμΉλ₯Ό κ·Έ μμΉλ₯Ό λ³μμ μ μ₯
-
5λΆν° μ°¨λ‘λ‘ νμΈ ν 맨 μ²μμ 5κ° μ μΌ μκΈ°λλ¬Έμ μμΉλ₯Ό λ³μμ μ μ₯
-
κ·Έ λ€μ 2μμ 2κ° 5λ³΄λ€ μμΌλκΉ μμΉλ₯Ό λ³μμ μ μ₯
-
μ νλ€κ° 1μ΄ 2λ³΄λ€ μμΌλκΉ μμΉλ₯Ό λ³μ μ μ₯
-
4κΉμ§ νμΈ ν μ¬μ΄ν΄μ λλ
β λ°°μ΄μμ κ°μ₯ μμ μ«μκ° μ΄λμ μλμ§ μλ€
-
κ·Έ λ€μ
swaps(λ°κΎΈκΈ°): κ°μ₯ μμ μ«μ(μμΉλ₯Ό μμ§!) κ·Έκ²μ 첫 λ²μ§Έ μμ΄ν κ³Ό λ°κΏ -
κ·Έ λ€μ μ¬μ΄ν΄μ μ§νμ 1λΆν° μμνμ§ μμ. 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));
}
}- μ μ¬ λ¬Έμ : https://jungol.co.kr/problem/1146/submission?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjB9
- μ½λ
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);
}
}- μ ν μ λ ¬ λ€λ₯Έ μ¬λ μ½λ
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");
}
}
}- νμ΄μ¬ μ½λ
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λΆν° μμμ, μΌμͺ½μ μ«μ β2βλ³΄λ€ ν° μ«μκ° μλμ§ νμΈ
β λ λ²μ§Έ λ°μ΄ν° λΆν° μμνλ μ΄μ λ 첫 λ²μ§Έλ κ·Έ μμ²΄λ‘ μ λ ¬λμ΄ μλ€κ³ νλ¨
Left > Right -
2μ 5 μ리 λ°κΎΈκΈ°
swap -
2λ²μ§Έ μ¬μ΄ν΄ : 6μ μ ν, μΌμͺ½μ λ ν° μ«μκ° μλμ§ νμΈ, μ€λ₯Έμͺ½μ΄ λ ν¬κΈ° λλ¬Έμ κ³μ μ§ν
Left < Right -
3λ²μ§Έ μ¬μ΄ν΄ : 3μ μ ν, μΌμͺ½μ λ ν° μ«μκ° μλμ§ νμΈ,
Left > Right -
6κ³Ό 3μ
swap -
3 μΌμͺ½μ μλ 5λ₯Ό λΉκ΅(comparisons)
Left > Right -
5κ³Ό 3μ
swap -
3μ μΌμͺ½μ μλ 2λ 3λ³΄λ€ μκΈ° λλ¬Έμ μλ°κΏ
-
λ°λ³΅
β μ½μ μ λ ¬μ, μ λ ¬μ΄ μ΄λ£¨μ΄μ§ μμλ νμ
μ€λ¦μ°¨μμ μ μ§νκΈ° λλ¬Έμ, νΉμ ν λ°μ΄ν°κ° μ½μ λ μμΉλ₯Ό μ μ ν λ(μ½μ λ μμΉλ₯Ό μ°ΎκΈ° μνμ¬ μΌμͺ½μΌλ‘ ν μΉΈμ© μ΄λν λ) μ½μ λ λ°μ΄ν°λ³΄λ€ μμ λ°μ΄ν°λ₯Ό λ§λλ©΄ κ·Έ μμΉμμ λ©μΆλ©΄ λλ€.
π‘ νΉμ ν λ°μ΄ν°μ μΌμͺ½μ μλ λ°μ΄ν°λ€μ μ΄λ―Έ μ λ ¬μ΄ λ μνμ΄λ―λ‘ μκΈ°λ³΄λ€ μμ λ°μ΄ν°λ₯Ό λ§λ¬λ€λ©΄, λ μ΄μ λ°μ΄ν° μ΄ν΄λ³Ό νμ μμ΄ μ½μ νλ©΄ λ¨
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));
}
}- λ¬Έμ λ§ν¬ : https://jungol.co.kr/problem/1158?cursor=eyJwcm9ibGVtc2V0IjoiNiIsImZpZWxkIjo2LCJpZHgiOjF9
- μ½λ
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);
}
}- λ€λ₯Έ μ¬λ μ½λ
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μ€ λ°λ³΅λ¬Έμ μ΄μ©νκΈ° λλ¬Έ
β μ νμ λ ¬λ³΄λ€ λΉ¨λΌλ μκ°λ³΅μ‘λλ κ°μ
β μ½μ μ λ ¬μ νμ¬ λ¦¬μ€νΈμ λ°μ΄ν°κ° κ±°μ μ λ ¬λμ΄ μλ μνλΌλ©΄ λ§€μ° λΉ λ₯΄κ² λμ
β¨ μκ°λ³΅μ‘λκ° λμΌν μ΄μ λ μ΅μ μ μλ리μ€λ₯Ό 보μ§λ§κ³ , νκ· μλ리μ€λ₯Ό λ΄μΌ νκΈ° λλ¬Έ
- λ²λΈ μ λ ¬
- 맨 μ²μμ μλ κ²μ μ μ₯ ν λ€μ μλ κ²μ λΉκ΅ ν΄μ λ€μ κ²μ΄ μ§κΈ μλ κ²λ³΄λ€ μ λ€λ©΄ κ·Έ μλ₯Ό 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)- μ½μ μ λ ¬
- λͺ¨λ λ°μ΄ν°λ₯Ό νμ΄λ΄ μΌλ¨, κ°μ₯ μμ λ°μ΄ν°λ₯Ό μ νν΄ λ§¨ μμ μλ λ°μ΄ν°μ λ°κΎΈκ³ , κ·Έλ€μ μμ λ°μ΄ν°λ₯Ό μ νν΄ μμμ λ λ²μ§Έ λ°μ΄ν°μ λ°κΎΈλ κ³Όμ λ°λ³΅
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μΈλ±μ€λΆν° νμ΄λ΄ μλ 첫 λ²μ§ΈκΊΌλ μ λ ¬λμ΄ μλ€κ³ μκ°νλκΉ
- κ·Έλ¦¬κ³ λλ²μ§Έκ° μΌμͺ½μ λ°λΌλ³΄κ³ μΌμͺ½μ΄ λ ν¬λ€λ©΄ λ°κΏ
- μ΄λ°μμΌλ‘ μ μ ν μμΉμ λ£κΈ° λλ¬Έμ κ±°μ μ λ ¬λμ΄ μλ€κ³ νλ¨λλ κ²μ μ’μ
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)


