๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ•ด์ปค๋žญํฌ. Sorting: Bubble Sort (java)

ttoance 2022. 3. 26. 19:36
 

Sorting: Bubble Sort | HackerRank

Find the minimum number of conditional checks taking place in Bubble Sort

www.hackerrank.com

 

์ž๋ฐ” [JAVA] - ๊ฑฐํ’ˆ ์ •๋ ฌ (Bubble Sort)

[์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ชจ์Œ] ๋”๋ณด๊ธฐ 1. ๊ณ„์ˆ˜ ์ •๋ ฌ (Counting Sort) 2. ์„ ํƒ ์ •๋ ฌ (Selection Sort) 3. ์‚ฝ์ž… ์ •๋ ฌ (Insertion Sort) 4. ๊ฑฐํ’ˆ ์ •๋ ฌ (Bubble Sort) - [ํ˜„์žฌ ํŽ˜์ด์ง€] 5. ์…ธ ์ •๋ ฌ (Shell Sort) 6. ํž™ ์ •๋ ฌ (He..

st-lab.tistory.com

- ์‹œ๊ฐ„๋ณต์žก๋„ :  O(n2) 

ใ„ด ํ•˜๋‚˜๋„ swap๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ํ„ด์„ ์ข…๋ฃŒํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‘์–ด ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Œ. 

for (int i = 1; i < a.size(); i ++) {
    
    boolean swapped = false; 
        
    for (int j = 0; j < (a.size() - i); j++) {
        if (a.get(j) > a.get(j + 1)) {
            int temp = a.get(j + 1);
            a.set(j + 1, a.get(j));
            a.set(j, temp);
            
            swapped = true; 
        }
    }            
    
    // ํ„ด ์ข…๋ฃŒ
    if(swapped == false) {
        break;
    }
}

 

 

๋‚ด ์†Œ์Šค์ฝ”๋“œ  

๋”๋ณด๊ธฐ
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'countSwaps' function below.
     *
     * The function accepts INTEGER_ARRAY a as parameter.
     */

    public static void countSwaps(List<Integer> a) {
    // Write your code here
    
        int swaps = 0;
        for (int i = 1; i < a.size(); i ++) {
            // debug 
            // System.out.println(i + " : " + a.get(i));
            
            for (int j = 0; j < (a.size() - i); j++) {
                // debug
                // System.out.println(i + " : " + a.get(j) + " > " + a.get(j + 1) );
                if (a.get(j) > a.get(j + 1)) {
                    int temp = a.get(j + 1);
                    a.set(j + 1, a.get(j));
                    a.set(j, temp);
                    swaps++;
                }
            }
            
            // debug 
            // System.out.println(i + " : " + a.get(i));
            // System.out.println(a);
            
        }
        System.out.println(String.format("Array is sorted in %s swaps.", swaps));
        System.out.println(String.format("First Element: %s ", a.get(0)));
        System.out.println(String.format("Last Element: %s ", a.get(a.size() - 1)));

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        Result.countSwaps(a);

        bufferedReader.close();
    }
}

 

๋ฐ˜์‘ํ˜•