Sorting Problems — Complete Guide
이 글의 핵심
Master sorting problems for coding interviews. Learn Python sort(), custom sorting with key functions, and multi-condition sorting patterns.
Introduction
In coding interviews, sorting is often the first step to solving problems. Whether organizing candidates by score/time, or preparing for greedy/binary search algorithms, sort and key functions express conditions elegantly.
1. Basic Sorting
Python Sorting
Choose between in-place sorting (sort) or creating a new list (sorted) based on whether you need to preserve the original.
# Ascending order
arr = [5, 2, 8, 1, 9]
arr.sort()
print(arr) # [1, 2, 5, 8, 9]
# Descending order
arr.sort(reverse=True)
print(arr) # [9, 8, 5, 2, 1]
# Return new list
arr = [5, 2, 8, 1, 9]
sorted_arr = sorted(arr)
print(arr) # [5, 2, 8, 1, 9] (original preserved)
print(sorted_arr) # [1, 2, 5, 8, 9]
Time Complexity
sort(),sorted(): O(n log n) (Timsort)- Stable sorting guaranteed
2. Custom Sorting
Key Functions
# Sort by absolute value
arr = [-5, -2, 1, 3, -8]
arr.sort(key=abs)
print(arr) # [1, -2, 3, -5, -8]
# Sort by string length
words = ["apple", "pie", "banana"]
words.sort(key=len)
print(words) # ["pie", "apple", "banana"]
# Sort tuples (by first element)
students = [(85, "Alice"), (90, "Bob"), (80, "Charlie")]
students.sort()
print(students)
# [(80, 'Charlie'), (85, 'Alice'), (90, 'Bob')]
Multi-Condition Sorting
# Score descending, name ascending
students = [
("Alice", 85),
("Bob", 90),
("Charlie", 85),
("David", 90)
]
students.sort(key=lambda x: (-x[1], x[0]))
print(students)
# [('Bob', 90), ('David', 90), ('Alice', 85), ('Charlie', 85)]
# Age ascending, name descending
people = [
("Alice", 30),
("Bob", 25),
("Charlie", 30),
]
people.sort(key=lambda x: (x[1], -ord(x[0][0])))
cmp_to_key
from functools import cmp_to_key
def compare(x, y):
if x > y:
return 1
elif x < y:
return -1
else:
return 0
arr = [5, 2, 8, 1]
arr.sort(key=cmp_to_key(compare))
print(arr) # [1, 2, 5, 8]
3. Problem Solving
Problem 1: Largest Number
def largest_number(nums):
"""
Concatenate numbers to form largest number
[3, 30, 34, 5, 9] → "9534330"
"""
from functools import cmp_to_key
def compare(x, y):
if x + y > y + x:
return -1
elif x + y < y + x:
return 1
else:
return 0
nums_str = list(map(str, nums))
nums_str.sort(key=cmp_to_key(compare))
result = '.join(nums_str)
return '0' if result[0] == '0' else result
print(largest_number([3, 30, 34, 5, 9])) # "9534330"
print(largest_number([0, 0, 0])) # "0"
Problem 2: H-Index
def h_index(citations):
"""
Find h-index: h papers cited at least h times
[3, 0, 6, 1, 5] → 3
"""
citations.sort(reverse=True)
h = 0
for i, citation in enumerate(citations):
if citation >= i + 1:
h = i + 1
else:
break
return h
print(h_index([3, 0, 6, 1, 5])) # 3
print(h_index([10, 8, 5, 4, 3])) # 4
Problem 3: Merge Intervals
def merge_intervals(intervals):
"""
Merge overlapping intervals
[[1,3],[2,6],[8,10],[15,18]] → [[1,6],[8,10],[15,18]]
"""
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for current in intervals[1:]:
last = merged[-1]
if current[0] <= last[1]:
merged[-1] = [last[0], max(last[1], current[1])]
else:
merged.append(current)
return merged
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
print(merge_intervals(intervals))
# [[1, 6], [8, 10], [15, 18]]
Problem 4: Meeting Rooms
def max_meetings(meetings):
"""
Maximum non-overlapping meetings
[(1, 4), (3, 5), (0, 6), (5, 7)] → 3
"""
meetings.sort(key=lambda x: x[1]) # Sort by end time
count = 0
last_end = 0
for start, end in meetings:
if start >= last_end:
count += 1
last_end = end
return count
meetings = [(1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9)]
print(max_meetings(meetings)) # 4
4. Sorting Patterns
Pattern 1: Sort + Two Pointers
def two_sum_sorted(arr, target):
"""
Sort then use two pointers to find sum
"""
arr.sort()
left, right = 0, len(arr) - 1
while left < right:
current_sum = arr[left] + arr[right]
if current_sum == target:
return [arr[left], arr[right]]
elif current_sum < target:
left += 1
else:
right -= 1
return None
print(two_sum_sorted([2, 7, 11, 15], 9)) # [2, 7]
Pattern 2: Sort + Binary Search
from bisect import bisect_left
def count_smaller(arr, target):
"""
Sort then binary search
"""
arr.sort()
idx = bisect_left(arr, target)
return idx
arr = [5, 2, 8, 1, 9]
print(count_smaller(arr, 6)) # 3 (1, 2, 5)
Pattern 3: Sort + Greedy
def assign_cookies(children, cookies):
"""
Assign cookies to children (maximize satisfaction)
"""
children.sort()
cookies.sort()
i = j = 0
while i < len(children) and j < len(cookies):
if cookies[j] >= children[i]:
i += 1
j += 1
return i
children = [1, 2, 3]
cookies = [1, 1]
print(assign_cookies(children, cookies)) # 1
Pattern 4: Custom Comparator
from functools import cmp_to_key
def sort_by_custom_rule(arr):
"""
Sort with custom comparison logic
"""
def compare(x, y):
# Custom logic here
if some_condition(x, y):
return -1
elif other_condition(x, y):
return 1
else:
return 0
arr.sort(key=cmp_to_key(compare))
return arr
5. Practical Tips
Sorting Debugging
def debug_sort(arr, key=None):
"""Print sorting process"""
print("Original:", arr)
sorted_arr = sorted(arr, key=key)
print("Sorted:", sorted_arr)
return sorted_arr
students = [("Alice", 85), ("Bob", 90), ("Charlie", 80)]
debug_sort(students, key=lambda x: x[1])
Performance Measurement
import time
arr = list(range(100000, 0, -1))
start = time.time()
arr.sort()
end = time.time()
print(f"Sort time: {(end - start) * 1000:.2f}ms")
Common Mistakes
# ❌ Wrong: Modifying while iterating
arr = [3, 1, 4, 1, 5]
# for i, val in enumerate(arr):
# if val == 1:
# arr.remove(val) # Skips elements!
# ✅ Correct: Filter with list comprehension
arr = [x for x in arr if x != 1]
# ❌ Wrong: Sorting without key
words = ["apple", "Banana", "cherry"]
words.sort() # ['Banana', 'apple', 'cherry'] (uppercase first)
# ✅ Correct: Case-insensitive
words.sort(key=str.lower) # ['apple', 'Banana', 'cherry']
Summary
Key Points
- Python Sorting: sort(), sorted(), key functions
- Custom Sorting: lambda, cmp_to_key
- Multi-Condition: Return tuple (condition1, condition2)
- Usage Patterns: Two pointers, binary search, greedy
- Time Complexity: O(n log n)
When to Sort
| Problem Type | Approach | Reason |
|---|---|---|
| Find pairs | Sort + Two Pointers | O(n) after sort |
| Range queries | Sort + Binary Search | O(log n) search |
| Interval problems | Sort by start/end | Greedy selection |
| Frequency problems | Sort by count | Top-k elements |
Recommended Problems
Beginner
- LeetCode 88: Merge Sorted Array
- LeetCode 242: Valid Anagram
- LeetCode 349: Intersection of Two Arrays
Intermediate
- LeetCode 56: Merge Intervals
- LeetCode 75: Sort Colors
- LeetCode 148: Sort List
Advanced
- LeetCode 315: Count of Smaller Numbers After Self
- LeetCode 327: Count of Range Sum
- LeetCode 493: Reverse Pairs
Related Posts
- Arrays and Lists | Essential Data Structures
- Sorting Algorithms | Bubble, Selection, Insertion Sort
- Binary Search | O(log n) Search Algorithm
Keywords
Sorting, Algorithm, Python sort, Custom Sorting, Key Function, Multi-Condition Sort, Coding Interview, Problem Solving, Time Complexity, Stable Sort
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Master sorting problems for coding interviews. Learn Python sort(), custom sorting with key functions, and multi-conditi… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- 정렬 알고리즘 | 버블, 선택, 삽입 정렬 완벽 정리
- 고급 정렬 | 퀵, 병합, 힙 정렬 O(n log n) 완벽 정리
- [Arrays and Lists](/en/blog/algorithm-series-01-array-list/
이 글에서 다루는 키워드 (관련 검색어)
Algorithm, Sorting, Problem Solving, Coding Interview, Python, sort 등으로 검색하시면 이 글이 도움이 됩니다.