본문 바로가기

old/Programming

[Java코드]알파벳 오더로 정렬하기

반응형

알파벳 오더로 정렬하기

아스키 코드 룰에 따라서
[숫자][대문자][소문자]로 정렬됩니다
0 ~ 9, A ~ Z, a ~ z

  • Time complexity : O( log n)
  • Space complexity : O(1)

List

    Collections.sort(List<String>_values);

Array

    Arrays.sort(String[]array);

  • Time complexity : O(n^2)
  • Space complexity : O(1)
    public static String[] LexicalOrder(String[] words) {
        int n = words.length;
        for (int i = 0; i < n - 1; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (words[i].compareTo(words[j]) > 0) {
                    String temp = words[i];
                    words[i] = words[j];
                    words[j] = temp;
                }
            }
        }
        return words;
    }
반응형

'old > Programming' 카테고리의 다른 글