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)]);
}
}
template<class ItemType>
void BubbleUp(ItemType values[], int startIndex, int endIndex)
{
for (int index = endIndex; index > startIndex; index--)
{
if (values[index] > values[index - 1])
Swap(values[index], values[index - 1]);
}
}
template<class ItemType>
void BubbleSort(ItemType values[], int numValues)
{
int current = 0;
while (current < numValues - 1)
{
BubbleUp(values, current, numValues - 1);
current++;
}
}
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);
}
template<class ItemType>
void HeapSort(ItemType values[], int numValues)
{
// leaf node가 아닌 곳부터 heap으로 만들어 줌
for (int index = numValues / 2 - 1; index >= 0; index--)
{
ReheapDown(values, index, numValues - 1);
}
for (int index = numVales - 1; index > 0; index--)
{
Swap(values[0], values[index]);
ReheapDown(values, 0, index-1);
}
}
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);
}
}
template<class ItemType>
void Merge(ItemType values[], int leftFirst, int leftLast, int rightFirst, int rightLast)
{
ItemType tempArray[SIZE];
int index = leftFirst;
int saveFirst = leftFirst;
while ((leftFirst <= leftLast) && (rightFirst <= rightLast))
{
if (values[leftFirst] < values[rightFirst])
{
tempArray[index] = values[leftFirst];
leftFirst++;
}
else
{
tempArray[index] = values[rightFirst];
rightFirst++;
}
index++;
}
while (leftFirst <= leftLast)
{
tempArray[index] = values[leftFirst];
leftFirst++;
index++;
}
while (rightFirst <= rightLast)
{
tempArray[index] = values[rightFirst];
rightFirst++;
index++;
}
for (index = saveFirst; index <= rightLast; index++)
values[index] = tempArray[index];
}
template<class ItemType>
void MergeSort(ItemType values[], int first, int last)
{
if (first < last)
{
int middle = (first + last) / 2;
MergeSort(values, first, middle);
MergeSort(values, middle + 1, last);
Merge(values, first, middle, middle + 1, last);
}
}
#include "QueType.h"
#include <math.h>
template<class ItemType>
int SubKey(ItemType values, int numPositions, int position)
{
if (values < pow(10, position-1))
return 0;
else
{
int key = (values % static_cast<int>(pow(10, position))) / pow(10, position - 1);
return key;
}
}
template<class ItemType>
void CollectQueues(ItemType values[], QueType<ItemType> queues[], int radix)
{
int index = 0;
ItemType item;
for (int counter = 0; counter < radix; counter++)
{
while (!queues[counter].IsEmpty())
{
queues[counter].Dequeue(item);
values[index] = item;
index++;
}
}
}
template<class ItemType>
void RadixSort(ItemType values[], int numValues, int numPositions, int radix)
{
QueType<int> queues[10];
int whichQueue;
for (int position = 1; position <= numPositions; position++)
{
for (int counter = 0; counter < numValues; counter++)
{
whichQueue = SubKey(values[counter], numPositions, position);
queues[whichQueue].Enqueue(values[counter]);
}
CollectQueues(values, queues, radix);
}
}
Sort | Best Case | Average Case | Worst Case |
---|---|---|---|
Selection Sort | O(N^2) | O(N^2) | O(N^2) |
Bubble Sort | O(N^2) | O(N^2) | O(N^2) |
Short Bubble Sort | O(N) | O(N^2) | O(N^2) |
Insertion Sort | O(1) | O(N^2) | O(N^2) |
Heap Sort | O(N*logN) | O(N*logN) | O(N*logN) |
Quick Sort | O(N*logN) | O(N*logN) | O(N^2) |
Merge Sort | O(N*logN) | O(N*logN) | O(N*logN) |
Index | item |
---|---|
0 | empty |
1 | 14001 |
2 | empty |
3 | 50002 |
4 | 00104 |
5 | 77003 |
6 | 42504 |
5번 index에 있는 77003을 삭제했을 때
Index | item |
---|---|
0 | empty |
1 | 14001 |
2 | empty |
3 | 50002 |
4 | 00104 |
5 | emty |
6 | 42504 |
42054를 찾기 위해 4번 index 접근
Linear Probing으로 5번 index 접근 (Empty)
42054는 6번 index에 존재함에도 불구하고 삭제로 인해 없는 아이템이라고 판단
item을 삭제할 경우 밑에 있는 item 중에 Linear Probing으로 밀린 item을 다시 위로 올려 주는 작업 필요
#ifndef _STUDENT_H
#define _STUDENT_H
#include <iostream>
using namespace std;
class Student
{
public:
void Print(ostream& out);
void InitValue(int _id, char* _name, float _gpa);
void getValue(int& _id, char* _name, float& _gpa);
char* getName();
void operator = (Student stu);
private:
int id;
char name[30];
float gpa;
};
void Print(ostream& out, Student stu[], int numelement);
void PrintByPointer(ostream& out, Student* values[], int numValues);
void Print(ostream& out, Student stu[], int numelement)
{
for (int i = 0; i < numelement; i++)
{
stu[i].Print(out);
}
}
void Student::Print(ostream& out)
{
out << id << "\t" << name << "\t" << gpa << endl;
}
void Student::InitValue(int _id, char* _name, float _gpa)
{
id = _id;
strcpy_s(name, sizeof(name), _name);
gpa = _gpa;
}
void Student::getValue(int& _id, char* _name, float& _gpa)
{
_id = id;
strcpy_s(_name, sizeof(name), name);
_gpa = gpa;
}
char* Student::getName()
{
return name;
}
void Student::operator = (Student stu)
{
id = stu.id;
strcpy_s(name, sizeof(name), stu.name);
gpa = stu.gpa;
}
void PrintByPointer(ostream& out, Student* values[], int numValues)
{
for (int i = 0; i < numValues; i++)
{
(*values[i]).Print(out);
}
}
#endif
#include "Student.h"
#include <string>
int MinIndex(Student values[], int startIndex, int endIndex) {
int IndexOfMin = startIndex;
for (int Index = startIndex + 1; Index <= endIndex; Index++) {
if (strcmp(values[Index].getName(), values[IndexOfMin].getName()) < 0)
IndexOfMin = Index;
}
return IndexOfMin;
}
void SelectionSort(Student values[], int numValues) {
int endIndex = numValues - 1;
for (int current = 0; current < endIndex; current++)
Swap(values[current], values[MinIndex(values, current, endIndex)]);
}
#include "Student.h"
void BubbleUp(Student values[], int startIndex, int endIndex) {
for (int index = endIndex; index > startIndex; index--) {
if (strcmp(values[index].getName(), values[index - 1].getName()) < 0)
Swap(values[index], values[index - 1]);
}
}
void BubbleSort(Student values[], int numValues) {
int current = 0;
while (current < numValues - 1) {
BubbleUp(values, current, numValues);
current++;
}
}
#include "Student.h"
void InsertItem(Student values[], int startIndex, int endIndex) {
bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);
while (moreToSearch && !finished) {
if (strcmp(values[current].getName(), values[current - 1].getName()) < 0) {
Swap(values[current], values[current - 1]);
current--;
moreToSearch = (current != startIndex);
}
else
finished = true;
}
}
void InsertionSort(Student values[], int numValues) {
for (int count = 0; count < numValues; count++)
InsertItem(values, 0, count);
}
template<class ItemType>
void HeapSort(ItemType values[], int numValues) {
int index;
Print(cout, values, 3);
std::cout << std::endl;
for (index = numValues / 2 - 1; index >= 0; index--)
ReheapDown(values, index, numValues - 1);
Print(cout, values, 3);
std::cout << std::endl;
for (index = numValues - 1; index >= 1; index--) {
Swap(values[0], values[index]);
ReheapDown(values, 0, index - 1);
}
}
from typing import MutableMapping
def selection_sort(values):
endIndex = len(values) - 1
for current in range(endIndex):
minIndex = values.index(min(values[current:]))
values[current], values[minIndex] = values[minIndex], values[current]
def bubble_sort(values):
for i in range(len(values) - 1, 0, -1):
for j in range(i):
if (values[j] > values[j + 1]):
values[j], values[j + 1] = values[j + 1], values[j]
def short_bubble(values, numValues):
current = 0
sorted = False
while(current < numValues - 1 and not sorted):
sorted = bubble_up(values, current, numValues - 1, sorted)
current +=1
def bubble_up(values, startIndex, endIndex, sort):
sorted = True
for index in range(endIndex, startIndex, -1):
if(values[index] < values[index - 1]):
values[index], values[index - 1] = values[index - 1], values[index]
sorted = False
return sorted
def insert_item(values, start, end):
finished = False
current = end
moreToSearch = (current != start)
while (moreToSearch and not finished):
if(values[current] < values[current - 1]):
values[current], values[current - 1] = values[current - 1], values[current]
current -= 1
moreToSearch = (current != start)
else:
finished = True
def insertion_sort(values):
for count in range(0, len(values)):
insert_item(values, 0, count)
def reheap_down(elements, root, bottom):
reheaped = False
leftChild = root * 2 + 1
while(leftChild <= bottom and not reheaped):
if(leftChild == bottom):
maxChild = leftChild
else:
rightChild = root * 2 + 2
if (elements[leftChild] <= elements[rightChild]):
maxChild = rightChild
else:
maxChild = leftChild
if(elements[root] < elements[maxChild]):
elements[root], elements[maxChild] = elements[maxChild], elements[root]
root = maxChild
leftChild = root * 2 + 1
else:
reheaped = True
def heap_sort(values, numValues):
for index in range(int(len(values)/2 - 1), 0, -1):
reheap_down(values, index, len(values)-1)
for index in range(int(len(values)-1), 0, -1):
values[0], values[index] = values[index], values[0]
reheap_down(values, 0, index - 1)
def split(values, first, last):
splitvalue = values[first]
saveFirst = first
onCorrectside = True
first += 1
while(first <= last):
while(onCorrectside):
if(values[first]>splitvalue):
onCorrectside = False
else:
first += 1
onCorrectside = (first <= last)
onCorrectside = (first <= last)
while(onCorrectside):
if(values[last] < splitvalue):
onCorrectside = False
else:
last -= 1
onCorrectside = (first <= last)
if(first <= last):
values[first], values[last] = values[last], values[first]
first += 1
last -= 1
splitPoint = last
values[saveFirst], values[splitPoint] = values[splitPoint], values[saveFirst]
return splitPoint
def quick_sort(values, first, last):
if(first < last):
splitPoint = split(values, first, last)
quick_sort(values, first,splitPoint-1 )
quick_sort(values, splitPoint+1, last)
return values
def merge_sort(values, first, last):
if(first < last):
middle = int((first + last) / 2)
merge_sort(values, first, middle)
merge_sort(values, middle + 1, last)
merge(values, first, middle, middle + 1, last)
def merge(values, leftFirst, leftLast, rightFirst, rightLast):
tempArray = []
saveFirst = leftFirst
while ((leftFirst <= leftLast) and (rightFirst <= rightLast)):
if (values[leftFirst] < values[rightFirst]):
tempArray.append(values[leftFirst])
leftFirst += 1
else:
tempArray.append(values[rightFirst])
rightFirst += 1
while(leftFirst <= leftLast):
tempArray.append(values[leftFirst])
leftFirst += 1
while(rightFirst <= rightLast):
tempArray.append(values[rightFirst])
rightFirst += 1
for index in range(saveFirst, rightLast):
values[index] = tempArray[index-saveFirst]