template<class ItemType>
int minIndex(ItemType values[], int start, int end)
{
int indexOfMin = start;
for (int index = start + 1; index <= end; index++)
{
if (values[index] < values[indexOfMin])
indexOfMin = index;
}
return indexOfMin
}
template<class ItemType>
void SelectionSort(ItemType values[], int numValues)
{
int endIndex = numValues - 1;
for (int current = 0; current < endIndex; current++)
{
Swap(values[current], values[minIndex(values, current, endIndex)]);
}
}
def selection_sort(array):
for i in range(len(array)):
min_index = i
for j in range(i + 1, len(array)):
if array[min_index] > array[j]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
return array
template<class ItemType>
void InsertItem(ItemType values[], int start, int end)
{
bool finished = false;
int current = end;
bool moreToSearch = (current != start);
while (moreToSearch && !finished)
{
if (values[current] < values[current - 1])
{
Swap(values[current], values[current - 1]);
current--;
moreToSearch = (current != start);
}
else
finished = true;
}
}
template<class ItemType>
void InsertionSort(ItemType values[], int numValues)
{
for (int count = 0; count < numValues; count++)
InsertItem(values, 0, count);
}
def insertion_sort(array):
for i in range(1, len(array)):
for j in range(i, 0, -1):
if array[j] < array[j - 1]:
array[j], array[j - 1] = array[j - 1], array[j]
else:
break
return array
이상적으로 반씩 쪼갰을 때
이미 정렬돼 있는 요소를 정렬했을 때
- split 후 반환되는 splitPoint가 정렬 순서로 나오기 때문에 split N번 실행
- split이 실행될 때마다 pivot과 값 N 번 비교
- 계산복잡도 O(N^2)
int Split(int values[], int first, int last) {
int pivot, temp;
int low, high;
low = first + 1;
high = last;
pivot = values[first];
while (low < high) {
while (low <= last && values[low] < pivot)
low++;
while (high >= first && values[high] > pivot)
high--;
if (low < high) {
temp = values[low];
values[low] = values[high];
values[high] = temp;
}
}
temp = values[first];
values[first] = values[high];
values[high] = temp;
return high;
}
void QuickSort(int values[], int first, int last) {
if (first < last) {
int splitPoint = Split(values, first, last);
QuickSort(values, first, splitPoint - 1);
QuickSort(values, splitPoint + 1, last);
}
}
def quick_sort(array, start, end):
if start >= end:
return
pivot = start
left = start + 1
right = end
while left <= right:
while left <= end and array[left] <= array[pivot]:
left += 1
while right > start and array[right] >= array[pivot]:
right -= 1
if left > right:
array[right], array[pivot] = array[pivot], array[right]
else:
array[left], array[right] = array[right], array[left]
quick_sort(array, start, right -1)
quick_sort(array, right + 1, end)
return array
def quick_sort_two(array):
if len(array) <= 1:
return array
pivot = array[0]
tail = array[1:]
left_side = [x for x in tail if x <= pivot]
right_side = [x for x in tail if x > pivot]
return quick_sort_two(left_side) + [pivot] + quick_sort_two(right_side)
void counting_sort(int* arr, int size)
{
int* counting, *sorted;
int maxNum = 0;
for (int i = 0; i < size; i++) if (arr[i] > maxNum) maxNum = arr[i];
counting = new int[maxNum + 1]{0};
sorted = new int[size] {0};
for (int i = 0; i < size; i++) counting[arr[i]]++;
for (int i = 1; i <= maxNum; i++) counting[i] += counting[i - 1];
for (int i = 0; i < size; i++)
{
sorted[counting[arr[i]] - 1] = arr[i];
counting[arr[i]]--;
}
for (int i = 0; i < size; i++) arr[i] = sorted[i];
delete[] counting;
delete[] sorted;
}
def counting_sort(array):
result = []
count = [0] * (max(array) + 1)
for i in range(len(array)):
count[array[i]] += 1
for i in range(len(count)):
for j in range(count[i]):
result.append(i)
return result
n = int(input())
array = []
for i in range(n):
array.append(input())
print(sorted(array))
n = int(input())
array = []
for i in range(n):
array.append(list(input().split()))
array.sort(key = lambda student: student[1])
for i in range(n):
print(array[0])
n, k = map(int, input().split())
array_one = list(map(int, input().split()))
array_two = list(map(int, input().split()))
array_one.sort()
array_two.sort(reverse=True)
for i in range(k):
if array_one[i] < array_two[i]:
array_one[i], array_two[i] = array_two[i], array_one[i]
print(sum(array_one))